我有两个带选择器和方法的类(在这两个类中我声明了webdriver)。我也使用@findBy来选择定位器。
带有选择器和方法的登录页面
package objectsAndMethods;
public class LoginPage{
public WebDriver driver;
HomePage homePage = new HomePage();
public void PageFactory(WebDriver driver){
this.driver = driver;
}
//Login Page
@FindBy(id = "id") private WebElement userName;
@FindBy(id = "id1") private WebElement password;
@FindBy(id = "id2") private WebElement loginButton;
Parameters Parameters = new Parameters();
public void navigateToPage() {
driver.navigate().to(Parameters.getBaseUrl());
driver.manage().window().maximize();
}
public void fillUsername(){
userName.clear();
userName.sendKeys(Parameters.getUsername());
}
public void fillPassword(){
password.clear();
password.sendKeys(Parameters.getPassword());
}
public void clickLoginButton(){
loginButton.click();
}
public void checkUserLogged(){
WebDriverWait wait = new WebDriverWait(driver, 10);
wait.until(ExpectedConditions.elementToBeClickable(By.id("id")));
}
}
主页 - 包含选择器和方法
public class HomePage {
@FindBy(id = "id") private WebElement logoutButton;
@FindBy(id = "optionId") private WebElement selectOption;
@FindBy(xpath="/xpath") private WebElement submitButton;
public WebDriver driver;
public void selectOption(){
Select droplist = new Select(selectOption);
droplist.selectByVisibleText(Parameters.getOption());
}
public void clickSubmitButton(){
submitButton.click();
}
}
BASE TEST CLASS - setUp + login + tearDown
package baseClasses;
public class BaseClassTest{
// TODO Auto-generated method stub
@Before
public void setUp() {
loginPage.driver = new FirefoxDriver();
}
LoginPage loginPage = new LoginPage();
@Test
public void loginTestCase() throws IOException, InterruptedException {
loginPage.navigateToPage();
System.out.println("You are on " + baseClasses.Parameters.getBaseUrl());
Thread.sleep(2000);
LoginPage loginPageFindBy = PageFactory.initElements(loginPage.driver, LoginPage.class);
loginPageFindBy.fillUsername();
Thread.sleep(2000);
loginPageFindBy.fillPassword();
Thread.sleep(2000);
loginPageFindBy.clickLoginButton();
Thread.sleep(2000);
loginPage.checkUserLogged();
System.out.println("User is logged");
}
@After
public void tearDown() {
loginPage.driver.quit();
}
HomePageTest - 测试类
public class HomePageTest extends BaseClassTest{
//setUp();
HomePage homePage = new HomePage();
@Test
public void homeTestCase() throws IOException, InterruptedException {
super.setUp();
super.loginTestCase();
HomePage homePageFindBy = PageFactory.initElements(homePage.driver, HomePage.class);
homePageFindBy.selectOption();
Thread.sleep(2000);
homePageFindBy.clickSubmitButton();
Thread.sleep(2000);
}
}
HomePage测试从2级(登录页面和主页)调用方法 - 两者都有声明的驱动程序。 问题是,在运行测试时,打开了2个firefox实例,只有登录测试成功执行,HomePage中的其他方法失败。 有人可以帮我理解我做错了什么吗?
答案 0 :(得分:0)
当您运行HomePageTest
时setUp()
之前的@Before
注释下的方法BaseClassTest
正在homeTestCase()
之前被调用,因为HomePageTest
扩展BaseClassTest
}}
第loginPage.driver = new FirefoxDriver();
行打开第一个浏览器。当您再次致电super.setUp();
setUp()
时,会打开第二个浏览器。
只需从super.setUp();
移除homeTestCase()
。