Index: ssts-selenium-test/src/test/java/test_4_1/factory/WaitFactory.java =================================================================== diff -u --- ssts-selenium-test/src/test/java/test_4_1/factory/WaitFactory.java (revision 0) +++ ssts-selenium-test/src/test/java/test_4_1/factory/WaitFactory.java (revision 18181) @@ -0,0 +1,43 @@ +package test_4_1.factory; + +import org.openqa.selenium.StaleElementReferenceException; +import org.openqa.selenium.WebDriver; +import org.openqa.selenium.support.ui.FluentWait; +import org.openqa.selenium.support.ui.Wait; +import test_4_1.tools.Utils; + +import java.util.NoSuchElementException; +import java.util.concurrent.TimeUnit; + +/** + * @author zhonghaowen + * @apiNote wait工厂 + * @since 2017-05-19 + */ +public class WaitFactory { + + + /** + * 默认的wait对象 + * + * @return {@link Wait} + */ + public static Wait createDefaultWait() { + return createWait(5000, 250); + } + + /** + * 创建wai对象 + * + * @param withTimeout 超时的时间 + * @param pollingEvery 轮询的间隔 + * @return {@link Wait} + */ + private static Wait createWait(int withTimeout, int pollingEvery) { + return new FluentWait<>(Utils.driver) + .withTimeout(withTimeout, TimeUnit.MILLISECONDS) + .pollingEvery(pollingEvery, TimeUnit.MILLISECONDS) + .ignoring(NoSuchElementException.class) + .ignoring(StaleElementReferenceException.class); + } +} Index: ssts-selenium-test/src/test/java/test_4_1/Recyle.java =================================================================== diff -u -r18161 -r18181 --- ssts-selenium-test/src/test/java/test_4_1/Recyle.java (.../Recyle.java) (revision 18161) +++ ssts-selenium-test/src/test/java/test_4_1/Recyle.java (.../Recyle.java) (revision 18181) @@ -7,24 +7,25 @@ import java.util.List; import java.util.NoSuchElementException; -import java.util.Set; import java.util.concurrent.TimeUnit; import java.util.function.Function; +import net.sf.json.JSONObject; import org.openqa.selenium.Alert; import org.openqa.selenium.By; import org.openqa.selenium.StaleElementReferenceException; import org.openqa.selenium.WebDriver; import org.openqa.selenium.WebElement; +import org.openqa.selenium.interactions.Actions; 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 org.testng.AssertJUnit; import org.testng.annotations.AfterMethod; import org.testng.annotations.BeforeMethod; import org.testng.annotations.Test; +import test_4_1.factory.WaitFactory; import test_4_1.tools.Utils; /** @@ -876,58 +877,51 @@ @Test public void recyle_custom(String basket) { try { - Utils.retryingFindAndSendKeys(By.id("awaitForRecycleTable"), Utils.settings.getJSONObject("临床科室").getString("条码") + "\n"); - WebElement element = Utils.retryingFind(By.id("tousseItemTable")); + Actions actions = new Actions(Utils.driver); + actions.moveToElement(Utils.findByAwait(By.id("awaitForRecycleTable"))).sendKeys(Utils.settings.getJSONObject("临床科室").getString("条码") + "\n").perform(); + // Utils.retryingFindAndSendKeys(By.id("awaitForRecycleTable"), Utils.settings.getJSONObject("临床科室").getString("条码") + "\n"); + WebElement element = Utils.tryFind(By.id("tousseItemTable")); + // WebElement element = Utils.retryingFind(By.id("tousseItemTable")); if (element == null) { Utils.retryingFindClickFirstCellOfTable(By.id("awaitForRecycleTable")); } - WebElement recycleForm = scanRecycleUserBarcode(); + this.scanRecycleUserBarcode(); Utils.changeCustomWindow(); - String currentwindowhandle = Utils.driver.getWindowHandle(); - Utils.driver.switchTo().window(currentwindowhandle); - Wait wait = new FluentWait(Utils.driver).withTimeout(5000, TimeUnit.MILLISECONDS).pollingEvery(250, TimeUnit.MILLISECONDS).ignoring( - NoSuchElementException.class).ignoring(StaleElementReferenceException.class); - recycleForm = wait.until(new Function() { - public WebElement apply(WebDriver driver) { - return Utils.retryingFind(By.id("tousseItemTable")); - } - }); - recycleForm.sendKeys(basket + "\n"); + String currentWindowHandle = Utils.driver.getWindowHandle(); + Utils.driver.switchTo().window(currentWindowHandle); + WebElement recycleForm = Utils.retryingFind(By.id("tousseItemTable")); + actions.moveToElement(recycleForm).sendKeys(basket + "\n").perform(); + // recycleForm.sendKeys(basket + "\n"); dialogWindowClose(); // 判断自定义入筐扫完篮筐直到回调成功后才执行后面的入筐 + Wait wait = WaitFactory.createDefaultWait(); wait.until(ExpectedConditions.attributeToBe(By.id("seleniumResult"), "value", "1")); - List addButtons = recycleForm.findElements(By.cssSelector(".btn-a")); + List addButtons = recycleForm.findElements(By.className("btn-a")); for (WebElement addButton : addButtons) { - addButton.click(); - + Utils.jsClick(addButton); + // addButton.click(); } - Utils.retryingFindAndClick(By.cssSelector(".btn-e")); + Utils.jsClick(Utils.findByAwait(By.className("btn-e"))); + // Utils.retryingFindAndClick(By.cssSelector(".btn-e")); Utils.doWait(1000); for (String winHandle : Utils.driver.getWindowHandles()) { Utils.driver.switchTo().window(winHandle); } - wait = new FluentWait(Utils.driver).withTimeout(5000, TimeUnit.MILLISECONDS).pollingEvery(250, TimeUnit.MILLISECONDS).ignoring(NoSuchElementException.class) - .ignoring(StaleElementReferenceException.class); - recycleForm = wait.until(new Function() { - public WebElement apply(WebDriver driver) { - return Utils.retryingFind(By.id("tousseItemTable")); - } - }); - addButtons = recycleForm.findElements(By.cssSelector(".btn-a")); + recycleForm = Utils.retryingFind(By.id("tousseItemTable")); + addButtons = recycleForm.findElements(By.className("btn-a")); + // addButtons = recycleForm.findElements(By.cssSelector(".btn-a")); for (WebElement addButton : addButtons) { - addButton.click(); - boolean displayed = false; + Utils.jsClick(addButton); + // addButton.click(); element = Utils.retryingFind(By.id("idBoxClose")); - if (element != null) { - displayed = element.isDisplayed(); - if (displayed) { - Utils.retryingFindAndClick(By.id("idBoxClose")); - } + if (Utils.isFind(element)){ + Utils.jsClick(element); + // Utils.retryingFindAndClick(By.id("idBoxClose")); } - } - Utils.retryingFindAndClick(By.id("saveBtn")); - Thread.sleep(2000); + Utils.jsClick(Utils.findByAwait(By.id("saveBtn"))); + // Utils.retryingFindAndClick(By.id("saveBtn")); + Utils.doWait(2000); } catch (Exception e) { e.printStackTrace(); AssertJUnit.fail(e.getMessage()); @@ -965,26 +959,60 @@ } /** + * 扫描条码并断言扫描后的结果 + * @param by 要输入的条码位置 + * @param user 要输入的条码对应的用户 + */ + private WebElement scanBarcode(By by, String user){ + JSONObject jsonObject = Utils.settings.getJSONObject(user); + WebElement webElement = Utils.retryingFindAndSendKeys(by, jsonObject.getString("条码") + "\n"); + Wait wait = WaitFactory.createDefaultWait(); + // 断言回收人或者清洗人的姓名填写成功 + wait.until(ExpectedConditions.attributeContains(by, "value", jsonObject.getString("姓名"))); + return webElement; + } + + /** * 省医回收扫描回收人和清点人 */ private WebElement scanRecycleUserBarcode() { + WebElement table = null; + try { + // 扫描回收人条码 + table = this.scanBarcode(By.id("recyclingUser"), "回收人"); + // 扫描清点人条码 + table = this.scanBarcode(By.id("operator"), "清点人"); + } catch (Exception e) { + e.printStackTrace(); + AssertJUnit.fail(e.getMessage()); + } + return table; + } + /*private WebElement scanRecycleUserBarcode() { + WebElement table = null; try { + // 扫描回收人条码 + String name = Utils.settings.getJSONObject("回收人").getString("条码"); + + table = Utils.retryingFindAndSendKeys(By.id("recyclingUser"), name + + "\n"); // table = Utils.retryingFindAndSendKeys(By.id("tousseItemTable"), // name + "\n"); - Wait wait = new FluentWait(Utils.driver).withTimeout(5000, TimeUnit.MILLISECONDS).pollingEvery(250, TimeUnit.MILLISECONDS).ignoring( - NoSuchElementException.class).ignoring(StaleElementReferenceException.class); - table=wait.until(new Function() { - String name = Utils.settings.getJSONObject("回收人").getString("条码"); - public WebElement apply(WebDriver driver) { - return Utils.retryingFindAndSendKeys(By.id("recyclingUser"), name + "\n"); - } - }); + // 断言回收人的姓名填写成功 - wait.until(ExpectedConditions.attributeContains(By.id("recyclingUser"), "value", Utils.settings.getJSONObject("回收人").getString("姓名"))); + Wait wait = new FluentWait(Utils.driver) + .withTimeout(5000, TimeUnit.MILLISECONDS) + .pollingEvery(250, TimeUnit.MILLISECONDS) + .ignoring(NoSuchElementException.class) + .ignoring(StaleElementReferenceException.class); + wait.until(ExpectedConditions.attributeContains(By + .id("recyclingUser"), "value", Utils.settings + .getJSONObject("回收人").getString("姓名"))); + // AssertJUnit.assertEquals(Utils.settings.getJSONObject("回收人").getString("姓名") // , // Utils.retryingFind(By.id("recyclingUser")).getAttribute("value")); @@ -995,6 +1023,8 @@ // String recyclingFormWindowhandle = // Utils.driver.getWindowHandle(); // Utils.driver.switchTo().window(recyclingFormWindowhandle); + name = Utils.settings.getJSONObject("清点人").getString("条码"); + // Thread.sleep(1000); // Utils.retryingFind(By.cssSelector("input[value='清点人员']")) // .click(); @@ -1003,14 +1033,13 @@ // 继续通过retryFind方式查找table,然后点击,提高可靠性 // table = Utils.retryingFindAndSendKeys(By.id("tousseItemTable"), // name + "\n"); - table=wait.until(new Function() { - String name = Utils.settings.getJSONObject("清点人").getString("条码"); - public WebElement apply(WebDriver driver) { - return Utils.retryingFindAndSendKeys(By.id("operator"), name + "\n"); - } - }); + table = Utils.retryingFindAndSendKeys(By.id("operator"), name + + "\n"); + // 断言清点人的姓名填写成功 - wait.until(ExpectedConditions.attributeContains(By.id("operator"), "value", Utils.settings.getJSONObject("清点人").getString("姓名"))); + wait.until(ExpectedConditions.attributeContains(By.id("operator"), + "value", Utils.settings.getJSONObject("清点人") + .getString("姓名"))); // AssertJUnit.assertEquals(Utils.settings.getJSONObject("清点人").getString("姓名") // , @@ -1029,17 +1058,21 @@ } return table; - } + }*/ private void dialogWindowClose() { - boolean displayed = false; + /*boolean displayed = false; WebElement element = Utils.retryingFind(By.xpath("//button/span[contains(text(), '是')]")); if (element != null) { displayed = element.isDisplayed(); } if (displayed) { Utils.retryingFindAndClick(By.xpath("//button/span[contains(text(), '是')]")); - + }*/ + WebElement button = Utils.tryFind(By.xpath("//button/span[contains(text(), '是')]")); + if (Utils.isFind(button)){ + button.click(); } + } } \ No newline at end of file Index: ssts-selenium-test/src/test/java/test_4_1/tools/Utils.java =================================================================== diff -u -r18161 -r18181 --- ssts-selenium-test/src/test/java/test_4_1/tools/Utils.java (.../Utils.java) (revision 18161) +++ ssts-selenium-test/src/test/java/test_4_1/tools/Utils.java (.../Utils.java) (revision 18181) @@ -2,6 +2,7 @@ import java.io.IOException; import java.io.InputStream; +import java.net.URL; import java.text.SimpleDateFormat; import java.util.ArrayList; import java.util.Date; @@ -26,6 +27,7 @@ import org.openqa.selenium.chrome.ChromeOptions; import org.openqa.selenium.ie.InternetExplorerDriver; 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.FluentWait; import org.openqa.selenium.support.ui.Wait; @@ -168,8 +170,8 @@ ieCapabilities.setCapability("version", "11.0"); try { -// driver = new RemoteWebDriver(new URL( "http://192.168.2.18:4444/wd/hub"), ieCapabilities); - driver = new InternetExplorerDriver(ieCapabilities); + 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); @@ -1127,4 +1129,12 @@ Utils.runJavaScript(script.toString()); } + /** + * 使用js来点击 + * @param webElement 要点击的元素 + */ + public static void jsClick(WebElement webElement){ + Utils.runJavaScript("arguments[0].click();", webElement); + } + }