硒/黄瓜/绒球模型-无法启动浏览器

时间:2019-02-28 09:49:04

标签: java selenium cucumber pageobjects

我正在使用页面对象模型使用Java,Selenium,Cucumber建立框架。项目-https://github.com/mohand1714/webautomatoin.git

运行代码时出现空指针异常。

如果我直接在launchstep.java文件中打开浏览器,代码将运行正常。

我不确定这里的问题是什么。我确定我已经初始化了所有必需的东西。

launchstep.java

package Steps.LaunchSteps;

import Utils.DriverFactory;
import cucumber.api.java.en.Given;

public class launchstep extends DriverFactory {

    @Given("^I go to HelloFresh$")
    public void iGoToHelloFresh() throws Throwable {
        OpenBrowser.goToURL();
    }
}

launchpage.java

package pageobjects.launchpage;

import pageobjects.BasePage;

import java.io.IOException;

public class launchpage extends BasePage {


    public launchpage() throws IOException {
    }

    public launchpage goToURL() throws IOException {
        getDriver().get("www.google.com");
        return new launchpage();

    }
  }

DriverFactory.java

public class DriverFactory {
public static WebDriver driver;
public static launchpage OpenBrowser;

public WebDriver getDriver() {

    try {
        Properties p = new Properties();
        FileInputStream fi = new FileInputStream(System.getProperty("user.dir") + "/src/main/java/properties/config.properties");
        p.load(fi);
        String browserName = p.getProperty("browser");


        switch (browserName) {

            case "firefox":

                if (null == driver) {
                    System.setProperty("webdriver.gecko.driver", Constant.GECKO_DRIVER_DIRECTORY);
                    DesiredCapabilities capabilities = DesiredCapabilities.firefox();
                    capabilities.setCapability("marionette", true);
                    driver = new FirefoxDriver();

                }
                break;

            case "chrome":

                if (null == driver) {
                    System.setProperty("webdriver.chrome.driver", Constant.CHROME_DRIVER_DIRECTORY);
                    driver = new ChromeDriver();
                    driver.manage().window().fullscreen();


                }
                break;

        }
    } catch (Exception e) {
        System.out.println("Unable to load browser" + e.getMessage());


    } finally

    {
        driver.manage().timeouts().pageLoadTimeout(60, TimeUnit.SECONDS);
OpenBrowser = PageFactory.initElements(driver, launchpage.class);

    }

    return driver;

}
}

0 个答案:

没有答案