将webdriver传递给另一个类的正确方法

时间:2018-05-29 19:01:02

标签: java selenium junit cucumber-jvm

我想将WebDriver传递给另一个类,而不是将其传递给该类中的各个方法。这意味着当我创建它的实例时将它传递给类的构造函数。这是我的代码,我的问题在下面进一步说明 -

public class StepDefinitions{

    public static WebDriver driver = null;
    CustomWaits waits;

    @Before("@setup") 
    public void setUp() {
        driver = utilities.DriverFactory.createDriver(browserType);
        System.out.println("# StepDefinitions.setUp(), driver = " + driver);
        waits = new CustomWaits(driver);
    }
}


public class CustomWaits {
    WebDriver driver;

    public CustomWaits(WebDriver driver){
        this.driver = driver;
    }
public boolean explicitWaitMethod(String id) {
        boolean status = false;
        try {
            WebDriverWait wait = new WebDriverWait(driver, 30);
            WebElement element = wait.until(ExpectedConditions.visibilityOfElementLocated(By.id(id)));
            status = element.isDisplayed();
        } catch (NullPointerException e){
            e.printStackTrace();
        }
        return status;
    }
    }

在@Given,@ When等中调用该类的方法时,我遇到的错误是NullPointerException。这是我无法解决的范围问题。

功能文件:

@test
Feature: Test 

  @setup
  Scenario: Navigate to Webpage and Assert Page Title
    Given I am on the "google" Website
    Then page title is "google"

以下是步骤定义:

@Given("^element with id \"([^\"]*)\" is displayed$")
public void element_is_displayed(String link) throws Throwable {
  if (waits.explicitWaitMethod(link)) { 
    // This is where waits becomes null when I put a breakpoint

    driver.findElement(By.id(link)).isDisplayed();
  } else {
    System.out.println("Timed out waiting for element to display");
  }
}

1 个答案:

答案 0 :(得分:0)

我会做这样的事情。

public class StepDefinitions{

    public StepDefinitions() {
        driver = utilities.DriverFactory.createDriver(browserType);
        System.out.println("# StepDefinitions.setUp(), driver = " + driver);
        waits = new CustomWaits(driver);
    }

    public static WebDriver driver = null;
    public static CustomWaits waits;

    @Given("^element with id \"([^\"]*)\" is displayed$")
    public void element_is_displayed(String link) throws Throwable {
       if (waits.explicitWaitMethod(link)) { 
          // This is where waits becomes null when I put a breakpoint

          driver.findElement(By.id(link)).isDisplayed();
       } else {
          System.out.println("Timed out waiting for element to display");
       }
    }

}


public class CustomWaits {
    private static WebDriver driver;

    public CustomWaits(WebDriver driver){
        this.driver = driver;
    }
    public boolean explicitWaitMethod(String id) {
        boolean status = false;
        try {
            WebDriverWait wait = new WebDriverWait(driver, 30);
            WebElement element = wait.until(ExpectedConditions.visibilityOfElementLocated(By.id(id)));
            status = element.isDisplayed();
        } catch (NullPointerException e){
            e.printStackTrace();
        }
        return status;
    }
}