Index: ssts-web/src/test/java/test/forgon/disinfectsystem/selenium/tools/Utils.java =================================================================== diff -u -r19260 -r19261 --- ssts-web/src/test/java/test/forgon/disinfectsystem/selenium/tools/Utils.java (.../Utils.java) (revision 19260) +++ ssts-web/src/test/java/test/forgon/disinfectsystem/selenium/tools/Utils.java (.../Utils.java) (revision 19261) @@ -34,6 +34,7 @@ import org.openqa.selenium.support.ui.Wait; import org.openqa.selenium.support.ui.WebDriverWait; import org.testng.Assert; +import org.testng.AssertJUnit; import test.forgon.disinfectsystem.selenium.factory.WaitFactory; @@ -978,10 +979,27 @@ */ public static void waitForWindowAmountEquals(int expectedNumberOfWindows, int timeOut) { + + boolean isEquals = isWindowAmountEquals(expectedNumberOfWindows, timeOut); + + if (!isEquals){ + int actualSize = driver.getWindowHandles().size(); + throw new RuntimeException("没有达到期望的窗口数量,实际窗口数量 = " + actualSize + + ",期望数量 = " + expectedNumberOfWindows); + } + } + + /** + * 是否浏览器窗口的数量达到了期望值 + * @param expectedNumberOfWindows 期望的窗口数量 + * @param timeOut 等待的超时时间 + */ + public static boolean isWindowAmountEquals(int expectedNumberOfWindows, + int timeOut) { int initCount = driver.getWindowHandles().size(); int actualSize = initCount; if (initCount == expectedNumberOfWindows) { - return; + return true; } else { // 达到了期望的窗口数量 boolean reachExpectedWindowAmount = false; @@ -999,11 +1017,12 @@ } duration = (System.currentTimeMillis() - startTime); } - if (timeOut <= duration && !reachExpectedWindowAmount) { // Throw - // Error - throw new RuntimeException("没有达到期望的窗口数量,实际窗口数量 = " + actualSize - + ",期望数量 = " + expectedNumberOfWindows); + if (timeOut <= duration && !reachExpectedWindowAmount) { + return false; } + else{ + return true; + } } } @@ -1082,9 +1101,18 @@ try { // 回收窗口 String mainWindow = Utils.driver.getWindowHandle(); - // 自定义入筐的div按钮必须通过Javascript点击,通过API点击有时候不起作用 - Utils.retryingFindAndClickByJavaScript(By.id("customIntoBasketDiv")); - Utils.waitForWindowAmountEquals(2, 8000); + + // 通过API方式点击自定义入筐的div按钮(适用于Win7 IE11环境) + Utils.retryingFindAndClickBySeleniumAPI(By.id("customIntoBasketDiv")); + boolean isNewWindowOpened = Utils.isWindowAmountEquals(2, 8000); + // 尝试用JS的方式点击自定义入筐按钮 + if (!isNewWindowOpened){ + Utils.retryingFindAndClickByJavaScript(By.id("customIntoBasketDiv")); + isNewWindowOpened = Utils.isWindowAmountEquals(2, 8000); + } + + AssertJUnit.assertTrue(isNewWindowOpened); + // 遍历所有窗口,切换至新窗口(自定义入筐窗口) Set handles = Utils.driver.getWindowHandles(); for (String windowHandle : handles) {