Tuesday, October 16, 2018

Selenium Firefox - Test using FindByElement( By.XPath() )

Today, the goal is to verify the home page for a user after login. Actually, a disclaimer is displayed to the user before the user can access the home page. A modal is displayed with the disclaimer agreement and waits for the user to accept the disclaimer before the application redirects the user to their homepage.

So, the title of the modal include this text: "Notice to All Users".

I want my test to find this modal title and verify that the text is "Notice to All Users". This will also confirm that my login test passes because when I enter an unrecognized username the login dialog continues to display as opposed to showing an error message or error page. (In my opinion, this is poor design of the application and I will fix this if I ever get the time.)

My Code:

[Test Script Class]
@Test
public void testLogin() {
    Application.login(webDriver, url, username, password);
    boolean isDisplayed = Application.isDisclaimerDisplayed(webDriver);
    Assert.assertTrue("User successfully logged into Application.", isDisplayed);
}

[Test Utility Class]
public static Boolean isDisclaimerDisplayed(WebDriver webDriver) {
    String xpathRegEx = "//h3[@class='modal-title']";
    String modalTitle = webDriver.findByElement(By.xpath(xpathRegEx).getText();
    return modalTitle.equalsIgnoreCase("Notice to All Users");
}

One side of Caution here: This is slower than other methods of finding elements like by ID or by Class Name because this method looks through the entire XML formatted HTML document to find the element expected.


References
https://www.softwaretestingmaterial.com/how-to-locate-element-by-xpath-locator/
https://saucelabs.com/resources/articles/selenium-tips-css-selectors


No comments:

Post a Comment