Conto Enteni sing implisit, eksplisit lan lancar WebDriver

Apa bedane antara ngenteni implisit, ngenteni eksplisit lan ngenteni lancar ing WebDriver? Luwih khusus, apa hubungane WebDriverWait lan FluentWait?

Mangkene conto nggunakake saben metode tunggu ing WebDriver karo Java.



Ngenteni Implisit

An ngenteni implisit yaiku ngandhani WebDriver supaya poling DOM sajrone wektu tartamtu nalika nyoba golek elemen utawa elemen yen ora kasedhiya. Setelan standar yaiku 0. Sawise disetel, ngenteni implisit disetel kanggo urip kedadean obyek WebDriver.


Tuladha nggunakake tunggu implisit

WebDriver driver = new FirefoxDriver(); driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS); driver.get('http://somedomain/slow_loading_url'); WebElement dynamicElement = driver.findElement(By.id('dynamicElement'));

Kapan kita kudu ngenteni kanthi implisit?


Biasane, ora disaranake nggunakake ngenteni sing implisit, yen kita bisa nggunakake ngenteni sing eksplisit utawa ngenteni lancar.





Enteni Eksplisit

An ngenteni kanthi eksplisit yaiku kode sing sampeyan nemtokake kanggo ngenteni kedadeyan tartamtu sadurunge dilanjutake ing kode kasebut. WebDriverWait minangka standar nelpon ExpectedCondition saben 500 millisecond nganti sukses.

Tuladha nggunakake ngenteni eksplisit

WebDriver driver = new FirefoxDriver(); driver.get('http://somedomain/someurl'); WebElement dynamicElement = (new WebDriverWait(driver, 10))
.until(ExpectedConditions.presenceOfElementLocated(By.id('dynamicElement')));

Kapan kita kudu ngenteni kanthi eksplisit?


Biasane kita bakal nggunakake ngenteni eksplisit yen ana elemen sing butuh wektu suwe. Kita uga nggunakake ngenteni kanthi eksplisit kanggo mriksa properti CSS saka sawijining elemen (anane, clickability. Lsp) sing bisa ngganti aplikasi Ajax.



Ngenteni lancar

Nalika nggunakake Kayata FluentWait, kita bisa nemtokake:

  • Frekuensi FluentWait kudu mriksa kahanan sing wis ditemtokake.
  • Nglirwakake jinis khusus istiméwa sing nunggu kayata NoSuchElementExceptions nalika nggoleki elemen ing kaca kasebut.
  • Wektu maksimal kanggo ngenteni kondhisi

Tuladha nggunakake FluentWait

// Waiting 30 seconds for an element to be present on the page, checking // for its presence once every 5 seconds. Wait wait = new FluentWait(driver)
.withTimeout(30, SECONDS)
.pollingEvery(5, SECONDS)
.ignoring(NoSuchElementException.class); WebElement foo = wait.until(new Function() {
public WebElement apply(WebDriver driver) {
return driver.findElement(By.id('foo')); } });

Kapan kita kudu nggunakake FluentWait?


Nalika nyoba nyoba ngarsane elemen sing bisa ditampilake sawise saben x detik / menit.



Bedane WebDriverWait lan FluentWait

WebDriverWait minangka subkelas FluentWait. Ing FluentWait sampeyan duwe opsi liyane kanggo ngatur, bebarengan karo wektu tunggu maksimum, kaya interval polling, istiméwa kanggo ora digatekake lsp.

Dadi, tinimbang ngenteni banjur nggunakake findElement:

WebDriverWait wait = new WebDriverWait(driver, 18); wait.until(ExpectedConditions.elementToBeClickable(By.linkText('Account'))); WebElement element = driver.findElement(By.linkText('Account')); element.sendKeys(Keys.CONTROL); element.click();

kita bisa nggunakake:


WebElement element = wait.until(
ExpectedConditions.elementToBeClickable(By.linkText('Account')));

Wacan luwih lanjut: