Original URL: https://www.guru99.com/handling-ajax-call-selenium-webdriver.html
Ajax is a technique used for creating fast and dynamic web pages. This technique is asynchronous and uses a combination of Javascript and XML .
It will updates the part/s of a web page without reloading the whole page.
Some of the famous applications that uses AJAX technique are Gmail, Google Maps, Facebook, Youtube, etc.
In this tutorial, you will learn-
AJAX stands for Asynchronous JavaScript & XML, and it allows the Web page to retrieve small amounts of data from the server without reloading the entire page.
For example, when you click on submit button, JavaScript will make a request to the server, interpret the result and update the current screen without reloading the webpage.
From a tester‘s point of view, if you are checking the content or the element to be displayed , you need to wait till you get the response. During AJAX call the data is stored in XML format and retrieved from the server.
The biggest challenge in handling Ajax call is knowing the loading time for the web page. Since the loading of the web page will last only for a fraction of seconds, it is difficult for the tester to test such application through automation tool. For that, Selenium Webdriver has to use the wait method on this Ajax Call.
So by executing this wait command, selenium will suspend the execution of current Test Case and wait for the expected or new value. When the new value or field appears, the suspended test cases will get executed by Selenium Webdriver.
Following are the wait methods that Selenium Webdriver can use
But the problem with all these waits is, you have to mention the time out unit. What if the element is still not present within the time? So there is one more wait called Fluent wait.
import org.openqa.selenium.By; import org.openqa.selenium.WebDriver; import org.openqa.selenium.WebElement; import org.openqa.selenium.chrome.ChromeDriver; import org.openqa.selenium.support.ui.ExpectedConditions; import org.openqa.selenium.support.ui.WebDriverWait; import org.testng.Assert; import org.testng.annotations.BeforeClass; import org.testng.annotations.Test; public class Ajaxdemo { private String URL = "http://demo.guru99.com/test/ajax.html"; WebDriver driver; WebDriverWait wait; @BeforeClass public void setUp() { System.setProperty("webdriver.chrome.driver",".\\chromedriver.exe"); //create chrome instance driver = new ChromeDriver(); driver.manage().window().maximize(); driver.navigate().to(URL); } @Test public void test_AjaxExample() { By container = By.cssSelector(".container"); wait = new WebDriverWait(driver, 5); wait.until(ExpectedConditions.presenceOfElementLocated(container)); //Get the text before performing an ajax call WebElement noTextElement = driver.findElement(By.className("radiobutton")); String textBefore = noTextElement.getText().trim(); //Click on the radio button driver.findElement(By.id("yes")).click(); //Click on Check Button driver.findElement(By.id("buttoncheck")).click(); /*Get the text after ajax call*/ WebElement TextElement = driver.findElement(By.className("radiobutton")); wait.until(ExpectedConditions.visibilityOf(TextElement)); String textAfter = TextElement.getText().trim(); /*Verify both texts before ajax call and after ajax call text.*/ Assert.assertNotEquals(textBefore, textAfter); System.out.println("Ajax Call Performed"); String expectedText = "Radio button is checked and it‘s value is Yes"; /*Verify expected text with text updated after ajax call*/ Assert.assertEquals(textAfter, expectedText); driver.close(); } }
[Selenium+Java] Handling AJAX Call in Selenium Webdriver
原文:https://www.cnblogs.com/alicegu2009/p/9098699.html