Showing posts with label AJAX. Show all posts
Showing posts with label AJAX. Show all posts

August 10, 2015

How to Use Explicit Wait in Selenium Webdriver

August 10, 2015 2
Here is another post which will explains you about Explicit wait.

Below is the definition of explicit wait from selenium docs:

An explicit waits is code you define to wait for a certain condition to occur before proceeding further in the code. The worst case of this is Thread.sleep(), which sets the condition to an exact time period to wait. There are some convenience methods provided that help you write code that will wait only as long as required. WebDriverWait in combination with ExpectedCondition is one way this can be accomplished.

In simple words...you can use explicit wait where Implicitwait doesnt work. Ex: For Ajax elements, dynamically loading elements Implicit wait will not work. To handle Ajax and Dynamically loading elements we have to use Explicit wait.


In Below example

After clicking Start button it will take some time get text "Hello World"

we have to wait until that text present / wait until the element loads.


Here is explicit wait example...






Explicit wait has so many options...

Below are some of the options in ExpectedConditions...



Here is the Code:






package sample;

import java.util.concurrent.TimeUnit;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;
import org.testng.annotations.AfterClass;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.Test;

public class Explicitwait_Test {
 public WebDriver driver;
 public WebDriverWait wait;

 @Test
 public void testExplicitwait_Test() throws Exception {

  driver.get("http://the-internet.herokuapp.com");
  driver.findElement(By.linkText("Dynamic Loading")).click();
  driver.findElement(By.linkText("Example 1: Element on page that is hidden")).click();
  driver.findElement(By.xpath("//div[@id='start']/button")).click();
//  Explicit wait --wait until element visible
  wait = new WebDriverWait(driver, 10);
  wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//div[@id='finish']/h4")));
  String msg=driver.findElement(By.xpath("//div[@id='finish']/h4")).getText();
  System.out.println("Message is : "+msg);
  
 }

 @BeforeClass
 public void beforeClass() {
  driver = new FirefoxDriver();
  driver.manage().window().maximize();
  driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
 }

 @AfterClass
 public void afterClass() throws Exception {
  driver.quit();
 }

}

December 13, 2013

Handling Cascading Dropdowns (Example for AJAX)

December 13, 2013 3
Handling Cascading Dropdowns (Example for AJAX)
Below post explains you how to handle Cascading Dropdowns. It a good example for Ajax.

Each time the selection of one the DropDownList controls changes, the CascadingDropDown makes a call to a specified web service to retrieve the list of values for the next DropDownList in the set.

CascadingDropDown enables a common scenario in which the contents of one list depends on the selection of another list and does so without having to embed the entire data set in the page or transfer it to the client at all.

Below code explains how to handle Cascading drop downs:


March 26, 2011

Working with AJAX pages or elements

March 26, 2011 0
Working with AJAX pages or elements
AJAX stands for Asynchronous JavaScript and XML

 AJAX allows the Web page to retrieve small amounts of data from the server without reloading the entire page. In AJAX driven web applications, data is retrieved from server without refreshing the page.

Using andWait commands will not work as the page is not actually refreshed. Pausing the test execution for a certain period of time is also not a good approach as web element might appear later or earlier than the stipulated period depending on the system’s responsiveness, load or other uncontrolled factors of the moment, leading to test failures. The best approach would be to wait for the needed element in a dynamic period and then continue the execution as soon as the element is found.


This is done using waitFor commands, as waitForElementPresent , which wait dynamically, checking for the desired condition every second and continuing to the next command in the script as soon as the condition is met.


Below is one example how to handle AJAX applications....


package newone;



import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.openqa.selenium.server.SeleniumServer;
import com.thoughtworks.selenium.*;


public class Ajax extends SeleneseTestCase{

public Selenium selenium;
public SeleniumServer seleniumserver;

@Before
public void setUp() throws Exception {

seleniumserver = new SeleniumServer();
selenium = new DefaultSelenium("localhost", 4444, "*iexplore", "http://");
seleniumserver.start();
selenium.start();

}

@Test
public void testAjax() throws Exception
{
selenium.open("http://ajaximpact.com/");
selenium.windowMaximize();
assertEquals("AJAX Tutorials, Examples, News, Events and much more", selenium.getTitle());
//Click on Products Link
selenium.click("Image12");
verifyEquals("AJAX PRODUCTS", selenium.getTitle());
//Waiting for Element until 60 seconds--we can change it to any value
for (int second = 0;; second++) {
if (second >= 60) fail("timeout");
try { if (selenium.isElementPresent("link=FarPoint Spread for Web Forms")) break; } catch (Exception e) {}
Thread.sleep(1000);
}

//After the link or element is present it will come out of for loop and execute the next command
selenium.click("link=FarPoint Spread for Web Forms");
selenium.waitForPageToLoad("30000");
assertEquals("Ajax Impact : FarPoint Spread for Web Forms", selenium.getTitle());
Thread.sleep(1000);
}
@After
public void tearDown() throws InterruptedException{
selenium.stop();
seleniumserver.stop();
}

}


Hope this post will help you....while working with AJAX applications..........