Computer >> Máy Tính >  >> Lập trình >> Javascript

Chờ đợi rõ ràng có tác dụng gì?


Thời gian chờ rõ ràng được áp dụng cho một phần tử cụ thể trong trang web. Nó sẽ tạm dừng việc thực hiện cho đến khi điều kiện được thỏa mãn. Chờ rõ ràng cũng là một động vì nếu thời gian chờ là mười lăm giây và các điều kiện (như chờ một phần tử có thể nhấp, hiển thị hoặc có thể chọn, v.v.) được thỏa mãn trước thời gian quy định này, điều khiển sẽ chuyển sang bước tiếp theo .

Chờ đợi rõ ràng có thể tùy chỉnh nhiều hơn vì chúng tôi có thể thiết lập nó cho điều kiện. Dưới đây là danh sách một số điều kiện mong đợi cho việc chờ đợi rõ ràng -

  • textToBePresentInElement ()

    Cú pháp

    w.until(ExpectedConditions.textToBePresentInElement(By.id(“<<id expression>>“), “Tutorialspoint”));
  • textToBeClickable ()

    Cú pháp

    w.until(ExpectedConditions.textToBeClickable(By.id(“<<id expression>>“)));
  • alertisPresent ()

    Cú pháp

    w.until(ExpectedConditions.alertisPresent())= null);
  • frameToBeAvailableAndSwitchToIt ()

    Cú pháp

    w.until(ExpectedConditions.frameToBeAvailableAndSwitchToIt(By.id(“<<frame id >>“)));

Rõ ràng là phức tạp về mặt triển khai, tuy nhiên nó không ảnh hưởng đến tốc độ thực thi và áp dụng cho một phần tử cụ thể trên một trang.

Trong thời gian chờ đợi rõ ràng, khi thời gian tối đa trôi qua, ElementNotVosystemException sẽ được ném.

Ví dụ

import org.openqa.selenium.By;
import org.openqa.selenium.Keys;
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.Wait;
public class Explictwt {
   public static void main(String[] args) {
      System.setProperty("webdriver.chrome.driver", "C:\\Users\\ghs6kor\\Desktop\\Java\\chromedriver.exe");
      WebDriver driver = new ChromeDriver();
      String url = "https://www.tutorialspoint.com/index.htm";
      driver.get(url);
      //implicit wait with time in seconds applied to each elements
      driver.manage().timeouts().implicitlyWait(12, TimeUnit.SECONDS);
      //Clicking on Coding Ground link
      driver.findElement(By.xpath("//span[text()=’Coding Ground’]")).click();
      // explicit wait declaration
      WebDriverWait w = new WebDriverWait(driver,10);
      // condition to wait for with textToBePresentInElement method
      w.until(ExpectedConditions.textToBePresentInElement(By.xpath("//img[@title=’Whiteboard’]"),”          Whiteboard”));
      driver.quit();
   }
}