Issue:- webelement (Button) isdisplayed() doesn't works for negative scenarios
Requirement:- I need to fail a test flow in case a button is not displayed on the screen and if its present, then proceed with the flow
Code:-
if (driver.findElement(By.id("button")).isDisplayed() == false) {
System.out.println("The Button isn't present. Exiting!!");
driver.findElement(By.linkText("Logout")).click();
}
else
{
//Proceed with the positive flow
}
In above code, if the button is not present at all on the screen, the test should fail (if statement should be executed, but it's not)
As TestAutomationEngr has mentioned, make sure there is only one type of such button on the page...
one more way you could test for negative flow in webdriver is by using a try and catch..
In your case,
boolean buttonFound=false;
try
{
new WebDriverWait(driver,10).until(ExpectedConditions.visibilityOfElementLocated(By.id("button")));
buttonFound=true;
}catch(Exception e)
{
System.out.println("The Button isn't present. Exiting!!");
driver.findElement(By.linkText("Logout")).click();
}
if(buttonFound)
{
//positive flow
}
here it'l wait for 10 secs for visibility of element,
if found, buttonFound is set to true,hence positive flow is executed
if not found, the message in catch clause will be displayed and logout link will be clicked
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With