使用 Selenium 2 WebDriver 等待 Ajax 呼叫完成。


我們可以使用 Selenium webdriver 等待 Ajax 呼叫完成。由於 Ajax 響應導致的頁面載入時間確定是一項困難的任務。這可以透過 Selenium 中的**同步**概念和等待方法來實現。其中一些列在下面:

**隱式等待** - 它允許 webdriver 等待指定的時間,之後丟擲異常。此等待適用於測試中的所有步驟。

語法

driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);

示例

使用隱式等待的程式碼實現。

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import java.util.concurrent.TimeUnit;
public class AjaxImplWt{
   public static void main(String[] args) {
      System.setProperty("webdriver.chrome.driver",
         "C:\Users\ghs6kor\Desktop\Java\chromedriver.exe");
      WebDriver driver = new ChromeDriver();
      // wait for page load
      driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);
      driver.get("https://tutorialspoint.tw/index.htm");
      // identify element
      WebElement e=driver.findElement(By.className("gsc-input"));
      e.sendKeys("Java");
      driver.close();
   }
}

Ajax 響應呼叫也可以使用**顯式等待**來處理。此等待應用於特定元素。對某個元素的**預期條件**進行指定時間的等待。一旦等待時間已過,就會丟擲異常。

示例

使用顯式等待的程式碼實現。

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;
public class AjaxWaitExplicit{
   public static void main(String[] args)
   throws InterruptedException{
      System.setProperty("webdriver.chrome.driver",
      "C:\Users\ghs6kor\Desktop\Java\chromedriver.exe");
      WebDriver driver = new ChromeDriver();
      driver.get("https://tutorialspoint.tw/index.htm");
      // identify element
      WebElement l=driver.findElement(By.xpath("//*[text()='Library']"));
      l.click();
      //explicit wait condition
      WebDriverWait wt = new WebDriverWait(driver,5);
      wt.until(ExpectedConditions.
      invisibilityOfElementLocated(By.xpath("//*[@class='mui-btn']")));
      driver.quit();
   }
}

我們還可以為 Selenium 新增自定義**ExpectedConditions**。當 webdriver 提供的預設預期條件不足時,我們需要自定義 ExpectedConditions。使用 until 方法,它是**WebDriverWait**類的一部分。

示例

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;
public class AjaxCustomExpCondition{
   public static void main(String[] args)
   throws InterruptedException{
      System.setProperty("webdriver.chrome.driver",
      "C:\Users\ghs6kor\Desktop\Java\chromedriver.exe");
      WebDriver driver = new ChromeDriver();
      driver.manage().timeouts().implicitlyWait(4, TimeUnit.SECONDS);
      driver.get("https://tutorialspoint.tw/about/about_careers.htm");
      // identify element
      WebElement l=driver.findElement(By.linkText("Team"));
      l.click();
      //object of WebDriverWait class
      WebDriverWait w = new WebDriverWait(driver,7);
      //custom expected condition with until method
      w.until(new ExpectedCondition <Boolean> (){
         public Boolean apply(WebDriver driver) {
            //identify paragraph
            WebElement e= driver.findElement(By.tagName("p"));
            if (e!= null){
               //to check if paragraph is displayed and has text India
               if (e.isDisplayed() && e.getText().contains("India")) {
                  System.out.println("Element found");
                  return true;
               } else {
                  System.out.println("Element not found");
                  return false;
               }
            }
            return false;
         }
      });
      driver.close();
   }
}

此外,可以使用**JavaScript 執行器**來等待 Ajax 呼叫。**executeScript** 方法用於在 Selenium 中執行 JavaScript 命令。等待直到**jQuery.active**命令產生 0。

示例

使用 JavaScript 的程式碼實現。

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.JavascriptExecutor;
public class AjaxJavaScrptWt{
   public static void main(String[] args) {
      System.setProperty("webdriver.chrome.driver",
         "C:\Users\ghs6kor\Desktop\Java\chromedriver.exe");
      WebDriver driver = new ChromeDriver();
      driver.get("https://tutorialspoint.tw/index.htm");
      //wait with JavaScript Executor
      while (true){
         if ((Boolean) ((JavascriptExecutor)driver).
         .executeScript("return jQuery.active == 0")){
            break;
         }
         Thread.sleep(500);
      }
   }
}

更新於:2021年1月30日

2K+ 次瀏覽

開啟你的職業生涯

完成課程獲得認證

開始學習
廣告