我想将我的部分代码Selenium
移动到另一个项目中,因为我计划在许多测试中多次使用它。
一开始,我的大多数情况下我需要以管理员身份登录服务,所以我不想在每次测试中都有这段代码。
public void tc_mp_011() throws InterruptedException {
WebDriverWait wait = new WebDriverWait(driver, 10);
driver.manage().timeouts().pageLoadTimeout(60, TimeUnit.SECONDS);
long start = System.currentTimeMillis();
long end = start + 10 * 1000;
while (true) {
if (driver.findElement(By.xpath("//*[@id='signout']/div/div/div/span")).isDisplayed()) {
Thread.sleep(10);
if (System.currentTimeMillis() < end);
else {
System.err.println("timeout after: " + (System.currentTimeMillis() - start) + "ms");
driver.navigate().refresh();
start = System.currentTimeMillis();
end = start + 10 * 1000;
}
continue;
} else {
System.out.println("login Page loaded after " + (System.currentTimeMillis() - start) + " ms");
break;
}
}
wait.until(ExpectedConditions.elementToBeClickable(By.id("username")));
start = System.currentTimeMillis();
end = start + 10 * 1000;
driver.findElement(By.id("username")).click();
WebElement a = driver.findElement(By.id("username"));
a.sendKeys("admin");
wait.until(ExpectedConditions.elementToBeClickable(By.id("password")));
driver.findElement(By.id("password")).click();
WebElement b = driver.findElement(By.id("password"));
b.sendKeys("admin");
if (driver.findElement(By.id("username")).getAttribute("value").equals("admin") && driver.findElement(By.id("password")).getAttribute("value").equals("admin")) {
} else {
driver.findElement(By.id("username")).clear();
driver.findElement(By.id("password")).clear();
driver.findElement(By.id("username")).sendKeys("admin");
driver.findElement(By.id("password")).sendKeys("admin");
}
wait.until(ExpectedConditions.elementToBeClickable(By.cssSelector("button.btn.btn-primary")));
driver.findElement(By.cssSelector("button.btn.btn-primary")).click();
//step 1
start = System.currentTimeMillis();
end = start + 10 * 1000;
wait.until(ExpectedConditions.visibilityOfElementLocated(By.cssSelector("#panel > div.panel-body > div.col-md-12 > div.controls > #enable")));
if (System.currentTimeMillis() < end) {
try {
while (
driver.findElement(By.cssSelector("#panel > div.panel-body > div.col-md-12 > div.controls > #enable")).isDisplayed()) {
System.out.println("Pageloaded after " + (System.currentTimeMillis() - start) + " ms");
break;
}
} catch (Exception nse) {
}
} else {
System.err.println("Fail - Page not loaded in 10s");
org.testng.Assert.fail("Fail - Page not loaded in given time");
}
wait.until(ExpectedConditions.visibilityOfElementLocated(By.cssSelector("#panel > div.panel-body > div.col-md-12 > div.controls > #enable")));
WebElement username = driver.findElement(By.id("signedinusername"));
wait.until(ExpectedConditions.textToBePresentInElement(username, "admin"));
if (driver.findElement(By.id("signedinusername")).getText().equals("admin")) {} else {
org.testng.Assert.fail("Fail - login as admin");
}
driver.findElement(By.linkText("Sign Out")).click();
}
@BeforeMethod
public void beforeMethod() {
System.setProperty("webdriver.chrome.driver", "C:\chromedriver.exe");
ChromeOptions options = new ChromeOptions();
options.addArguments("--start-maximized");
driver = new ChromeDriver(options);
driver.get("http://192.168.1.100./");
}
@AfterMethod
public void afterMethod() {
// Close the driver
driver.quit();
}
我试图使用Page Object Pattern
,但我有变量,我不想再无视一次,老实说我不能
例如,driver = new ChromeDriver(options);
将打开浏览器的新实例,我不想要这个。
也许有一种不同的更简单的方法来解决这个问题
我不想复制代码我想做这样的事情
Test1.
Common.loginAsAdmin(driver); //this is call to another class in another project
Rest of Code (deleting users)
Test2.
Common.loginAsAdmin(driver); //this is call to another class in another project
Rest of Code (creating users)
Test3.
Common.loginAsAdmin(driver); //this is call to another class in another project
Rest of Code (modify users)
(...)
你应该有一套测试,通常每个测试都有一种方法,以防你使用的是Junit,试试这个
@Test
public void testOne() { //all your Selenium code here}
@Test
public void testTwo() { //all your Selenium code here}
最后,如果你需要它们之间的对话,请尝试使用作用域类变量代替
你的代码真的搞砸了,你应该清理它。如果您正在谈论代码可重用性,请创建您需要调用的函数,然后在测试中调用它们。不要在测试中编写所有内容。说几个问题
- 元素是硬编码的
- 所有逻辑都在测试中,因此没有代码可重用性。
我建议谷歌页面对象模式并学习如何使用它。
不过这将是个好的开始https://sqa.stackexchange.com/questions/9901/how-to-implement-page-object-and-page-factory-pattern-in-selenium-webdriver