Professional Documents
Culture Documents
Working With AJAX Pages or Elementsall Scripts
Working With AJAX Pages or Elementsall Scripts
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.
package newone;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import
org.openqa.selenium.server.SeleniumServer;
import com.thoughtworks.selenium.*;
@Before
public void setUp() throws Exception {
@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);
}
Generally we will have scenarios like get a particular text from the table, but in this example i am
trying to get the number of the row based on the row text.
In this example a table contains number of rows and each row represents a company name. If we
have row number we can get the company name easily...the below scenarios is different ..based on
the company name it will find out the number of the row.
package scripts;
import java.util.List;
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.testng.annotations.AfterClass;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.Test;
public class Rowcount {
public WebDriver driver;
@Test()
public void markets() throws Throwable
{
String companyName="Agro Dutch";
int count=1;
driver.get("http://content.icicidirect.com");
driver.findElement(By.xpath("//div[@id='topNavLv1']/ul/li[3]/a")).click();
driver.findElement(By.xpath("//a[@id='M_Stats']")).click();
WebElement table=driver.findElement(By.xpath("//table[@id='gridSource']/tbody")) ;
List<WebElement> rows=table.findElements(By.tagName("tr"));
for(WebElement r : rows){
List<WebElement>colItems=r.findElements(By.tagName("td"));
String frstelement =colItems.get(0).getText();
if(!frstelement.isEmpty())
{
if(frstelement.equalsIgnoreCase(companyName))
{
break;
}
}
else
{
driver.findElement(By.xpath("//*[@id='gridSource']/tfoot/tr/td[3]")).click();
count++;
}
count++;
}
}
@BeforeClass
public void beforeClass() {
driver = new FirefoxDriver();
driver.manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS);
driver.manage().window().maximize();
}
@AfterClass
public void afterClass() {
driver.quit();
}