Index: ssts-selenium-test/src/test/java/test_4_1/Apply.java =================================================================== diff -u -r17716 -r17741 --- ssts-selenium-test/src/test/java/test_4_1/Apply.java (.../Apply.java) (revision 17716) +++ ssts-selenium-test/src/test/java/test_4_1/Apply.java (.../Apply.java) (revision 17741) @@ -459,15 +459,18 @@ * js.executeScript(enterEdit); Actions action = new * Actions(Utils.driver); action.sendKeys("2" + "\n").perform(); **/ - Utils.retryingFindAndSendKeys(By + // 输入物品名称,package1为名称输入框 + Utils.retryingFindComboBoxAndSendKeys(By .cssSelector("input[name='package1']"), Utils.settings .getJSONObject("一级供应室敷料包").getString("名称")); Utils.retryingFindAndSendKeys( By.cssSelector("input[name='package1']"), "\n"); + + Utils.retryingFindAndSendKeys( By.cssSelector("input[name='count1']"), "2" + "\n"); - Utils.retryingFindAndSendKeys(By + Utils.retryingFindComboBoxAndSendKeys(By .cssSelector("input[name='package1']"), Utils.settings .getJSONObject("一级供应室器械包3").getString("名称")); @@ -530,7 +533,7 @@ .xpath("//div/ul/li[contains(text(), '" + SupplyRoomName + "')]")); - Utils.retryingFindAndSendKeys(By.id("package1-inputEl"), + Utils.retryingFindComboBoxAndSendKeys(By.id("package1-inputEl"), Utils.settings.getJSONObject("一级供应室消毒物品").getString("名称")); // 休眠,等待下拉框出现 @@ -539,7 +542,7 @@ Utils.retryingFindAndSendKeys(By.id("package1-inputEl"), "\n"); Utils.retryingFindAndSendKeys(By.id("count1-inputEl"), "2" + "\n"); - Utils.retryingFindAndSendKeys(By.id("package1-inputEl"), + Utils.retryingFindComboBoxAndSendKeys(By.id("package1-inputEl"), Utils.settings.getJSONObject("一级供应室消毒物品1").getString("名称")); Utils.retryingFindAndSendKeys(By.id("package1-inputEl"), "\n"); @@ -577,7 +580,7 @@ .xpath("//div/ul/li[contains(text(), '" + SupplyRoomName + "')]")); - Utils.retryingFindAndSendKeys(By + Utils.retryingFindComboBoxAndSendKeys(By .cssSelector("input[name='package1']"), Utils.settings .getJSONObject("一级供应室一次性物品").getString("名称")); @@ -713,7 +716,7 @@ Utils.retryingFindAndClick(By .xpath("//div/div/div[contains(text(), '" + SupplyRoomName + "')]")); - Utils.retryingFindAndSendKeys( + Utils.retryingFindComboBoxAndSendKeys( By.cssSelector("input[name='leaseTousseName']"), Utils.settings.getJSONObject("一级供应室器械包2").getString("名称")); Utils.retryingFindAndSendKeys(By.id("applicantAmount"), "2"); Index: ssts-selenium-test/src/test/java/test_4_1/tools/Utils.java =================================================================== diff -u -r17739 -r17741 --- ssts-selenium-test/src/test/java/test_4_1/tools/Utils.java (.../Utils.java) (revision 17739) +++ ssts-selenium-test/src/test/java/test_4_1/tools/Utils.java (.../Utils.java) (revision 17741) @@ -2,7 +2,6 @@ import java.io.IOException; import java.io.InputStream; -import java.net.MalformedURLException; import java.net.URL; import java.text.SimpleDateFormat; import java.util.ArrayList; @@ -82,7 +81,7 @@ InternetExplorerDriver.INTRODUCE_FLAKINESS_BY_IGNORING_SECURITY_DOMAINS, true); - ieCapabilities.setCapability("applicationName", "wangyi"); + ieCapabilities.setCapability("applicationName", "terry"); ieCapabilities.setCapability("version", "11.0"); // driver = new InternetExplorerDriver(ieCapabilities); @@ -170,22 +169,13 @@ public static WebElement retryingFind(By by) { WebElement element = null; int attempts = 0; - while (attempts < 5) { + while (attempts < 3) { try { - Wait wait = new FluentWait(Utils.driver) - .withTimeout(5000, TimeUnit.MILLISECONDS) - .pollingEvery(250, TimeUnit.MILLISECONDS) - .ignoring(NoSuchElementException.class) - .ignoring(StaleElementReferenceException.class); - element = wait - .until(new Function() { - public WebElement apply(WebDriver driver) { - return driver.findElement(by); - } - }); -// driver.findElement(by); Thread.sleep(1000); + + element = fluentWaitUntilFind(by); + break; } catch (Exception e) { } @@ -203,26 +193,10 @@ public static WebElement retryingFindAndClick(By by) { WebElement element = null; int attempts = 0; - while (attempts < 5) { + while (attempts < 3) { try { - // Collection types = new HashSet(); - // types.add(NoSuchElementException.class); - // types.add(StaleElementReferenceException.class); - Wait wait = new FluentWait(Utils.driver) - .withTimeout(5000, TimeUnit.MILLISECONDS) - .pollingEvery(250, TimeUnit.MILLISECONDS) - .ignoring(NoSuchElementException.class) - .ignoring(StaleElementReferenceException.class); - element = wait - .until(new Function() { - public WebElement apply(WebDriver driver) { - return driver.findElement(by); - } - }); -// driver.findElement(by); -// Thread.sleep(1000); - + element = fluentWaitUntilFind(by); element.click(); break; } catch (Exception e) { @@ -235,26 +209,10 @@ public static WebElement retryingFindAndSendKeys(By by, java.lang.CharSequence... keys) { WebElement element = null; int attempts = 0; - while (attempts < 5) { + while (attempts < 3) { try { - // Collection types = new HashSet(); - // types.add(NoSuchElementException.class); - // types.add(StaleElementReferenceException.class); - Wait wait = new FluentWait(Utils.driver) - .withTimeout(5000, TimeUnit.MILLISECONDS) - .pollingEvery(250, TimeUnit.MILLISECONDS) - .ignoring(NoSuchElementException.class) - .ignoring(StaleElementReferenceException.class); - element = wait - .until(new Function() { - public WebElement apply(WebDriver driver) { - return driver.findElement(by); - } - }); -// driver.findElement(by); - Thread.sleep(1000); - + element = fluentWaitUntilFind(by); element.sendKeys(keys); break; } catch (Exception e) { @@ -266,24 +224,40 @@ return element; } + /** + * 查找组合框,输入按键。输入按键后休眠1.5秒,等待下拉框的内容出现,保证可靠的输入值。 + * TODO:如果找到更可靠输入组合框内容的方法,可以取消休眠。如果需要更可靠的输入,可以延长休眠时间 + * @param by + * @param keys + * @return + */ + public static WebElement retryingFindComboBoxAndSendKeys(By by, java.lang.CharSequence... keys) { + WebElement element = null; + int attempts = 0; + while (attempts < 3) { + try { + + element = fluentWaitUntilFind(by); + element.sendKeys(keys); + Thread.sleep(1500); + break; + } catch (Exception e) { +// System.out.println("hkkkkkk"); +// e.printStackTrace(); + } + attempts++; + } + return element; + } + + public static WebElement retryingFindAndSwitchToFrame(By by) { WebElement element = null; int attempts = 0; - while (attempts < 5) { + while (attempts < 3) { try { - Wait wait = new FluentWait(Utils.driver) - .withTimeout(5000, TimeUnit.MILLISECONDS) - .pollingEvery(250, TimeUnit.MILLISECONDS) - .ignoring(NoSuchElementException.class) - .ignoring(StaleElementReferenceException.class) - .ignoring(NullPointerException.class); - element = wait - .until(new Function() { - public WebElement apply(WebDriver driver) { - return driver.findElement(by); - } - }); + element = fluentWaitUntilFind(by); Utils.driver.switchTo().frame(element); break; @@ -293,6 +267,29 @@ } return element; } + + /** + * 流畅等待,查找页面元素 + * + * @param by + * @return + */ + private static WebElement fluentWaitUntilFind(By by) { + WebElement element; + Wait wait = new FluentWait(Utils.driver) + .withTimeout(5000, TimeUnit.MILLISECONDS) + .pollingEvery(250, TimeUnit.MILLISECONDS) + .ignoring(NoSuchElementException.class) + .ignoring(StaleElementReferenceException.class) + .ignoring(NullPointerException.class); + element = wait + .until(new Function() { + public WebElement apply(WebDriver driver) { + return driver.findElement(by); + } + }); + return element; + } /** * 重复查找和click,html表格的第一个单元格对象 Index: ssts-selenium-test/src/test/java/test_4_1/ALL.java =================================================================== diff -u -r17716 -r17741 --- ssts-selenium-test/src/test/java/test_4_1/ALL.java (.../ALL.java) (revision 17716) +++ ssts-selenium-test/src/test/java/test_4_1/ALL.java (.../ALL.java) (revision 17741) @@ -46,7 +46,10 @@ /** 申请一次性物品 */ Utils.retryingFindAndClick(By.id("diposableGoodsApplication")); apply.apply_gdsy_oneuse(); - Utils.driver.switchTo().frame(frame); + + Utils.retryingFindAndSwitchToFrame(By + .id("iframe_recyclingApplicationVersion2")); + /** 申请消毒物品 */ Utils.retryingFindAndClick(By.id("disinfectGoodsApplication")); apply.apply_gdsy_sterilize();