Friday, November 1, 2013

Java Script Alerts

In most of the web sites now users are tend to use java scripts to perform various functions. when there are java scripts running in the web site we have to handle those elements in a different way. 

As an example if there is a window or dialog box opening upon click on click of a button we cannot locate those kinds element like we did for other. Those needs to be handled differently. Below example shows how java script enabled sites can be handled. 

//Goes to the site 
WebDriver A = new FirefoxDriver();
A.get("http://www.javascripter.net/faq/prompt.htm");
//finds the try now button and clicks
A.findElement(By.xpath("//input[@value='Try it now']")).click(); 
//Before using the method the alert class should be declared as follows 
Alert Alert01 = A.switchTo().alert(); 

//Gets alert text value (if exist)
System.out.println(Alert01.getText()); 
Alert01.sendKeys("test");  //

//Accept or Dismiss alert dialog box 
Alert01.accept(); 
Alert01.dismiss(); 
//Quit the browser 
A.quit();


Alert class provides so many various methods to handle alerts, Such as 

Alert01.accept(); 
Alert01.dismiss(); 
Alert01.getText();
Alert01.sendKeys(arg);

These functionalists  makes the testers life easier when there are so many java scripts running inside a single web application. 


No comments:

Post a Comment