Index: ssts-selenium-test/src/test/java/test_4_1/tools/Utils.java =================================================================== diff -u -r17869 -r17889 --- ssts-selenium-test/src/test/java/test_4_1/tools/Utils.java (.../Utils.java) (revision 17869) +++ ssts-selenium-test/src/test/java/test_4_1/tools/Utils.java (.../Utils.java) (revision 17889) @@ -2,7 +2,6 @@ import java.io.IOException; import java.io.InputStream; -import java.net.URL; import java.text.SimpleDateFormat; import java.util.ArrayList; import java.util.Date; @@ -30,22 +29,30 @@ import org.openqa.selenium.support.ui.ExpectedCondition; import org.openqa.selenium.support.ui.FluentWait; import org.openqa.selenium.support.ui.Wait; - -import com.fasterxml.jackson.core.JsonParser.Feature; -import com.fasterxml.jackson.databind.ObjectMapper; import org.openqa.selenium.support.ui.WebDriverWait; import org.slf4j.Logger; import org.slf4j.LoggerFactory; -import test_4_1.apply.helper.ApplyHelper; +import com.fasterxml.jackson.core.JsonParser.Feature; +import com.fasterxml.jackson.databind.ObjectMapper; + /** * @author Terry Date : 2016-02-29 23:17 工具类 */ public class Utils { private final static Logger logger = LoggerFactory.getLogger(Utils.class); + + + public static final String ACTION_CLICK = "click"; + public static final String ACTION_SENDKEYS = "sendKeys"; + public static final String ACTION_SWITCH_TO_FRAME = "switchToFrame"; + public static final String ACTION_CLEAR = "clear"; + + public static Wait wait; + /** * 项目设置对象 */ @@ -133,6 +140,12 @@ try { // driver = new RemoteWebDriver(new URL( "http://192.168.2.18:4444/wd/hub"), ieCapabilities); driver = new InternetExplorerDriver(ieCapabilities); + + wait = new FluentWait(driver) + .withTimeout(5000, TimeUnit.MILLISECONDS) + .pollingEvery(250, TimeUnit.MILLISECONDS) + .ignoring(NoSuchElementException.class) + .ignoring(StaleElementReferenceException.class); } catch (Exception e) { e.printStackTrace(); @@ -213,11 +226,10 @@ public static WebElement retryingFind(By by) { WebElement element = null; int attempts = 0; - while (attempts < 5) { + // TODO:下面的循环重试应该可以去掉,直接用fluentWaitUntilFind即可 + while (attempts < 1) { try { - Thread.sleep(1000); - element = fluentWaitUntilFind(by); break; @@ -227,24 +239,41 @@ } return element; } - + /** * 重复查找和click,当出现引用的element过时后,重新查找该element * * @param by * @return */ - public static WebElement retryingFindAndClick(By by) { + public static WebElement retryingFindAndDoAction(By by , String actionType , CharSequence... keys) { WebElement element = null; int attempts = 0; while (attempts < 5) { try { element = fluentWaitUntilFind(by); - element.click(); + + if (ACTION_CLICK.equalsIgnoreCase(actionType)){ + element.click(); + } + else if (ACTION_SENDKEYS.equalsIgnoreCase(actionType)){ + element.sendKeys(keys); + } + else if (ACTION_CLEAR.equalsIgnoreCase(actionType)){ + element.clear(); + } + else if (ACTION_SWITCH_TO_FRAME.equalsIgnoreCase(actionType)){ + // 跳出所有的frame。调用后:you are now outside both frames +// Utils.driver.switchTo().defaultContent(); + + Utils.driver.switchTo().frame(element); + } + //等待页面状态加载完成 - JavascriptExecutor js=(JavascriptExecutor)driver; - js.executeScript("return document.readyState").equals("complete"); + //TODO:以下代码应该无效果,待确认后删除 +// JavascriptExecutor js=(JavascriptExecutor)driver; +// js.executeScript("return document.readyState").equals("complete"); break; } catch (Exception e) { } @@ -253,25 +282,29 @@ return element; } - public static WebElement retryingFindAndSendKeys(By by, java.lang.CharSequence... keys) { - WebElement element = null; - int attempts = 0; - while (attempts < 5) { - try { + /** + * 重复查找和click,当出现引用的element过时后,重新查找该element + * + * @param by + * @return + */ + public static WebElement retryingFindAndClick(By by) { + + return retryingFindAndDoAction(by, ACTION_CLICK); + } + + /** + * 重复查找和清空内容,当出现引用的element过时后,重新查找该element + * + * @param by + * @return + */ + public static WebElement retryingFindAndClear(By by) { + return retryingFindAndDoAction(by, ACTION_CLEAR); + } - element = fluentWaitUntilFind(by); - element.sendKeys(keys); - //等待页面状态加载完成 - JavascriptExecutor js=(JavascriptExecutor)driver; - js.executeScript("return document.readyState").equals("complete"); - break; - } catch (Exception e) { -// System.out.println("hkkkkkk"); -// e.printStackTrace(); - } - attempts++; - } - return element; + public static WebElement retryingFindAndSendKeys(By by, java.lang.CharSequence... keys) { + return retryingFindAndDoAction(by, ACTION_SENDKEYS, keys); } /** @@ -291,8 +324,8 @@ element.sendKeys(keys); Thread.sleep(1500); //等待页面状态加载完成 - JavascriptExecutor js=(JavascriptExecutor)driver; - js.executeScript("return document.readyState").equals("complete"); +// JavascriptExecutor js=(JavascriptExecutor)driver; +// js.executeScript("return document.readyState").equals("complete"); break; } catch (Exception e) { // System.out.println("hkkkkkk"); @@ -309,26 +342,7 @@ * @return */ public static WebElement retryingFindAndSwitchToFrame(By by) { - WebElement element = null; - int attempts = 0; - while (attempts < 5) { - try { - - // 跳出所有的frame。调用后:you are now outside both frames -// Utils.driver.switchTo().defaultContent(); - - element = fluentWaitUntilFind(by); - - Utils.driver.switchTo().frame(element); - //等待页面状态加载完成 - JavascriptExecutor js=(JavascriptExecutor)driver; - js.executeScript("return document.readyState").equals("complete"); - break; - } catch (Exception e) { - } - attempts++; - } - return element; + return retryingFindAndDoAction(by, ACTION_SWITCH_TO_FRAME); } /** @@ -349,8 +363,8 @@ .until(new Function() { public WebElement apply(WebDriver driver) { //等待页面状态加载完成 - JavascriptExecutor js=(JavascriptExecutor)driver; - js.executeScript("return document.readyState").equals("complete"); +// JavascriptExecutor js=(JavascriptExecutor)driver; +// js.executeScript("return document.readyState").equals("complete"); return driver.findElement(by); } }); @@ -434,39 +448,52 @@ */ public static WebElement findByAwaitAndSend(By by, java.lang.CharSequence... keys) { //先清空文本框上面的然后再输入 - WebElement webElement = findByAwaitAndClear(by); - webElement.sendKeys(keys); - return webElement; +// WebElement webElement = findByAwaitAndClear(by); + + retryingFindAndClear(by); + return retryingFindAndSendKeys(by, keys); + +// webElement.sendKeys(keys); +// return webElement; } /** * 点击页面元素 */ public static WebElement findByAwaitAndClick(By 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; + + 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; } /** * 清除页面的元素,例如文本框 */ public static WebElement findByAwaitAndClear(By by) { - WebElement webElement = findByAwait(by); - webElement.clear(); - return webElement; + + return retryingFindAndClear(by); + +// WebElement webElement = findByAwait(by); +// webElement.clear(); +// return webElement; } /** * 切换到对应的frame */ public static WebElement findByAwaitAndSwitchToFrame(By by) { - Utils.driver.switchTo().defaultContent(); - WebElement webElement = findByAwait(by); - Utils.driver.switchTo().frame(webElement); - return webElement; + + return retryingFindAndSwitchToFrame(by); + +// Utils.driver.switchTo().defaultContent(); +// WebElement webElement = findByAwait(by); +// Utils.driver.switchTo().frame(webElement); +// return webElement; }