How to "handle" an alert as per UnhandledAlertException? - java

This topic is sadly very poisoned by non-applying answers, e.g. 1, 2, 3, 4, etc. pp.
The issue:
One may do an action which, when successful, makes the website raise an alert.
E.g. click on a "logout" button to see an "Are you sure you want to logout?" alert:
public class Logout extends PageObject {
static final Target LOGOUT = /* logout button, which will display the alert when clicked */;
public Performable logout() {
return Task.where("{0} clicks on Logout"
, Ensure.that(LOGOUT).isDisplayed()
, Click.on(LOGOUT) // done manually: alert visible; done by Serenity: no alert
);
}
}
Serenity will cause an org.openqa.selenium.UnhandledAlertException during execution of Click.on and the alert will not be present after this task. If the next task would be "interact with alert" in any way or form (as done in all the linked articles and all others I have perused before posting this question), the next task will then always fail because there is no alert .
This can be easily observed in the browser window as well: The alert may show, if at all, for but a split second and immediately disappear.
The question:
Given that the alert has to be handled before the action causing it completes: How do I do that?
N.b.:
I have done a deeper analysis in this old bug report. Includes a call stack where one may place breakpoints inbetween the alert's appearance and disappearance.
Others have indicated that it is an issue pertaining to chromedriver - but using a different framework with the same chromedriver works just fine.
A workaround might be to disable screenshots, but
I do not want to do that!
Serenity indicates (via a log warning) that this is a user error. So I'd rather correct my error than work around it.

Related

JavaFX: How to check if page available or not

In my project I use simple JavaFX browser, that works in background and do some stuff without displaying it.
More precisely it submitted some form to one online page.
So, I ran into a problem: when this page doesn't available, I can't figure it out from my Java code, it looks like form wasn't submitted and clicks on Submit button do nothing, but in Chrome for example I see that the page isn't available.
So, is there an option to check from Java code if page is available?
Thanks in advance and sorry for bad English.
So, I found an answer.
There is Worker class in JavaFX that associates with WebEngine and it has a field with State type.
State is enum that has 6 options:
READY
SCHEDULED
RUNNING
SUCCEEDED
CANCELLED
FAILED
So State.FAILED can be used for handling errors.
For example, something like that (we'll assume we have WebEngine instance):
webEngine.getLoadWorker().stateProperty().addListener((observable, oldState, newState) -> {
if (newState == Worker.State.FAILED) {
doOnError();
return;
}
doOnSuccess();
});
webEndgine.load("example.com");
So, every time state is changed ObservableValue#changed will be called with new State value as one of parameteres and if state become FAILED we do some error processing.

Selenium alert only shows in debug mode

I am testing a site page using Webdriver/Java which contains many fields. One of the fields is a dropdown box (or select box) and it contains validation upon losing focus (e.g. tabbing out). So if I select a particular option in the dropdown and tab to the next field, an alert box will pop up saying "You cannot choose that!".
Now I am doing code that checks for the presence of the alert box, and accepts it, however this is only working for me in DEBUG mode. When running the test (i.e. not in debug) I get "Timed out after 10 seconds waiting for alert to be present Build info: version: '2.53.0'".
I understand that this is probably a timing issue since it works in DEBUG mode, but I can't understand why as I'm using ExpectedConditions.alertIsPresent(). The code where this is failing is here:
WebElement currentElement = driver.findElement(By.id("selectbox"));
Select currentSelect = new Select(currentElement);
currentSelect.selectByVisibleText(updatedValue);
currentElement.sendKeys(Keys.TAB);
System.out.println("milestoneA");
if ((exceptionExpected()) {
System.out.println("milestoneB");
wait.until(ExpectedConditions.alertIsPresent());
System.out.println("milestoneC");
checkAlertBox(getExpectedResultFromExcel());
}
In DEBUG mode, the code keeps going fine and all is good. In RUN mode in my logs I get up to milestoneB and then the above mentioned error is thrown.
Further to this, if I add a Thread.sleep(1000) before Tabbing, all works fine.
Any ideas please?
I would try to send the TAB key until the element loses the focus:
WebElement currentElement = driver.findElement(By.id("selectbox"));
currentElement.click();
currentElement.sendKeys("abcd");
// wait for the popup to be visible
wait.until(ExpectedConditions.visibilityOfElementLocated(By.cssSelector("#abc")));
// send the TAB key while the current element has focus
new WebDriverWait(driver, 20).until((WebDriver wd) -> {
currentElement.sendKeys(Keys.TAB);
return !wd.switchTo().activeElement().equals(currentElement);
});

Handle Logout Button Action of JavaFX Application

Here is part of my code that shows error. This is done inside public void start method. I have already done the Login part, whenever user logs in the id and password remains in the TextField. Which is why I have created a logout button so that when user clicks on it, I want the useridtextfield & passwordtextfield to be empty.
LogoutButton.setOnAction(e->
UserIdTextField==null;
PasswordTextField==null;);
I have searched over the internet about this problem but got no solution regarding JavaFX Logout handling. I have no idea about FXML so I would appreciate any help.
The syntax for a lambda expression with multiple statements is
logoutButton.setOnAction(e -> {
/* statements */
});
(I.e. you need to delimit the statements with { ... }.)
As pointed out in the comments, comparing the text fields to null is probably not what you want to do here, but it will at least compile.

Validator in ControlsFX Wizard last page

Is there any way of adding a Validator to the last page in ControlsFX Wizards?
I am currently using the following on the last page :
public void onEnteringPage(Wizard wizard) {
wizard.getValidationSupport().registerValidator(cb,
Validator.createEmptyValidator("The instance field is mandatory"));
But since this is the last page I am still able to click finish when the cb(choice box) is empty.
I had the same problem.
I think this is due to a bug in version 8.40.9 of ControlsFX where they forgot to check the FINISH Button against the invalid property, too.
Look here in the Issue Issue 521 at line 633. They have added the necessary call. Just wait for the next update, when the issue will be merged.
For a quick and dirty workaround just add another pane to your wizard. With a text "Finished" for example, because on the "next" buttons the validation works.

How to handle 'Are you sure you want to navigate away from this page' on page close?

I have an web application, which generates a JS message
'Are you sure you want to navigate away from this page?' when you try to open a new page.
I know I can handle this message by
Selenium.getAlert();
(or some modification of it, I haven't tried it yet)
But my main problem is that this message generates only when I leave this page.
In selenium I can leave page by using
Selenium.open("new address");
or
Selenium.back();
So I use code like this
Selenium.open("new address");
Selenium.getAlert();
But the problem is that Selenium.open doesn't finish and go to next code line in program until the new page is fully loaded, but the page can't be loaded until program goes to next code line and handles this alert. So it's ablocked situation and I don't know how to handle it.
I don't think it can be done, staying entirely within the Selenium RC API. I have cases similar to this that I handle by launching an AutoIt script, before the open(), that waits for the prompt and answers it. That only works on Windows, but if you need something for other systems, I'm sure there are equivalent tools.
Selenium has always had a problem with alerts and confirmations (which this is - a confirmation has an OK/Cancel choice) that occur duing page loading. There's even an ancient bug number enshrined in one of the error messages that explains that it can't catch them.
Use:
openAndWait(..)
Maybe this other SO question will help you.
I don't know if this will work, but it's an idea. You could try something like:
try {
driver.manage().timeouts().implicitlyWait(1, TimeUnit.SECONDS);
Selenium.open("new address");
} catch (Exception e) {
// Should throw after 1 second
}
// Now we may be able to interact with the alert.
Selenium.getAlert();

Categories

Resources