Index: ssts-selenium-test/src/test/java/test_4_1/tools/Utils.java =================================================================== diff -u -r17922 -r17947 --- ssts-selenium-test/src/test/java/test_4_1/tools/Utils.java (.../Utils.java) (revision 17922) +++ ssts-selenium-test/src/test/java/test_4_1/tools/Utils.java (.../Utils.java) (revision 17947) @@ -20,6 +20,7 @@ import org.apache.log4j.Logger; import org.openqa.selenium.By; import org.openqa.selenium.JavascriptExecutor; +import org.openqa.selenium.Keys; import org.openqa.selenium.StaleElementReferenceException; import org.openqa.selenium.WebDriver; import org.openqa.selenium.WebElement; @@ -29,12 +30,14 @@ import org.openqa.selenium.remote.DesiredCapabilities; import org.openqa.selenium.remote.RemoteWebDriver; import org.openqa.selenium.support.ui.ExpectedCondition; +import org.openqa.selenium.support.ui.ExpectedConditions; import org.openqa.selenium.support.ui.FluentWait; import org.openqa.selenium.support.ui.Wait; import org.openqa.selenium.support.ui.WebDriverWait; import com.fasterxml.jackson.core.JsonParser.Feature; import com.fasterxml.jackson.databind.ObjectMapper; + import org.testng.Assert; /** @@ -60,13 +63,15 @@ /** * 全局等待的设置 + * WebDriverWait不能用全局变量,Terry Kwan,2017-5-1 */ - public static WebDriverWait waitSetting; +// public static WebDriverWait waitSetting; /** * extJs confirm窗口的等待时间 + * WebDriverWait不能用全局变量,Terry Kwan,2017-5-1 */ - public static WebDriverWait confirmWaitSetting; +// public static WebDriverWait confirmWaitSetting; // public static Wait waitSetting; /** * 单次装配后产生的条码 @@ -117,9 +122,9 @@ */ public static void setWaitSetting(WebDriver driver) { // 最长等待15秒,每1秒轮询一次 - waitSetting = new WebDriverWait(driver, 8, 350); + WebDriverWait waitSetting = new WebDriverWait(driver, 8, 350); // 最长等待2秒,每0.3秒轮询一次 - confirmWaitSetting = new WebDriverWait(driver, 2, 300); + WebDriverWait confirmWaitSetting = new WebDriverWait(driver, 2, 300); // waitSetting = new FluentWait<>(driver).withTimeout(5000, // TimeUnit.MILLISECONDS).pollingEvery(250, TimeUnit.MILLISECONDS); } @@ -145,19 +150,24 @@ // 解决ie兼容模式设置导致无法启动问题 DesiredCapabilities ieCapabilities = DesiredCapabilities .internetExplorer(); + // 禁用IE的native Events,解决偶尔有时候页面元素的click点击不生效的问题 + ieCapabilities.setCapability(InternetExplorerDriver.NATIVE_EVENTS, false); + ieCapabilities .setCapability( InternetExplorerDriver.INTRODUCE_FLAKINESS_BY_IGNORING_SECURITY_DOMAINS, true); + ieCapabilities.setCapability("applicationName", "terry"); ieCapabilities.setCapability("version", "11.0"); try { // driver = new RemoteWebDriver(new URL( // "http://192.168.2.18:4444/wd/hub"), ieCapabilities); driver = new InternetExplorerDriver(ieCapabilities); - + String cap = ((InternetExplorerDriver)driver).getCapabilities().toString(); + System.out.println("my cap = " + cap); } catch (Exception e) { e.printStackTrace(); } @@ -305,9 +315,35 @@ // 将鼠标移动到html元素,然后click,似乎能增强click的成功率 // actions.moveToElement(element).click().build().perform(); - - element.click(); - logger.info("点击元素[" + by + "]成功"); +// try{ +// WebDriverWait wait = new WebDriverWait(driver, 15); +// element = wait.until(ExpectedConditions.elementToBeClickable(by)); +// element.click(); +// } +// catch(Exception e){ +// System.out.println("click() 发生异常:"); +// e.printStackTrace(); +// } + +// String onclick = element.getAttribute("onclick"); +// System.out.println("onclick = " + onclick); + + JavascriptExecutor executor = (JavascriptExecutor) driver; + Object aa=executor.executeScript("var items = {}; for (index = 0; index < arguments[0].attributes.length; ++index) { items[arguments[0].attributes[index].name] = arguments[0].attributes[index].value }; return items;", element); + System.out.println("click element attributes:\n" + aa); + String attribute = element.getAttribute("disabled"); + System.out.println("disabled = " + attribute); + if (!"true".equalsIgnoreCase(attribute) && element.isEnabled()){ + element.click(); + logger.info("点击元素[" + by + "]成功"); + } + else{ + logger.info("元素[" + by + "]不是Enable状态,不能点击"); + continue; + } + // 用按回车键模拟click,当element在屏幕上不可见时,也能达到click的效果 +// element.sendKeys(Keys.ENTER); + } else if (ACTION_SENDKEYS.equalsIgnoreCase(actionType)) { element.sendKeys(keys); } else if (ACTION_CLEAR.equalsIgnoreCase(actionType)) { @@ -324,12 +360,17 @@ } catch (Exception e) { if (attempts == attemptsTime) { + String message = e.getMessage(); logger.error(String.format("执行%s时尝试%s次仍然发生错误:%s", by, - attemptsTime, e.getMessage())); - throw new RuntimeException(e); + attemptsTime, message)); + if ("null".equals(message)){ + e.printStackTrace(); + } + throw new RuntimeException(message); } logger.warn(String.format("执行动作:%s操作节点%s时,发生错误,重试第%s次,异常如下:%s", actionType, by, attempts, e)); + e.printStackTrace(); } } return element; @@ -447,11 +488,13 @@ // element = wait // 暂时改成waitSetting,如果有必要,可以每次new一个新的. + waitPageLoaded(); + WebDriverWait waitSetting = new WebDriverWait(driver, 8, 350); WebElement element = waitSetting .until(new Function() { public WebElement apply(WebDriver driver) { // 等待页面状态加载完成 - waitPageLoaded(); +// waitPageLoaded(); return driver.findElement(by); } @@ -465,6 +508,7 @@ * @see Utils#findByAwait(By, WebDriverWait) */ public static WebElement findByAwait(By by) { + WebDriverWait waitSetting = new WebDriverWait(driver, 8, 350); return findByAwait(by, waitSetting); } @@ -509,6 +553,7 @@ */ public static WebElement tryFind(By by) { try { + WebDriverWait confirmWaitSetting = new WebDriverWait(driver, 2, 300); return findByAwait(by, confirmWaitSetting); } catch (Exception ignored) { logger.warn(String.format("尝试查找%s失败", by)); @@ -581,8 +626,12 @@ * 要运行的js脚本 */ public static Object runJavaScript(String script) { - logger.info("执行脚本:" + script); - return ((JavascriptExecutor) Utils.driver).executeScript(script); + + + Object response = ((JavascriptExecutor) Utils.driver).executeScript(script); + logger.info("执行脚本:" + script + " 返回值:" + response); + + return response; } public static Object runJavaScript(String script, Object... args) { @@ -593,6 +642,7 @@ * 等待页面加载完成 */ public static void waitPageLoaded() { + WebDriverWait waitSetting = new WebDriverWait(driver, 8, 350); waitSetting .until((ExpectedCondition) driver -> Utils .runJavaScript("return document.readyState").equals( Index: ssts-selenium-test/src/test/java/test_4_1/apply/helper/ApplyHelper.java =================================================================== diff -u -r17920 -r17947 --- ssts-selenium-test/src/test/java/test_4_1/apply/helper/ApplyHelper.java (.../ApplyHelper.java) (revision 17920) +++ ssts-selenium-test/src/test/java/test_4_1/apply/helper/ApplyHelper.java (.../ApplyHelper.java) (revision 17947) @@ -6,8 +6,10 @@ import org.openqa.selenium.WebElement; import org.openqa.selenium.interactions.Actions; import org.openqa.selenium.support.ui.ExpectedConditions; +import org.openqa.selenium.support.ui.WebDriverWait; import org.slf4j.Logger; import org.slf4j.LoggerFactory; + import test_4_1.Constants; import test_4_1.SettingConstants; import test_4_1.apply.applyConstants.ApplyEnum; @@ -43,7 +45,8 @@ // Utils.findByAwaitAndSend(By.cssSelector("input[id='handleDepart-inputEl']"), Keys.DOWN); Utils.findByAwaitAndClick(By.xpath("//div/div/ul/li[contains(text(), '" + handleRoomName + "')]")); // 断言处理科室填写成功 - Utils.waitSetting.until(ExpectedConditions.attributeContains(By.id("handleDepart-inputEl"), "value", handleRoomName)); + WebDriverWait waitSetting = new WebDriverWait(Utils.driver, 8, 350); + waitSetting.until(ExpectedConditions.attributeContains(By.id("handleDepart-inputEl"), "value", handleRoomName)); } @@ -246,7 +249,9 @@ // throw new RuntimeException("提交失败!"); // } //这里等待提交完成暂时改成用until方式,上面的方法好像不太稳定 - if (!Utils.waitSetting.until(ExpectedConditions.textToBePresentInElementLocated(By.id("extTipMsg"), "成功"))) { + WebDriverWait waitSetting = new WebDriverWait(Utils.driver, 8, 350); + + if (!waitSetting.until(ExpectedConditions.textToBePresentInElementLocated(By.id("extTipMsg"), "成功"))) { throw new RuntimeException("提交失败!"); } }