Index: ssts-web/src/test/java/test/forgon/disinfectsystem/selenium/tools/Utils.java =================================================================== diff -u -r19497 -r19861 --- ssts-web/src/test/java/test/forgon/disinfectsystem/selenium/tools/Utils.java (.../Utils.java) (revision 19497) +++ ssts-web/src/test/java/test/forgon/disinfectsystem/selenium/tools/Utils.java (.../Utils.java) (revision 19861) @@ -71,6 +71,7 @@ * 测试用例的运行模式 */ private static RunMode testRunMode = RunMode.REMOTE; + // private static RunMode testRunMode = RunMode.LOCAL; /* @@ -327,8 +328,7 @@ } /** - * 重复查找,当出现引用的element过时后,重新查找该element - * 如果找不到元素,不会抛出异常 + * 重复查找,当出现引用的element过时后,重新查找该element 如果找不到元素,不会抛出异常 * * @param by * @return @@ -349,10 +349,9 @@ } return element; } - + /** - * 重复查找,当出现引用的element过时后,重新查找该element - * 如果找不到元素,会抛出异常 + * 重复查找,当出现引用的element过时后,重新查找该element 如果找不到元素,会抛出异常 * * @param by * @return @@ -369,7 +368,7 @@ } return element; } - + /** * 重复查找和执行动作(click或sendKeys等),当出现引用的element过时后,重新查找该element * @@ -559,8 +558,7 @@ } /** - * 查找组合框,输入按键。 - * 说明:之前输入按键后,会休眠1.5秒,等待下拉框的内容出现,保证可靠的输入值。 + * 查找组合框,输入按键。 说明:之前输入按键后,会休眠1.5秒,等待下拉框的内容出现,保证可靠的输入值。 * 后面采用了JSWaiter的waitUntilJQueryReady方法,取消了休眠。 * * @param by @@ -630,9 +628,9 @@ } }); if (isFind(element) == true) { - logger.info("元素"+element+"存在"); + logger.info("元素" + element + "存在"); } else { - logger.info("元素"+element+"不存在"); + logger.info("元素" + element + "不存在"); } return element; } @@ -712,15 +710,14 @@ JSWaiter.waitUntilJQueryReady(); Object response = null; int attempts = 0; - while (attempts < 5){ + while (attempts < 5) { // 通过Try Catch执行js语句,发生JavaScriptException等异常后重试 - try{ + try { response = ((JavascriptExecutor) Utils.driver) .executeScript(script); logger.info("执行脚本:" + script + " 返回值:" + response); break; - } - catch (Exception e){ + } catch (Exception e) { logger.info("第" + (attempts + 1) + "次出现异常:" + e.getMessage()); e.printStackTrace(); // 休眠1000ms后重试 @@ -931,8 +928,11 @@ /** * 获取表格的第一行的第index个单元格,只获取显示的单元格,会过滤掉不显示的单元格 - * @param table HTML的表格 - * @param index 第一行的第index个单元格,index从0开始计数 + * + * @param table + * HTML的表格 + * @param index + * 第一行的第index个单元格,index从0开始计数 * @return */ private static WebElement getIndexCellOfTable(WebElement table, int index) { @@ -944,17 +944,17 @@ WebElement row = allRows.get(0); // return row; List cells = row.findElements(By.tagName("td")); - + List cellsNotHidden = new ArrayList(); // 去掉隐藏的cell - for (WebElement myCell : cells){ + for (WebElement myCell : cells) { String style = myCell.getAttribute("style"); - if ( style == null || style.indexOf("none") == -1){ + if (style == null || style.indexOf("none") == -1) { cellsNotHidden.add(myCell); } } - - if (cellsNotHidden.size() >= index + 1 ) { + + if (cellsNotHidden.size() >= index + 1) { cell = cellsNotHidden.get(index); } } @@ -971,20 +971,24 @@ */ public static void waitForWindowAmountEquals(int expectedNumberOfWindows, int timeOut) { - - boolean isEquals = isWindowAmountEquals(expectedNumberOfWindows, timeOut); - - if (!isEquals){ + + boolean isEquals = isWindowAmountEquals(expectedNumberOfWindows, + timeOut); + + if (!isEquals) { int actualSize = driver.getWindowHandles().size(); throw new RuntimeException("没有达到期望的窗口数量,实际窗口数量 = " + actualSize + ",期望数量 = " + expectedNumberOfWindows); } } - + /** * 是否浏览器窗口的数量达到了期望值 - * @param expectedNumberOfWindows 期望的窗口数量 - * @param timeOut 等待的超时时间 + * + * @param expectedNumberOfWindows + * 期望的窗口数量 + * @param timeOut + * 等待的超时时间 */ public static boolean isWindowAmountEquals(int expectedNumberOfWindows, int timeOut) { @@ -1011,8 +1015,7 @@ } if (timeOut <= duration && !reachExpectedWindowAmount) { return false; - } - else{ + } else { return true; } } @@ -1098,24 +1101,31 @@ Utils.retryingFindAndClickByJavaScript(By.id("customIntoBasketDiv")); boolean isNewWindowOpened = Utils.isWindowAmountEquals(2, 8000); // 尝试用API的方式点击自定义入筐按钮 - if (!isNewWindowOpened){ + if (!isNewWindowOpened) { logger.info("通过JS点击,没有打开自定义入筐的窗口,尝试用API调用方式打开..."); - Utils.retryingFindAndClickBySeleniumAPI(By.id("customIntoBasketDiv")); + Utils.retryingFindAndClickBySeleniumAPI(By + .id("customIntoBasketDiv")); isNewWindowOpened = Utils.isWindowAmountEquals(2, 8000); - } - else{ + } else { logger.info("通过JS点击,打开了自定义入筐的窗口"); } - - if (isNewWindowOpened){ + + if (isNewWindowOpened) { logger.info("最终打开了自定义入筐的窗口!"); + } else { + logger.info("没有打开自定义入筐的窗口,再次尝试打开!"); + Utils.retryingFindAndClick(By.id("customIntoBasketDiv")); + isNewWindowOpened = Utils.isWindowAmountEquals(2, 8000); + if (isNewWindowOpened) { + logger.info("最终打开了自定义入筐的窗口!"); + } else { + logger.info("最终没有打开自定义入筐的窗口,测试失败!"); + + } } - else{ - logger.info("最终没有打开自定义入筐的窗口,测试失败!"); - } - + AssertJUnit.assertTrue(isNewWindowOpened); - + // 遍历所有窗口,切换至新窗口(自定义入筐窗口) Set handles = Utils.driver.getWindowHandles(); for (String windowHandle : handles) { @@ -1166,13 +1176,13 @@ */ public static void waitUntilUrlChange(final String newUrl, int seconds) { Wait wait = WaitFactory.createDefaultWait(); - + boolean isURLOk = wait.until(new Function() { public Boolean apply(WebDriver d) { return d.getCurrentUrl().startsWith(newUrl); } }); - if (!isURLOk){ + if (!isURLOk) { String currentURL = Utils.driver.getCurrentUrl(); throw new RuntimeException("URL地址没有跳转到期望地址,期望地址 = " + newUrl + ",当前地址 = " + currentURL); @@ -1198,12 +1208,12 @@ } }); - if (!isURLOk){ + if (!isURLOk) { String currentURL = Utils.driver.getCurrentUrl(); throw new RuntimeException("URL地址包含期望值,期望包含值 = " + urlContaints + ",当前地址 = " + currentURL); } - + } /** Index: ssts-web/src/test/java/test/forgon/disinfectsystem/selenium/Recyle.java =================================================================== diff -u -r19800 -r19861 --- ssts-web/src/test/java/test/forgon/disinfectsystem/selenium/Recyle.java (.../Recyle.java) (revision 19800) +++ ssts-web/src/test/java/test/forgon/disinfectsystem/selenium/Recyle.java (.../Recyle.java) (revision 19861) @@ -13,13 +13,11 @@ import org.apache.log4j.Logger; import org.openqa.selenium.Alert; import org.openqa.selenium.By; -import org.openqa.selenium.Keys; import org.openqa.selenium.WebDriver; import org.openqa.selenium.WebElement; import org.openqa.selenium.interactions.Actions; import org.openqa.selenium.os.WindowsUtils; import org.openqa.selenium.support.ui.ExpectedConditions; -import org.openqa.selenium.support.ui.Select; import org.openqa.selenium.support.ui.Wait; import org.testng.Assert; import org.testng.AssertJUnit; Index: ssts-web/src/test/java/test/forgon/disinfectsystem/selenium/Wash.java =================================================================== diff -u -r19800 -r19861 --- ssts-web/src/test/java/test/forgon/disinfectsystem/selenium/Wash.java (.../Wash.java) (revision 19800) +++ ssts-web/src/test/java/test/forgon/disinfectsystem/selenium/Wash.java (.../Wash.java) (revision 19861) @@ -36,7 +36,7 @@ Utils.driver.switchTo().window(winHandle); } - WebElement frame = Utils.retryingFindAndSwitchToFrame(By + Utils.retryingFindAndSwitchToFrame(By .id("iframe_washAndDisinfect")); Utils.retryingFindAndClick(By .xpath("//button[contains(text(), '添加')]")); @@ -439,7 +439,7 @@ Utils.driver.switchTo().window(winHandle); } Wait wait = WaitFactory.createDefaultWait(); - WebElement frame = Utils.retryingFindAndSwitchToFrame(By + Utils.retryingFindAndSwitchToFrame(By .id("iframe_washAndDisinfect")); Utils.retryingFindAndClick(By .xpath("//button[contains(text(), '添加')]"));