我想代表我在项目中使用的结构。看来你忘了@Before
,@After
和 @Test
符号。
public class BaseSeleniumTest extends SeleneseTestBase {
static WebDriver driver;
@Before
public void openFirefox() throws IOException {
driver = new FirefoxDriver();
driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);
driver.get(propertyKeysLoader("login.base.url"));
doAdminLogin();
}
@After
public void closeFirefox(){
driver.quit();
}
public void doAdminLogin() throws IOException {
String curTitle=driver.getTitle();
locatorFindingHandling("login.logininput", "login.admin.login");
locatorFindingHandling("login.passinput", "login.admin.pass");
locatorFindingHandling("login.loginbutton");
String newTitle=driver.getTitle();
Assert.assertFalse(curTitle.equals(newTitle));
}
public void locatorFindingHandling(String key) throws IOException /*throws IOException*/ {
fluentWait(By.cssSelector(propertyKeysLoader(key))).click();
}
public void locatorFindingHandling(String key, String key1) throws IOException {
driver.findElement(By.xpath(propertyKeysLoader(key))).sendKeys(propertyKeysLoader(key1));
}
public void doLogout() throws InterruptedException, IOException {
String curTitle=driver.getTitle();
jsClick("rms.home.logout");
String newTitle=driver.getTitle();
Assert.assertFalse(curTitle.equals(newTitle));
}
....
}
然后我BaseSeleniumTest.java
以以下方式扩展我的:
public class LoginPageTestSuite extends BaseSeleniumTest {
@Test
public void loginWithEmptyCredentials() throws IOException, InterruptedException {
doLogout();
fluentWait(By.cssSelector(propertyKeysLoader("login.loginbutton"))).click();
Assert.assertTrue(fluentWait(By.cssSelector(propertyKeysLoader("login.validator.invalidautentication"))).getText().trim().equals("Invalid authentication"));
}
@Test
public void logoutAdminLogin() throws IOException, InterruptedException {
doLogout();
doAdminLogin();
}
@Test
public void loginWithWrongPass() throws IOException, InterruptedException {
doLogout();
locatorFindingHandling("login.logininput", "login.admin.login");
locatorFindingHandling("login.passinput", "login.invalidPass");
locatorFindingHandling("login.loginbutton");
Assert.assertTrue(fluentWait(By.cssSelector(propertyKeysLoader("login.validator.invalidautentication"))).getText().trim().equals("Invalid authentication"));
}
.....
}
因此,从您的代码来看,它类似于:
public class Setupbase extends SeleneseTestBase {
static WebDriver driver;
@Before
public void openFirefox() throws IOException {
driver = new FirefoxDriver();
driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);
String baseUrl = "http://example.com";
driver.get(baseUrl);
}
@After
public void closeFirefox(){
driver.quit();
}
}
public class Login extends Setupbase{
@Test
public void loginTest() {
driver.findElement(By.id("Email")).clear();
driver.findElement(By.id("Email")).sendKeys("username");
driver.findElement(By.id("Passwd")).clear();
driver.findElement(By.id("Passwd")).sendKeys("password");
driver.findElement(By.id("signIn")).click();
}
}
希望这对你有用。