问题
My test checks whether the user can log in to the site.
I wrote a code to alert that works but showing parts in catch
.
I tried to write alert without try
and catch
but displays an error.
How to write alert without try
and catch
and alert to be displayed(test to pass).
org.openqa.selenium.TimeoutException: Expected condition failed: waiting for alert to be present (tried for 5 second(s) with 500 MILLISECONDS interval)
This is my code:
public void getMessage() {
//Verify that is message preview
try {
WebDriverWait wait = new WebDriverWait(driver, 5);
wait.until(ExpectedConditions.alertIsPresent());
Alert alert = driver.switchTo().alert();
System.out.println(alert.getText());
alert.accept();
Assert.assertTrue(alert.getText().contains("ERROR: Invalid login credentials."));
} catch (Exception e) {
System.out.println("Alert is not displayed");
}
回答1:
Since you have confirmed that the dialog is not truly a JS alert dialog, you won't be able to use Alert
. The good news is that it's HTML just like any other part of the page so you can just scrape the element that contains the error text.
来源:https://stackoverflow.com/questions/42597943/alert-is-not-displayed