'HTMLUnit HandleAlert and Javascript
I'm attempting to log on to a web page that I cannot guarantee has previously been logged out of.
If the prior logoff was not successful then a javascript alert pops up which needs to be acknowledged, then logging back on will work.
1: Initial Logon 2: Acknowledge javascript alert 3: Second Logon
I can verify that this works using a web browser. I can also verify that my handleAlert function works. However... upon attempting my second logon the javascript alert opens back up.
I'm not much of a javascript or web expert. I've tried clearing the cache, reloading the page, emptying the form and re-setting the credentials, and I cannot get around this issue.
Are there any suggestions for what I'm either doing wrong or what I can do to troubleshoot?
import com.gargoylesoftware.htmlunit.BrowserVersion
import com.gargoylesoftware.htmlunit.*
isError = 0
def login() {
cancelPage = cancelButton.click()
form = cancelPage.getFormByName("loginForm");
userField = form.getInputByName('j_username');
passwordField = form.getInputByName('j_password');
submitButton = page.getElementById("loginBtnId");
cancelButton = page.getElementById("cancelBtnId");
userField.setValueAttribute(username);
passwordField.setValueAttribute(password);
submitButton = page.getElementById("loginBtnId")
submitButton.click()
}
try
{
if (!url.startsWith("https"))
{
url = "https://" + url;
}
conn = new WebClient(javaScriptTimeout:10000)
conn.waitForBackgroundJavaScript(10000)
conn.waitForBackgroundJavaScriptStartingBefore(3000)
conn.getOptions().setJavaScriptEnabled(true);
conn.getOptions().setCssEnabled(false);
conn.setAlertHandler(new AlertHandler() {
void handleAlert(Page page,String errorMessage) {
println "\nIn handleAlert routine"
isError = isError + 1
if (isError == 1) {
login()
}
}
});
//get page
page = conn.getPage(url)
form = page.getFormByName("loginForm");
//get username and password form input fields and submit button
userField = form.getInputByName('j_username');
passwordField = form.getInputByName('j_password');
submitButton = page.getElementById("loginBtnId");
cancelButton = page.getElementById("cancelBtnId");
submitButton.click()
}
catch (Exception e)
{
println "\nFAIL - Unexpected exception: " + e.getMessage();
for (trace in e.getStackTrace())
{
println "\n\t" + trace;
}
}
Solution 1:[1]
The alert handler is only for the alert dialog - to get you informed that there is an alert. Do not call you login code from the alert handler, you have to place this code directly after the click call that opens the alert.
Some more hints:
Maybe this is not a alert box maybe it is a confirm box and you need a confirm handler.
Have a look at the javadoc of the handler classes.
Make yourself familiar with the page your are driving, many modern js libs do not use alert/confirm at all. They use some js/css magic to show something that looks like an dialog but is part of the page dom tree. This has to be handled with the usual HtmlUnit API.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 |
