Selenium supports the testing of Ajax based web applications. Ajax is asynchronous JavaScript & XML which helps web pages dynamically load data from the web server without the need to refresh the complete page.
When automating Ajax, there are some known challenges like how to confirm whether the Ajax call is completed and page is updated. There may be a case where it takes longer than expected to complete the call and tests may show fake failure. To avoid this, we can use explicit and wait for the page to load completely.
We can also build a custom library to handle the synchronization issue as shown below:
package com.automation;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
public class Synchronization_WAIT {
public static WebElement isElementPresnt(WebDriver driver, String xpath, int time) {
WebElement ele = null;
for (int i = 0; i < time; i++) {
try {
ele = driver.findElement(By.xpath(xpath));
break;
} catch (Exception e) {
try {
Thread.sleep(1000);
} catch (InterruptedException e1) {
System.out.println("Waiting for element to appear on DOM");
}
}
}
return else;
}
}
Below is an example Test Script using the above library:
package com.automation;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.testng.annotations.Test;
import com.automation.Synchronization_WAIT;
public class TestScript_Wait {
@Test
public void checkBuses() {
WebDriver driver = new FirefoxDriver();
driver.manage().window().maximize();
driver.get("https://www.redbus.in/");
Synchronization_WAIT.isElementPresnt(driver, ".//*[@id='txtSource']", 20).sendKeys("Bangalore");
Synchronization_WAIT.isElementPresnt(driver, ".//*[@id='txtDestination']", 20).sendKeys("Chennai");
}
}
Post a Comment