使用Cucumber Driver Factory后无法加载Chrome / Gecko驱动程序

时间:2018-11-15 17:22:11

标签: nullpointerexception cucumber selenium-chromedriver driver geckodriver

这是我在这里问过的第一个问题,所以我希望我以正确的方式提出它: 我正在开发一个多模块Maven项目,以测试仍在开发中的软件。我已经使用Cucumber,Selenium,JUnit已有一段时间了,并通过在线课程来提高自己的技能。在其中一个教程中,解释了如何使用驱动程序工厂和配置文件,因此我将其应用到了项目中,但是此后无法运行任何驱动程序(gecko / chrome / ie)。这是代码的很大一部分,所以我希望有人可以提供帮助:)

我从DriverFactory.class捕获了异常:

“无法加载浏览器:空”


config.properties文件只有一行: browser = chrome

public class DriverFactory {

public static WebDriver driver;

public WebDriver getDriver() {
    try {
        //Read Config
        ReadConfigFile file = new ReadConfigFile();
        String browserName = file.getBrowser();

        switch (browserName) {

            case "firefox":
                if (driver == null) {
                    System.setProperty("webdriver.gecko.driver", Constant.GECKO_DRIVER_DIRECTORY);

                    FirefoxOptions options = new FirefoxOptions();
                    options.setCapability("marionette", false);
                    driver = new FirefoxDriver(options);
                    driver.manage().window().maximize();
                }
                break;

            case "chrome":
                if (driver == null) {
                    System.setProperty("webdriver.chrome.driver", Constant.CHROME_DRIVER_DIRECTORY);

                    driver = new ChromeDriver();
                    driver.manage().window().maximize();
                }
                break;

            /*case "ie":
                if (driver == null) {
                    DesiredCapabilities capabilities = DesiredCapabilities.internetExplorer();
                    System.setProperty("webdriver.ie.driver", Constant.IE_DRIVER_DIRECTORY);
                    capabilities.setCapability("ignoreZoomSetting", true);
                    driver = new InternetExplorerDriver(capabilities);
                    driver.manage().window().maximize();
                }
             */
        }
    } catch (Exception e) {
        System.out.println("Unable to load browser: " + e.getMessage());
    } finally {
        driver.manage().timeouts().pageLoadTimeout(60, TimeUnit.SECONDS);
    }
    return driver;
}

public class ReadConfigFile {
protected InputStream input = null;
protected Properties prop = null;

public ReadConfigFile() {
    try {
        input = ReadConfigFile.class.getClassLoader().getResourceAsStream(Constant.CONFIG_PROPERTIES_DIRECTORY);
        prop = new Properties();
        prop.load(input);
    } catch(IOException e) {
        e.printStackTrace();
    }
}

public String getBrowser() {
    if(prop.getProperty("browser") == null) {
        return "";
    }
    return prop.getProperty("browser");
}

public class Constant {

public final static String CONFIG_PROPERTIES_DIRECTORY = "properties\\config.properties";
public final static String GECKO_DRIVER_DIRECTORY = System.getProperty("user.dir") + "\\src\\test\\resources\\geckodriver.exe";
public final static String CHROME_DRIVER_DIRECTORY = System.getProperty("user.dir") + "\\src\\test\\resources\\chromedriver.exe";

public class CreateCaseSteps extends DriverFactory {

//Background Steps

@Given("^user accesses the login page$")
public void userAccessesTheLoginPage() throws Throwable {
    getDriver().get("http://localhost:8080/");
}

@When("^user enters a valid username")
public void userEntersAValidUsername(DataTable table) throws Throwable {
    Thread.sleep(3000);
    List<List<String>> data = table.raw();
    getDriver().findElement(By.xpath("//input[@placeholder='username']")).sendKeys(data.get(0).get(0));
}

@When("^user enters a valid password")
public void userEntersAValidPassword(DataTable table) throws Throwable {
    Thread.sleep(3000);
    List<List<String>> data = table.raw();
    getDriver().findElement(By.xpath("//input[@placeholder='password']")).sendKeys(data.get(0).get(0));
}

@And("^user clicks on the login button$")
public void userClicksOnTheLoginButton() throws Throwable {
    getDriver().findElement(By.xpath("//input[@value='Login']")).click();
}

@Then("^user should be taken successfully to the login page$")
public void userShouldBeTakenSuccessfullyToTheLoginPage() throws Throwable {
    Thread.sleep(3000);
    WebElement logoutMenu = getDriver().findElement(By.xpath("//i[@class='glyphicon glyphicon-user']"));
    Assert.assertEquals(true, logoutMenu.isDisplayed());
}

0 个答案:

没有答案