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

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

Selenium 使用要点记录二

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

為什么80%的碼農都做不了架構師?>>> ??

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

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

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

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

????????public?boolean?switchToWindowAttach(WebDriver?driver,?String?windowTitle,String?frameId)?{boolean?flag?=?false;try?{//記下當前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才能操作內部元素//getDriver().switchTo().defaultContent()?切換回外層driver.switchTo().frame(frameId);}flag?=?true;loggerContxt.info("Switch?to?window:?"?+?windowTitle+?"?successfully!");break;}?else?{//如果當前循環到的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. 關掉處理完成的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. 需要等待頁面的某個元素加載完成再做后續操作?

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

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;}

第一個就是隱式的等待啦。第二種我自己隱式的實現了ExceptedCondition,它的apply方法應該會被回調。

ExceptedCondition提供了許多靜態的方法大家可以根據自己的需求來使用。

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

轉載于:https://my.oschina.net/athhu/blog/272401

總結

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

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。