Zitsanzo za WebDriver Zosavuta, Zowonekera Poyera komanso Zodekha

Kodi pali kusiyana kotani pakati pa kudikirira kwathunthu, kudikira kotsimikizika ndi kuyembekezera bwino mu WebDriver? Makamaka, pali ubale wotani pakati pa WebDriverWait ndi FluentWait?

Nazi zitsanzo za kugwiritsa ntchito njira iliyonse yodikira mu WebDriver ndi Java.



Yembekezerani

An kudikira kotheratu ndikuuza WebDriver kuti afufuze DOM kwakanthawi kochepa poyesa kupeza chinthu kapena zinthu ngati sizikupezeka msanga. Makonda osakhazikika ndi 0. Akangokhazikitsidwa, kudikirira kwathunthu kumayikidwa pamoyo wa chinthu cha WebDriver.


Chitsanzo chogwiritsa ntchito kudikira kwathunthu

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'));

Kodi tiyenera kugwiritsa ntchito nthawi iti kudikira?


Nthawi zambiri, sizoyenera kugwiritsa ntchito kudikirira kwathunthu, pomwe tingagwiritse ntchito kudikirira momveka bwino kapena kuyembekezera bwino.





Dikirani Mwapoyera

An kudikira kotheratu ndi code yomwe mumatanthauzira kudikirira kuti zinthu zina zichitike musanapitilize pa code. WebDriverIyembekezerani mwachisawawa imayitanitsa ExpectCondition pama milliseconds 500 aliwonse mpaka ibwerere bwino.

Chitsanzo chogwiritsa ntchito kudikirira

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

Kodi tiyenera kugwiritsa ntchito nthawi yanji kudikira?


Titha kugwiritsa ntchito kudikira kotsimikizika ngati chinthu chimatenga nthawi yayitali kutsegula. Tidagwiritsanso ntchito kudikira kotsimikizika kuti tione ngati CSS ili ndi chinthu china (kupezeka, kudina, ndi zina zambiri) chomwe chingasinthe mu ntchito za Ajax.



Yembekezerani Bwino

Mukamagwiritsa ntchito FluentWait Mwachitsanzo, titha kunena:

  • Pafupipafupi pomwe FluentWait amayenera kuwunika momwe zinthu zilili.
  • Pewani mitundu yapadera yoyembekezera monga NoSuchElementExceptions mukamayang'ana chinthu patsamba.
  • Nthawi yayitali kwambiri yodikirira chikhalidwe

Chitsanzo chogwiritsa ntchito 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')); } });

Kodi tiyenera kugwiritsa ntchito liti FluentWait?


Mukayesa kuyesa kupezeka kwa chinthu chomwe chitha kuoneka pambuyo pa masekondi / mphindi iliyonse.



Kusiyanitsa Pakati pa WebDriverWait ndi FluentWait

WebDriverWait ndi gulu laling'ono la FluentWait. Mu FluentWait muli ndi zina zomwe mungasankhe, limodzi ndi nthawi yayitali yodikirira, monga nthawi yovota, kupatula kunyalanyaza zina.

Chifukwa chake, m'malo modikirira kenako ndikugwiritsa ntchito 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();

titha kugwiritsa ntchito:


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

Kuwerenga kwina: