Skip to main content

WebDriverWait(Handles AJAX) Fundamental - Selenium

public class testclass {
    public static void main(String[] args) {
        WebDriver driver = new FirefoxDriver();
        driver.get("http://somedomain/url_that_delays_loading");
     

 // Below is one way of implemening
          WebElement myDynamicElement = (new WebDriverWait(driver, 10)).until(new ExpectedCondition<WebElement>(){
            @Override
            public WebElement apply(WebDriver d) {
                return d.findElement(By.id("myDynamicElement"));
            }});
  
//Second easy way of implementing
       WebDriverWait wait = new WebDriverWait(driver, 10);
        WebElement element = wait.until(ExpectedConditions.presenceOfElementLocated(By.id("someid")));

          }} 


   
    }}

Above is explicit wait where you define code to wait for a certain condition to occur before proceeding further in the code. WebDriverWait in combination with ExpectedCondition is one way this can be accomplished.

This waits up to 10 seconds before throwing a TimeoutException or if it finds the element will return it in 0 - 10 seconds. WebDriverWait by default calls the ExpectedCondition every 500 milliseconds until it returns successfully. A successful return is for ExpectedCondition type is Boolean return true or not null return value for all other ExpectedCondition types.


Expected Condition :  There are some common conditions that are frequently come across when automating web browsers. Listed below are Implementations of each. Java happens to have convienence methods so you don’t have to code an ExpectedCondition class yourself or create your own utility package for them. Refer below for some of the condition

http://selenium.googlecode.com/svn/trunk/docs/api/java/org/openqa/selenium/support/ui/ExpectedConditions.html


---------------------------------------------------------------------------------------------------------------





There is nothing special about how to get the value of Ajax Droplist. The below code is self-explanatory.

 Few points in below code : 
1) The suggestion are in Table.
2) The number of suggestion is unknown at compile time, so we have to use increasing variable for creating xpath of suggestion element
3) Since loop to get all element will run indefinately so NoSuchElement exception needs to be handled.


package test;
import org.openqa.selenium.By;
import org.openqa.selenium.NoSuchElementException;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
public class googleajaxlist {
 public static void main(String[] args) throws InterruptedException{
 
  WebDriver driver = new FirefoxDriver();
  driver.get("https://www.google.com");
  driver.findElement(By.xpath("//input[@class='gbqfif']")).sendKeys("harp");
  Thread.sleep(4000);    //This sleep is to make sure suggestion got is for word we want.
  int  i = 1;
  String xp;
 
  try {
 
  while(true){
   System.out.println(String.valueOf(i));  // Needs to convert integer variable to String 
   xp= "//*[@id='gsr']/table/tbody/tr/td[2]/table/tbody/tr[" + String.valueOf(i) + "]/td/div/table/tbody/tr/td[1]";  //variable xpath string
   System.out.println(xp);
   System.out.println(driver.findElement(By.xpath(xp)).getText()) ;  // getText() is used to get the suggestion text
   i++;
 
  }
 
  } catch ( NoSuchElementException e ) {};  //Exception handling is done for non-existing element at end of the loop
 
 
   }

}

Comments

Popular posts from this blog

Reporting in Selenium

How do you Handle reporting in your project ? Method 1 : Maven - Testng - Surefire report The project will be created in Maven with  SureFire Plugin  and  SuiteXmlFile  tag in the POM.xml . The testng.xml will be called in the POM.            <plugin>                 <groupId>org.apache.maven.plugins</groupId>            <artifactId>maven-surefire-plugin</artifactId>            <version>2.18.1</version>                 <configuration>                     <suiteXmlFiles>                         <suiteXmlFile>                             ...

Handling Modal Dialog Window in Selenium Webdriver

Handling Modal Dialog Window in Selenium Webdriver In this tutorial we are going to see how to handle " Modal Dialog Window "  using Selenium Webdriver Disadvantage using Selenium Use Can't handle elements which is inside  Modal Dialog Window. Using Robot Class Using Robot Class we can handle  Modal Dialog Window. In this tutorial Open  http://vodkabears.github.io/remodal/# Click on  Show  button In the  Modal Dialog Window.  Click on Follow button  Source Code : import java.awt.AWTException; import java.awt.Robot; import java.awt.Toolkit; import org.openqa.selenium.By; import org.openqa.selenium.WebDriver; import org.openqa.selenium.firefox.FirefoxDriver; import java.awt.datatransfer.StringSelection; import java.awt.event.KeyEvent; public class robot_demo { public static void main(String[] args) throws InterruptedException, AWTException { WebDrive...

Autonium

About Autonium Autonium is a test automation framework for web applications. This framework is built by using Java and Selenium. It uses Maven project structure. Easy to understand and use it. Having basic idea on Java, Selenium, Maven and TestNG is good enough to start with Autonium. Let’s start Automation with Autonium --------------------------------------------------- Advantages Provides support to all major browsers –  Firefox, Chrome, IE and Safari . Capability of executing scripts on  Remote Machines  by using Selenium Server. Users can make use of Selenium Web driver methods and Autonium methods. Autonium simplifies  Test data management . Test data can be read from properties files. Scripts can be developed without  hard coding  test data . Identifiers can also be read from properties files and  hard coding   of identifiers can be avoided. Test data and Identifier v...