書接上回,最近項目裏邊新的release須要move to uat。而後我很光榮的被委派去給tester執行自動化測試腳本作support,讓我極度遺憾的是tester不是妹子,表示本絲註定單身孤獨終老的命啊。java
好吧不扯淡了,在測試的過程當中碰到幾個問題致使程序不夠穩定,腳本也被噴不夠robust,我本身也噴page object模式就是shit,維護的人會shi的很難看。app
1. 處理popup window問題處理的不夠好?ide
a. 切換到新彈出的window測試
public boolean switchToWindowAttach(WebDriver driver, String windowTitle,String frameId) { boolean flag = false; try { //記下當前window String 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則切換回最初window driver.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
spa
/** * 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 close if (theTitle.contains(title)) { driver.close(); } //switch back to the original window if (!handle.equalsIgnoreCase(orginalHandle)) { driver.switchTo().window(orginalHandle); } } }
2. 須要等待頁面的某個元素加載完成再作後續操做?code
Selenium提供了2個等待的操做,一種是隱式的,另外一種,er,也不知道是否是叫現實的orm
a.ci
public void waitForElementLoading(Long millis) { driver.manage().timeouts().implicitlyWait(millis, TimeUnit.MILLISECONDS); }
b.
get
public WebElement waitForElementByLocator(final By locator, Long timeOut) { if (timeOut == null) { timeOut = 60L; } WebElement id = (new WebDriverWait(getDriver(), timeOut)) .until(new ExpectedCondition<WebElement>() { @Override public WebElement apply(WebDriver d) { return d.findElement(locator); } }); return id; }
第一個就是隱式的等待啦。第二種我本身隱式的實現了ExceptedCondition,它的apply方法應該會被回調。iframe
ExceptedCondition提供了許多靜態的方法你們能夠根據本身的需求來使用。
這兩種等待的區別和具體使用須要本絲研究一把,下次給你們介紹哈。額,表噴我,這準備不足真心講很差。