0

我有两个带有选择器和方法的类(在它们中我都声明了 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();
      }
    }

基本测试类 - 设置 + 登录 + 拆解

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 实例,只有 Login Test 成功执行,HomePage 中的其他方法失败。有人可以帮我理解我做错了什么吗?

4

1 回答 1

0

当您在注释下运行HomePageTest该方法时, in之前被调用,因为extends 。setUp()@BeforeBaseClassTesthomeTestCase()HomePageTestBaseClassTest

该行loginPage.driver = new FirefoxDriver();打开第一个浏览器。当您super.setUp(); setUp()再次执行呼叫并打开第二个浏览器时。

只需super.setUp();homeTestCase().

于 2016-03-01T10:07:43.467 回答