Tuesday, 4 March 2014

Selenium function SwitchTo, Alert and Comparison of two string

Below i have mentioned some selenium functions:


1) SwitchTo()
------------------

driver.switchTo().activeElement();
driver.findElement(By.id("ctl00_contentPlaceHolder_imgBtnClose")).click();

2) How to compare string in selenium?
---------------------------------------

 String name1 = driver.findElement(By.xpath(".//*[@id='SrvSideValidator']")).getText();
   
    if(name1.equals("Your account is not configured for this functionality. Please contact your administrator for assistance."))
    {
    System.out.println("pass");
    }
   
    else {
   
    System.out.println("Failed");
    }


3)How to handle alert in selenium?
---------------------------------------

@Test
public void testAlertOk()
{
//Now we would click on AlertButton
WebElement button = driver.findElement(By.id("AlerButton"));
button.click();
try {

//Now once we hit AlertButton we get the alert
Alert alert = driver.switchTo().alert();

//Text displayed on Alert using getText() method of Alert class
String AlertText = alert.getText();

//accept() method of Alert Class is used for ok button
alert.accept();

//Verify Alert displayed correct message to user
assertEquals("this is alert box",AlertText);

} catch (Exception e) {
e.printStackTrace();

}
}

For clicking on Cancel we need to use dismiss() method of Alert Class

alert.dismiss();

No comments:

Post a Comment