Ajax Selenium Webdriver
Ajax Selenium Webdriver
small amounts of data from the server without reloading the entire page. In AJA
X driven web applications, data is retrieved from server without refreshing the
page.
When we perform any action on Ajax controls, using Wait commands will not work a
s the page is not actually refreshed here. Pausing the test execution using thre
ads for a certain period of time is also not a good approach as web element migh
t appear later or earlier than the stipulated period of time depending on the sy
stem s responsiveness, load or other uncontrolled factors of the moment, leads to
test failures.
The best approach would be to wait for the required element in a dynamic period
and then continue the test execution as soon as the element is found/visible.
This can done achieved with WebDriverWait in combination with ExpectedCondition
, the best way to wait for an element dynamically, checking for the condition ev
ery second and continuing to the next command in the script as soon as the condi
tion is met.
There are many methods which are available to use with wait.until(ExpectedCondit
ions.anyCondition); The below is the image for the number of methods which are a
vailable.
Syntax:
WebDriverWait wait = new WebDriverWait(driver, waitTime);
wait.until(ExpectedConditions.presenceOfElementLocated(locator));
The above statement will check for the element presence on the DOM of a page. Th
is does not necessarily mean that the element is visible.
Syntax:
WebDriverWait wait = new WebDriverWait(driver, waitTime);
wait.until(ExpectedConditions.visibilityOfElementLocated(locator));
The above syntax will for the element present in the DOM of a page is visible.
Some times we may also need to check if the element is invisible or not. To do t
his we need use the below :
Syntax:
WebDriverWait wait = new WebDriverWait(driver, waitTime);
wait.until(ExpectedConditions.invisibilityOfElementLocated(locator));
Some times you will get an exception as ""org.openqa.selenium.WebDriverException
: Element is not clickable at point (611, 419). Other element would receive the
click:'. The below one is used to wait for the element to be clickable.
Syntax:
WebDriverWait wait = new WebDriverWait(driver, waitTime);
wait.until(ExpectedConditions.elementToBeClickable(locator));
WebDriver Waits Examples
We can use WebDriverWait class in many different cases. When ever we need to per
form any operation on element, we can use webdriver wait to check if the element
is Present or visible or enabled or disabled or Clickable etc.
We will look into different examples for all the above scenarios:
isElementPresent:
Below is the syntax to check for the element presence using WebDriverWait. Here
we need to pass locator and wait time as the parameters to the below method. Her
e it is checking that an element is present on the DOM of a page or not. That do
es not necessarily mean that the element is visible. ExpectedConditions will ret
urn true once the element is found in the DOM.
WebDriverWait wait = new WebDriverWait(driver, waitTime);
wait.until(ExpectedConditions.presenceOfElementLocated(locator));
We should use presenceOfElementLocated when we don't care about the element visi
ble or not, we just need to know if it's on the page.
We can also use the below syntax which is used to check if the element is presen
t or not. We can return true ONLY when the size of elements is greater than Zero
. That mean there exists atleast one element.
WebElement element = driver.findElements(By.cssSelector(""));
element.size()>0;
isElementClickable
Below is the syntax for checking an element is visible and enabled such that we
can click on the element. We need to pass wait time and the locator as parameter
s.
WebDriverWait wait = new WebDriverWait(driver, waitTime);
wait.until(ExpectedConditions.elementToBeClickable(locator));
isElementVisible
Below is the syntax to check if the element is present on the DOM of a page and
visible. Visibility means that the element is not just displayed but also should
also has a height and width that is greater than 0.
WebDriverWait wait = new WebDriverWait(driver, waitTime);
wait.until(ExpectedConditions.visibilityOfElementLocated(locator));
We can also use the below to check the element to be visible by WebElement.
WebDriverWait wait = new WebDriverWait(driver, waitTime);
wait..until(ExpectedConditions.visibilityOf(element));
We can also use the below to check all elements present on the web page are visi
ble. We need to pass list of WebElements.
List<WebElement> linkElements = driver.findelements(By.cssSelector('#linkhello')
);
WebDriverWait wait = new WebDriverWait(driver, waitTime);
wait..until(ExpectedConditions.visibilityOfAllElements(linkElements));
isElementInVisible
Below is the syntax which is used for checking that an element is either invisib
le or not present on the DOM.
WebDriverWait wait = new WebDriverWait(driver, waitTime);
wait.until(ExpectedConditions.invisibilityOfElementLocated(locator));
isElementEnabled
Below is the syntax which is used to check if the element is enabled or not
We specify a condition along with timeout value, so that tool waits to check for
the condition and then come out if nothing happens.
It is very important to set the timeout value in conditional synchronization, be
cause the tool should proceed further instead of making the tool to wait for a p
articular condition to satisfy.
In Selenium we have implicit Wait and Explicit Wait conditional statements. Chec
k here for Examples on how to use Webdriver Waits
1. Implicit Wait.
An implicit wait is to tell WebDriver to poll the DOM for a certain amount of ti
me when trying to find an element or elements if they are not immediately availa
ble.
The default setting is 0. Once when we define the implicit wait, it will set for
the life of the WebDriver object instance.
It is a mechanism which will be written once and applied for entire session auto
matically. It should be applied immediately once we initiate the Webdriver.
Implicit wait will not work all the commands/statements in the application. It w
ill work only for "FindElement" and "FindElements" statements.
If we set implicit wait, find element will not throw an exception if the element
is not found in first instance, instead it will poll for the element until the
timeout and then proceeds further. We should always remember to add the below sy
ntax immediately below the Webdriver statement.
Syntax:
driver.manage.TimeOuts.implicitwait(6,Timeunit.SECONDS);
Example using implicit timeout
WebDriver driver = new FirefoxDriver();
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
driver.get("http://www.google.com");
Explicit Wait:
We need to define a wait statement for certain condition to be satisfied until t
he specified timeout period. If the Webdriver finds the element within the timeo
ut period the code will get executed.
Explicit wait is mostly used when we need to Wait for a specific content/attribu
te change after performing any action, like when application gives AJAX call to
system and get dynamic data and render on UI.
Example: Like there are drop-downs Country and State, based on the country value
selected, the values in the state drop-down will change, which will take few se
conds of time to get the data based on user selection.
Example:
/*Explicit wait for state dropdown field*/
WebDriverWait wait = new WebDriverWait(driver, 10);
wait.until(ExpectedConditions.visibilityOfElementLocated(By.id("statedropdow
n")));
The above statement waits up to 10 seconds before throwing Exception (TimeoutExc
eption - Timed out after 10 seconds waiting for visibility of element) or if it
------------------------------------package com.pack.ajax;
import
import
import
import
import
import
import
import
import
org.openqa.selenium.By;
org.openqa.selenium.WebDriver;
org.openqa.selenium.WebElement;
org.openqa.selenium.firefox.FirefoxDriver;
org.openqa.selenium.support.ui.ExpectedConditions;
org.openqa.selenium.support.ui.WebDriverWait;
org.testng.Assert;
org.testng.annotations.BeforeClass;
org.testng.annotations.Test;
}
@Test
public void test_AjaxExample() {
/*Wait for grid to appear*/
By container = By.cssSelector(".demo-container");
wait = new WebDriverWait(driver, 5);
wait.until(ExpectedConditions.presenceOfElementLocated(container
));
/*Get the text before performing an ajax call*/
WebElement noDatesTextElement = driver.findElement(By.xpath("//d
iv[@class='RadAjaxPanel']/span"));
String textBeforeAjaxCall = noDatesTextElement.getText().trim();
/*Click on the date*/
driver.findElement(By.linkText("1")).click();
/*Wait for loader to disappear */
By loader = By.className("raDiv");
wait.until(ExpectedConditions.invisibilityOfElementLocated(loade
r));
/*Get the text after ajax call*/
WebElement selectedDatesTextElement = driver.findElement(By.xpat
h("//div[@class='RadAjaxPanel']/span"));
wait.until(ExpectedConditions.visibilityOf(selectedDatesTextElem
ent));
String textAfterAjaxCall = selectedDatesTextElement.getText().tr
im();
/*Verify both texts before ajax call and after ajax call text.*/
Assert.assertNotEquals(textBeforeAjaxCall, textAfterAjaxCall);
String expectedTextAfterAjaxCall = "Thursday, January 01, 2016";
/*Verify expected text with text updated after ajax call*/
Assert.assertEquals(textAfterAjaxCall, expectedTextAfterAjaxCall
);
}
}
--------------------------------