package test_4_1; import org.testng.annotations.AfterMethod; import org.testng.annotations.Test; import org.testng.annotations.BeforeMethod; import org.testng.AssertJUnit; import java.text.SimpleDateFormat; import java.util.Date; import java.util.NoSuchElementException; import java.util.concurrent.TimeUnit; import org.openqa.selenium.By; import org.openqa.selenium.Keys; import org.openqa.selenium.StaleElementReferenceException; import org.openqa.selenium.WebDriver; import org.openqa.selenium.WebElement; import org.openqa.selenium.support.ui.ExpectedConditions; import org.openqa.selenium.support.ui.FluentWait; import org.openqa.selenium.support.ui.Wait; import test_4_1.tools.Utils; /** * @author cherry 2016-06-01 * */ public class One_use { @BeforeMethod public void init() { try { Utils.init(); Login login = new Login(); login.login_oneSupplyRoom(); Utils.retryingFindAndClick(By.id("enterpriseView")); WebElement frame = Utils.retryingFindAndSwitchToFrame(By .id("iframe_enterpriseView")); Utils.retryingFindAndClick( By.xpath("//div/span[contains(text(), '后台监控管理')]")); Utils.retryingFindAndClick( By.xpath("//div/a/span[contains(text(), '一次性物品管理')]")); } catch (Exception e) { e.printStackTrace(); AssertJUnit.fail(e.getMessage()); } } /** * 一次性物品入库 * */ @Test public void One_use_Entry_success() { try { Utils.retryingFindAndClick( By.xpath("//div/a/span[contains(text(), '入库单管理')]")); Utils.retryingFindAndSwitchToFrame(By.id("mainFrame")); Utils.retryingFindAndClick(By.xpath("//button[contains(text(), '添加')]")); String currentwindowhandle = Utils.driver.getWindowHandle(); Utils.driver.switchTo().window(currentwindowhandle); String name = Utils.settings.getJSONObject("一级供应室一次性物品").getString( "名称"); //要等待物品名称下拉框由disable变成enable后即可以被点击后再进行以下操作 Wait wait = new FluentWait(Utils.driver) .withTimeout(5000, TimeUnit.MILLISECONDS) .pollingEvery(250, TimeUnit.MILLISECONDS) .ignoring(NoSuchElementException.class) .ignoring(StaleElementReferenceException.class); wait.until(ExpectedConditions.elementToBeClickable(By .id("name1"))); for (int i = 1; i < 4; i++) { String x = (int) (Math.random() * 8 + 1) + ""; Utils.retryingFindAndSendKeys(By.id("name1"),Keys.DOWN); Utils.retryingFindAndClick( By.xpath("//div/div/div[contains(text(), '" + name + "')]")); Utils.retryingFind(By.id("amount1")).clear(); Utils.retryingFindAndSendKeys(By.id("amount1"),i + "\n"); Utils.retryingFindAndSendKeys(By.id("batchNumber1"), "2017030" + i + "\n"); Utils.retryingFind(By.id("expDate1")).clear(); Utils.retryingFindAndSendKeys(By.id("expDate1"), "2020010" + i + "\n"); Utils.retryingFind(By.id("cost1")).clear(); Utils.retryingFindAndSendKeys(By.id("cost1"), x + "." + x + x + "\n"); WebElement supplier = Utils.retryingFind(By.id("supplier1")); if (supplier.getText() != null) { supplier.sendKeys(Keys.DOWN); supplier.sendKeys("\n"); } Utils.retryingFindAndClick(By.xpath("//button[contains(text(), '添加')]")); Thread.sleep(1500); } name = Utils.settings.getJSONObject("一级供应室一次性物品1").getString( "名称"); //要等待物品名称下拉框由disable变成enable后即可以被点击后再进行以下操作 wait = new FluentWait(Utils.driver) .withTimeout(5000, TimeUnit.MILLISECONDS) .pollingEvery(250, TimeUnit.MILLISECONDS) .ignoring(NoSuchElementException.class) .ignoring(StaleElementReferenceException.class); wait.until(ExpectedConditions.elementToBeClickable(By .id("name1"))); for (int i = 5; i > 2; i--) { String x = (int) (Math.random() * 8 + 1) + ""; Utils.retryingFindAndSendKeys(By.id("name1"),Keys.DOWN); Utils.retryingFindAndClick( By.xpath("//div/div/div[contains(text(), '" + name + "')]")); Utils.retryingFind(By.id("amount1")).clear(); Utils.retryingFindAndSendKeys(By.id("amount1"),i + "\n"); Utils.retryingFindAndSendKeys(By.id("batchNumber1"), "2017030" + i + "\n"); Utils.retryingFind(By.id("expDate1")).clear(); Utils.retryingFindAndSendKeys(By.id("expDate1"), "2020010" + i + "\n"); Utils.retryingFind(By.id("cost1")).clear(); Utils.retryingFindAndSendKeys(By.id("cost1"), x + "." + x + x + "\n"); WebElement supplier = Utils.retryingFind(By.id("supplier1")); if (supplier.getText() != null) { supplier.sendKeys(Keys.DOWN); supplier.sendKeys("\n"); } Utils.retryingFindAndClick(By.xpath("//button[contains(text(), '添加')]")); Thread.sleep(1500); } Utils.retryingFindAndClick( By.xpath("//button[contains(text(), '保存并打印新标签')]")); //必须先定位回到最外层的iframe,然后逐步定位里层的iframe Utils.retryingFindAndSwitchToFrame(By .id("iframe_enterpriseView")); // 必须加上如下的切换代码,否则获取条码的javascript语句执行会报异常错误 Utils.retryingFindAndSwitchToFrame(By.id("mainFrame")); //等待表单提交成功 wait.until(ExpectedConditions.attributeToBe(Utils.driver.findElement(By .id("formSubmitResult")), "value", "1")); //获取一次性物品入库后的条码 Utils.disposableGoodsBatchBarcodesAfterEntry = Utils.getDisposableGoodsBatchBarcodesAfterEntry(); System.out.println("本次一次性物品入库后的条码:" + Utils.disposableGoodsBatchBarcodesAfterEntry); } catch (Exception e) { e.printStackTrace(); AssertJUnit.fail(e.getMessage()); } } /** * 一次性物品退库 * */ @Test public void One_use_cancle_success() { try { Utils.retryingFindAndClick( By.xpath("//div/a/span[contains(text(), '退库单管理')]")); WebElement frame = Utils.retryingFindAndSwitchToFrame(By.id("mainFrame")); Utils.retryingFindAndClick(By.xpath("//button[contains(text(), '添加')]")); String currentwindowhandle = Utils.driver.getWindowHandle(); Utils.driver.switchTo().window(currentwindowhandle); Utils.retryingFindAndSendKeys(By.id("name1"), Utils.settings.getJSONObject("一级供应室一次性物品").getString("名称")); Utils.retryingFindAndSendKeys(By.id("name1"), "\n"); Utils.retryingFindAndSendKeys(By.id("amount1"), "1" + "\n"); Utils.retryingFindAndClick(By.xpath("//button[contains(text(), '添加')]")); Utils.retryingFindAndClick(By.xpath("//button[contains(text(), '保存')]")); } catch (Exception e) { e.printStackTrace(); AssertJUnit.fail(e.getMessage()); } } /** * 一次性物品调拨 * */ @Test public void One_use_allot_success() { try { Utils.retryingFindAndClick( By.xpath("//div/a/span[contains(text(), '调拨单管理')]")); WebElement frame = Utils.retryingFindAndSwitchToFrame(By.id("mainFrame")); Utils.retryingFindAndClick(By.xpath("//button[contains(text(), '添加')]")); String currentwindowhandle = Utils.driver.getWindowHandle(); Utils.driver.switchTo().window(currentwindowhandle); String name = Utils.settings.getJSONObject("二级供应室用户").getString( "科室名称"); Utils.retryingFindAndSendKeys(By.id("targetWareHouse"),Keys.DOWN); Utils.retryingFindAndClick( By.xpath("//div/div/div[contains(text(), '" + name + "')]")); name = Utils.settings.getJSONObject("一级供应室一次性物品").getString("名称"); Utils.retryingFindAndSendKeys(By.id("name1"),Keys.DOWN); Utils.retryingFindAndClick( By.xpath("//div/div/div[contains(text(), '" + name + "')]")); Utils.retryingFindAndSendKeys(By.id("amount1"), "1" + "\n"); Utils.retryingFindAndClick(By.xpath("//button[contains(text(), '添加')]")); Utils.retryingFindAndClick(By.xpath("//button[contains(text(), '保存')]")); } catch (Exception e) { e.printStackTrace(); AssertJUnit.fail(e.getMessage()); } } /** * 一次性物品盘点 * */ @Test public void One_use_check() { try { Date now = new Date(); SimpleDateFormat dateFormater = new SimpleDateFormat("yyyy-MM-dd"); Utils.retryingFindAndClick( By.xpath("//div/a/span[contains(text(), '一次性物品盘点管理')]")); WebElement frame = Utils.retryingFindAndSwitchToFrame(By.id("mainFrame")); Utils.retryingFindAndClick(By.xpath("//button[contains(text(), '添加盘点记录')]")); Utils.retryingFindAndClick(By.id("saveBtn")); Utils.retryingFindAndClick( By.xpath("//a[contains(text(), '" + dateFormater.format(now) + "')]")); Utils.retryingFindAndClick( By.xpath("//button[contains(text(), '盘点数据初始化')]")); } catch (Exception e) { e.printStackTrace(); AssertJUnit.fail(e.getMessage()); } } @AfterMethod public void after() { Utils.driver.quit(); } }