Selenium - 无法访问页面对象模型属性

Selenium - Can't access page object model properties

我正在使用 Boni Garcia 'webdrivermanager'。 https://github.com/bonigarcia/webdrivermanager/blob/master/README.md

我的问题: 我无法 'get' 我的初始化驱动程序属性从 'BaseSwag' 到 'home.page' 以启动 Chrome 并转到所需的 URL。这是我的设置如下。我该怎么做才能解决这个问题?

src/main/java/swaglogin/BaseSwag

package swaggerLogin;

import io.github.bonigarcia.wdm.WebDriverManager;
import io.github.bonigarcia.wdm.managers.ChromeDriverManager;
import io.github.bonigarcia.wdm.managers.FirefoxDriverManager;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.Properties;

public class BaseSwag {

  public WebDriver driver;
  public Properties prop;

  public WebDriverManager initializeDriver() throws IOException {
    // Create global property file
    prop = new Properties();
    FileInputStream fis = new FileInputStream(
        "//Users/rad/WebTest/src/main/resources/data.properties");

    prop.load(fis);
    String browserName = prop.getProperty("browser");
    System.out.println(browserName);

    if (browserName.equals("chrome")) {
      WebDriverManager.chromedriver().setup();
      driver = new ChromeDriver();
    } else if (browserName.equals("firefox")) {
      WebDriverManager.firefoxdriver().setup();
      driver = new FirefoxDriver();
    }
    return driver;
  }
}

resources/data.properties

url = http://qaclickacademy.com/
apiKey = 1234563333random
browser = chrome

test/java/home.page

package home.page;

import java.io.IOException;
import org.testng.annotations.BeforeTest;
import swaggerLogin.BaseSwag;

public class SwaggerLoginDev extends BaseSwag {

  @BeforeTest

  public void initialize() throws IOException {
    driver = initializeDriver();
    driver.get(prop.getProperty("url"));

  }

}

堆栈跟踪

Error:(32, 12) java: cannot find symbol
  symbol:   method driver()
  location: class resources.Base

您的 driver 变量应该是 WebDriver 的实例,而不是 WebDriverManager

您可以将 WebDriverManager 视为实用程序 class,它只管理(下载、设置等)不同浏览器的驱动程序。为所需类型的浏览器调用 .setup() 方法后,您可以创建它的实例:

public class BaseSwag {

  public WebDriver driver;
  public Properties prop;

  public WebDriver initializeDriver() throws IOException {
    // Create global property file
    prop = new Properties();
    FileInputStream fis = new FileInputStream(
        "//Users/rad/WebTest/src/main/resources/data.properties");

    prop.load(fis);
    String browserName = prop.getProperty("browser");
    System.out.println(browserName);

    if (browserName.equals("chrome")) {
      WebDriverManager.chromedriver().setup();
      driver = new ChromeDriver();
    } else if (browserName.equals("firefox")) {
      WebDriverManager.firefoxdriver().setup();
      driver = new FirefoxDriver();
    }
    return driver;
  }
}

现在,您应该可以在驱动程序上调用 .get(...) 方法了。