Starting with Selenium/Cucumber, I want to check the presence of an element on my application (h1 title). For this I drew inspiration from the following code when writing this method:
@Then("the admin arrives on the clients/prospects dashboard")
public boolean the_admin_arrives_on_the_clients_prospects_dashboard() {
return driver.findElements(By.xpath("//h1[contains(text(), 'Dashboard')]")).size() != 0;
}
However, I would like my test to fail if the element is not present. Is the fact that the method returns the "false" value enough to make the test fail? Many thanks
CodePudding user response:
list.size() != 0 returning false indicates the list doesn't contain any elements.
That definately validates the element is not present.
CodePudding user response:
Your method returning false alone won't cause the test to fail. You'll need to add an assert to validate the boolean returned. I don't know what, if any, test library you are using like TestNG, JUnit, etc.
An example assert using TestNG would be
import static org.testng.AssertJUnit.*;
...
assertFalse(the_admin_arrives_on_the_clients_prospects_dashboard(), "Verify that the current page is the dashboard");
This will cause your test to fail if the returned bool is not false. I don't fully understand your scenario so it's possible you'll need to switch to assertTrue() or update the assert comment, etc.
See the docs for more info on TestNG asserts.
