Archive for the ‘Selenium’ Category

Assert for elementNotPresent on a page in Selenium WebDriver

There would be some scenarios where we need to assert that a particular emenet is not present or element is hidden on a page.

we need to do the following to assertElementNotPresent:
Create a class by Name: public class Element

Inside the class Element.java add the following method by name isHidden()
public static Boolean isHidden( WebDriver webDriver, By by )

{

try

{

WebElement element = webDriver.findElement( by );

return false;

}

catch ( NoSuchElementException e )

{

return true;

}

}

what the above does is it gets the Element not found exception and returns true.
How to implement the above class and method:

In the actual test add the following:

assertTrue( Element.isHidden( getWebDriver(),   By.xpath( xpathLocator ) ) );

How to assert for Page is OK

How to assert for page is ok or how to assert that there is no HTTP 500 error

 

private void assertPageOk( WebDriver driver )

{

boolean hasFailed = driver.getPageSource().contains( “HTTP ERROR 500″ )

|| driver.getPageSource().contains( “HTTP ERROR: 503″ );

 

assertFalse( “Failed due to server error.  Please check the server logs.”,

hasFailed );

}

 

Method to Check Page Loaded before performing any actions in WebDriver

Use this code instead of  using WaitForPageLoad in your page classes.

this will make sure what ever Element locator you are entering is loaded before it performs any action related to that page from your test case.

So best way to put it is add a Page element which will load last on the page t make sure the page is completely loaded, this will reduce your wait statements or pause statement in the code which is not a great idea unless your test needs to do it.

@Override

public String getPageLoadedCheckElementLocator()

{

return ELEMENT_LOCATOR;

}

 

In your API which is common across all the pages add this

public CommonPage( WebDriver driver )

{

Driver = driver;

 

Wait< WebDriver > wait = new WebDriverWait( Driver, 90 );

wait.until( ElementVisible.byXPath( By.xpath( getPageLoadedCheckElementLocator() ) ) );

PageFactory.initElements( new AjaxElementLocatorFactory( driver, 60 ), this );

assertPageOk( driver );

}

 

 

Return top

INFORMATION

Do not forget to leave a comment if you find the posts useful.
 
38057