How to work on a Radio Button in Selenium WebDriver.

The code below explains how t select a Radio button on any WebPage.

Because of Ajax on websites sometimes we need to reload the elements so we need to try and locate the element on the page so we use a for loop

 

public static RadioButton getRadio( WebDriver driver, String label, String locator )

{

RadioButton radio = null;

int i = 0;

while ( true )

{

i++ ;

try

{

radio = new RadioButton( label,

driver.findElement( By.xpath( locator ) ) );

break;

}

catch ( StaleElementReferenceException e )

{

sleep( 50 );

}

if ( i > 10 )

{

System.out.println( “[WARNING] Unable to find RadioButton 10 attempts” );

break;

}

}

return radio;

}

 

XPATH Select element by Class

Issue: How to select an Xpath by element class which has multiple values or a long class name

Solution:  we can use Class Contains function

//span[contains(@class, 'active')]/input

you can use the above expression when you are asserting for certain element on the page

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 );

}

 

Return top

INFORMATION

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