日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

Selenium 使用要点记录二

發(fā)布時間:2024/9/20 编程问答 46 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Selenium 使用要点记录二 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

為什么80%的碼農(nóng)都做不了架構(gòu)師?>>> ??

書接上回,最近項目里邊新的release需要move to uat。然后我很光榮的被委派去給tester執(zhí)行自動化測試腳本做support,讓我極度遺憾的是tester不是妹子,表示本絲注定單身孤獨終老的命啊。

好吧不扯淡了,在測試的過程中碰到幾個問題導(dǎo)致程序不夠穩(wěn)定,腳本也被噴不夠robust,我自己也噴page object模式就是shit,維護(hù)的人會shi的很難看。

1. 處理popup window問題處理的不夠好?

????a. 切換到新彈出的window

????????public?boolean?switchToWindowAttach(WebDriver?driver,?String?windowTitle,String?frameId)?{boolean?flag?=?false;try?{//記下當(dāng)前windowString?currentHandle?=?getDriver().getWindowHandle();Set<String>?handles?=?getDriver().getWindowHandles();for?(String?s?:?handles)?{if?(s.equals(currentHandle))?{continue;}?else?{driver.switchTo().window(s);if?(driver.getTitle().contains(windowTitle))?{if(!StringUtils.isBlank(frameId)){//有些window可能要切換到具體的iframe才能操作內(nèi)部元素//getDriver().switchTo().defaultContent()?切換回外層driver.switchTo().frame(frameId);}flag?=?true;loggerContxt.info("Switch?to?window:?"?+?windowTitle+?"?successfully!");break;}?else?{//如果當(dāng)前循環(huán)到的window不是需要切換的window則切換回最初windowdriver.switchTo().window(currentHandle);continue;}}}}?catch?(NoSuchWindowException?e)?{loggerContxt.fatal(String.format("Failed?to?swith?to?window?whose?title?contains::?",?windowTitle),e);flag?=?false;}return?flag;}

????b. 關(guān)掉處理完成的popup window

/***?close?popup?window?by?the?title?name*?@param?driver?WebDriver*?@param?title?the?title?of?the?window?need?to?be?closed*?@param?orginalHandle?the?Window?Handler?of?current?window?*?@return*/protected?void?closePopupByTitle(WebDriver?driver,?String?title,String?orginalHandle)?{for?(String?handle?:?driver.getWindowHandles())?{String?theTitle?=?driver.switchTo().window(handle).getTitle();//if?the?title?are?samilar,?then?closeif?(theTitle.contains(title))?{driver.close();}//switch?back?to?the?original?windowif?(!handle.equalsIgnoreCase(orginalHandle))?{driver.switchTo().window(orginalHandle);}}}

2. 需要等待頁面的某個元素加載完成再做后續(xù)操作?

Selenium提供了2個等待的操作,一種是隱式的,另一種,er,也不知道是不是叫現(xiàn)實的

a.

public?void?waitForElementLoading(Long?millis)?{driver.manage().timeouts().implicitlyWait(millis,?TimeUnit.MILLISECONDS);}

b.

public?WebElement?waitForElementByLocator(final?By?locator,?Long?timeOut)?{if?(timeOut?==?null)?{timeOut?=?60L;}WebElement?id?=?(new?WebDriverWait(getDriver(),?timeOut)).until(new?ExpectedCondition<WebElement>()?{@Overridepublic?WebElement?apply(WebDriver?d)?{return?d.findElement(locator);}});return?id;}

第一個就是隱式的等待啦。第二種我自己隱式的實現(xiàn)了ExceptedCondition,它的apply方法應(yīng)該會被回調(diào)。

ExceptedCondition提供了許多靜態(tài)的方法大家可以根據(jù)自己的需求來使用。

這兩種等待的區(qū)別和具體使用需要本絲研究一把,下次給大家介紹哈。額,表噴我,這準(zhǔn)備不足真心講不好。

轉(zhuǎn)載于:https://my.oschina.net/athhu/blog/272401

總結(jié)

以上是生活随笔為你收集整理的Selenium 使用要点记录二的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。