Index: ssts-selenium-test/src/test/java/test_4_1/tools/Utils.java =================================================================== diff -u -r17894 -r17904 --- ssts-selenium-test/src/test/java/test_4_1/tools/Utils.java (.../Utils.java) (revision 17894) +++ ssts-selenium-test/src/test/java/test_4_1/tools/Utils.java (.../Utils.java) (revision 17904) @@ -16,7 +16,6 @@ import net.sf.json.JSONObject; import org.apache.commons.lang3.BooleanUtils; -import org.junit.Assert; import org.openqa.selenium.By; import org.openqa.selenium.JavascriptExecutor; import org.openqa.selenium.StaleElementReferenceException; @@ -36,6 +35,7 @@ import com.fasterxml.jackson.core.JsonParser.Feature; import com.fasterxml.jackson.databind.ObjectMapper; +import org.testng.Assert; /** @@ -251,18 +251,21 @@ public static WebElement retryingFindAndDoAction(By by , String actionType , CharSequence... keys) { WebElement element = null; int attempts = 0; - while (attempts < 3) { + int attemptsTime = 3; + while (attempts++ < attemptsTime) { try { + // element = findByAwait(by); element = fluentWaitUntilFind(by); - + if (ACTION_CLICK.equalsIgnoreCase(actionType)){ // Actions actions = new Actions(driver); // 将鼠标移动到html元素,然后click,似乎能增强click的成功率 // actions.moveToElement(element).click().build().perform(); element.click(); + logger.info("点击元素{}-成功", element); } else if (ACTION_SENDKEYS.equalsIgnoreCase(actionType)){ element.sendKeys(keys); @@ -275,6 +278,7 @@ // Utils.driver.switchTo().defaultContent(); Utils.driver.switchTo().frame(element); + logger.info("切换{},frame成功", element); } //等待页面状态加载完成 @@ -283,8 +287,12 @@ // js.executeScript("return document.readyState").equals("complete"); break; } catch (Exception e) { + if (attempts == attemptsTime){ + logger.warn("执行{}时尝试{}次仍然发生错误:{}", by, attemptsTime, e.getMessage()); + throw new RuntimeException(e); + } + logger.info("重试"); } - attempts++; } return element; } @@ -387,16 +395,17 @@ * @return */ public static WebElement fluentWaitUntilFind(By by) { - WebElement element; + + // Wait wait = new FluentWait(driver) + // .withTimeout(5000, TimeUnit.MILLISECONDS) + // .pollingEvery(250, TimeUnit.MILLISECONDS) + // .ignoring(NoSuchElementException.class) + // .ignoring(StaleElementReferenceException.class) + // .ignoring(NullPointerException.class); - Wait wait = new FluentWait(driver) - .withTimeout(5000, TimeUnit.MILLISECONDS) - .pollingEvery(250, TimeUnit.MILLISECONDS) - .ignoring(NoSuchElementException.class) - .ignoring(StaleElementReferenceException.class) - .ignoring(NullPointerException.class); - - element = wait + // element = wait + //暂时改成waitSetting,如果有必要,可以每次new一个新的 + WebElement element = waitSetting .until(new Function() { public WebElement apply(WebDriver driver) { //等待页面状态加载完成 @@ -424,20 +433,13 @@ * @return 查找到的节点,如果在指定的时间找不到则不会返回,直接抛出异常 */ public static WebElement findByAwait(By by, WebDriverWait wait) { - final WebElement[] webElement = {null}; try { //这里找不到会抛出异常 - if (wait.until((ExpectedCondition) driver -> { - webElement[0] = driver.findElement(by); - return webElement[0] != null && webElement[0].isDisplayed(); - })) { - // webElement[0] = driver.findElement(by); - } + return wait.until((ExpectedCondition) driver -> driver.findElement(by)); } catch (Exception e) { throw new NoSuchElementException(String.format("找不到对应的元素:%s,原因如下:\n%s", by, e)); } - return webElement[0]; } @@ -447,8 +449,11 @@ public static WebElement findConfirmAndClick(By by) { WebElement webElement = null; try { - webElement = findByAwait(by, confirmWaitSetting); - webElement.click(); + webElement = tryFind(by); + if (isFind(webElement)){ + webElement.click(); + } + // webElement = findByAwait(by, confirmWaitSetting); } catch (Exception ignored) { } @@ -464,6 +469,7 @@ return findByAwait(by, confirmWaitSetting); } catch (Exception ignored) { + logger.warn("尝试查找{},失败", by); } return null; } @@ -485,52 +491,39 @@ */ public static WebElement findByAwaitAndSend(By by, java.lang.CharSequence... keys) { //先清空文本框上面的然后再输入 -// WebElement webElement = findByAwaitAndClear(by); - + // WebElement webElement = findByAwaitAndClear(by); + // WebElement webElement = findByAwait(by); + // webElement.clear(); retryingFindAndClear(by); return retryingFindAndSendKeys(by, keys); -// webElement.sendKeys(keys); -// return webElement; + // webElement.sendKeys(keys); + // return webElement; } /** * 点击页面元素 */ public static WebElement findByAwaitAndClick(By by) { - return retryingFindAndClick(by); -// -// WebElement webElement = findByAwait(by); -// // String javascript = "var evt = document.createEvent('MouseEvents');" + "evt.initMouseEvent('click',true, true, window, 0, 0, 0, 0, 0, false, false, false, false, 0,null);" + "arguments[0].dispatchEvent(evt);"; -// // runJavaScript(javascript, webElement); -// webElement.click(); -// return webElement; + // WebElement webElement = waitSetting.until(ExpectedConditions.elementToBeClickable(by)); + // String javascript = "var evt = document.createEvent('MouseEvents');" + "evt.initMouseEvent('click',true, true, window, 0, 0, 0, 0, 0, false, false, false, false, 0,null);" + "arguments[0].dispatchEvent(evt);"; + // runJavaScript(javascript, webElement); + // WebElement webElement = findByAwait(by); + // webElement.click(); + // return webElement; } - /** - * 清除页面的元素,例如文本框 - */ - public static WebElement findByAwaitAndClear(By by) { - - return retryingFindAndClear(by); - -// WebElement webElement = findByAwait(by); -// webElement.clear(); -// return webElement; - } /** * 切换到对应的frame */ public static WebElement findByAwaitAndSwitchToFrame(By by) { - return retryingFindAndSwitchToFrame(by); - -// Utils.driver.switchTo().defaultContent(); -// WebElement webElement = findByAwait(by); -// Utils.driver.switchTo().frame(webElement); -// return webElement; + // Utils.driver.switchTo().defaultContent(); + // WebElement webElement = findByAwait(by); + // Utils.driver.switchTo().frame(webElement); + // return webElement; }