Computer >> 컴퓨터 >  >> 프로그램 작성 >> JavaScript

유창한 대기는 무엇을 수행합니까?

<시간/>

유창한 대기는 예외를 던지기 전에 빈도로 확인되는 조건에 대해 드라이버가 일시 중지되도록 하는 동적 대기입니다. 요소는 DOM에서 지속적으로 검색되지 않고 일정한 시간 간격으로 검색됩니다.

예를 들어 대기 시간이 5초인 경우 FluentWait는 일정한 간격으로 DOM을 모니터링합니다(시간 동안 폴링으로 정의됨). FluentWait에서는 조건에 따라 맞춤형 대기 메소드를 구축해야 합니다.

구문 -

Wait<WebDriver> w = new FluentWait< WebDriver >(driver)
.withTimeout (10, SECONDS)
.pollingEvery (2, SECONDS)
.ignoring (NoSuchElementException.class)

예시

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;
import org.openqa.selenium.support.ui.FluentWait;
public class Fluentwt {
   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();
      // fluent wait declaration
      Wait<WebDriver> w = new FluentWait<WebDriver>(driver).withTimeout
      (Duration.ofSeconds(30))
      .pollingEvery(Duration.ofSeconds(3)).ignoring(
      NoSuchElementException.class);
      WebElement fl = w.until(new Function<WebDriver, WebElement>() {
         // customized condition for fluent wait
         public WebElement apply(WebDriver driver) {
            if (driver.findElement(By.xpath("[//img[@title=’Whiteboard’"))
            .isDisplayed()) {
               return true;
            }else {
               return null;
            }
         }
      });
      driver.quit();
   }
}