我创建了两个测试。在超类方法中拉出BeforeSuite
和AfterSuite
。
测试单独传递但在我作为包执行时失败。
字符串错误NullPointerException
driver.get(baseUrl + "test/test/");
public class MyTestBase {
private WebDriver driver;
private String baseUrl;
private boolean acceptNextAlert = true;
@BeforeSuite
public void setUp() throws Exception {
driver = new FirefoxDriver();
baseUrl = "http://test.com/";
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
}
@AfterSuite
public void tearDown() throws Exception {
driver.quit();
}
代码:
private void login() throws InterruptedException {
driver.get(baseUrl + "test/test/");
driver.findElement(By.xpath("//input")).clear();
driver.findElement(By.xpath("//input")).sendKeys("user234");
driver.findElement(By.xpath("//div[2]/div[2]/div/input")).clear();
driver.findElement(By.xpath("//div[2]/div[2]/div/input")).sendKeys("645637");
submitButton();
Thread.sleep(2000);
}
测试1
`public class AddEquipment extends MyTestBase { }`
测试2
`public class AddAnotherEquipment extends MyTestBase { }`
两个测试都是相同的,但使用不同的参数设备和描述字段。
我刚开始学习WebDriver并尝试创建简单的东西。
测试代码1:
package com.example.tests;
import org.testng.annotations.Test;
public class AddEquipment extends MyTestBase {
@Test
public void testAddEquipment() throws Exception {
GroupData group = new GroupData("Equipment", "Description");
createEquipment(group);
// assert
}
protected void createEquipment(GroupData group) throws InterruptedException {
login();
menuClickEquipment();
clickAddButton();
fillOutForm(group);
submitButton();
logout();
}
private void login() throws InterruptedException {
driver.get(baseUrl + "test/test/");
driver.findElement(By.xpath("//input")).clear();
driver.findElement(By.xpath("//input")).sendKeys("user234");
driver.findElement(By.xpath("//div[2]/div[2]/div/input")).clear();
driver.findElement(By.xpath("//div[2]/div[2]/div/input")).sendKeys("645637");
submitButton();
Thread.sleep(2000);
}