簡體   English   中英

將網絡驅動程序傳遞給另一個類的正確方法

[英]Proper way to pass a webdriver to another class

我想將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");
  }
}

我會做這樣的事情。

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;
    }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM