java - 没有一个浏览器能够启动并获取 java.lang.NullPointerException 并以 -1 退出进程

标签 java junit cross-browser cucumber selenium-chromedriver

这是我的 Hook 类。

public class Hooks {


    public String browser;
    public static WebDriver driver;

    //IE, Chrome, Opera working |||  firefox ~working
    @Before
    public void beforeEach() throws IOException {

        browser = System.getenv("BROWSER");
        if (browser == null) {
            browser = "ie";
        }
        System.out.println("Browser selected is " + browser);

        if (browser.equalsIgnoreCase("chrome")) {
            DesiredCapabilities cap = new DesiredCapabilities();
            cap.setJavascriptEnabled(true);
            cap.setCapability(CapabilityType.ACCEPT_SSL_CERTS, true);
            System.setProperty("webdriver.chrome.driver", "C:/Program Files/SeleniumDrivers/chromedriver.exe");
            driver = new ChromeDriver(cap);

        } else if (browser.equalsIgnoreCase("ie")) {

            File file = new File("C:/Program Files/SeleniumDrivers/IEDriverServer.exe");
            System.setProperty("webdriver.ie.driver", file.getAbsolutePath());
            driver = new InternetExplorerDriver();

        } else if (browser.equalsIgnoreCase("opera")) {
            DesiredCapabilities capabilities = DesiredCapabilities.opera();
            capabilities.setCapability(CapabilityType.ACCEPT_SSL_CERTS, true);
            File file = new File("C:/Program Files/SeleniumDrivers/operadriver.exe");
            System.setProperty("webdriver.opera.driver", file.getAbsolutePath());
            driver = new OperaDriver(capabilities);

        } else{
            driver = new FirefoxDriver();
              }
        driver.manage().timeouts().pageLoadTimeout(120, TimeUnit.SECONDS);
        driver.manage().timeouts().implicitlyWait(60, TimeUnit.SECONDS);
        driver.manage().window().maximize();
        driver.manage().deleteAllCookies();
    }


    @After
    public void close() {


        driver.quit();
    }

} 

我正在将 Selenium Junit 与 cucumber 一起使用。 我已经实现了 IE、firefox、chrome、opera 的代码。 我无法启动浏览器并出现 java 空指针异常。 并且控制台中没有其他错误信息。

下面是我的用于登录步骤的 java 类。请问有什么线索吗?

public class LoginSteps {

    public WebDriver driverLaunch;
    public String landingURL = "https://www.periscopix.com";

    public LoginSteps() {
        driverLaunch = Hooks.driver;
    }

    @Given("^I am on company landing page$")
    public void I_am_on_company_landing_page() throws Throwable {
        driverLaunch.navigate().to("https://www.google.com");
        driverLaunch.close();
    }

    @Given("^I wait for some time$")
    public void I_wait_for_some_time() throws Throwable {
        Thread.sleep(1000);

    }

    @When("^Then the page loads succesfully$")
    public void Then_the_page_loads_succesfully() throws Throwable {
        assertEquals(driverLaunch.getCurrentUrl(), landingURL);

    }

}

提前致谢

最佳答案

在 ^LoginSteps^ 中扩展 ^Hooks^ 并使用 ^Hooks^ 的静态驱动程序实例。删除 ^driverLaunch^ 变量。

关于java - 没有一个浏览器能够启动并获取 java.lang.NullPointerException 并以 -1 退出进程,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31270192/

相关文章:

windows - Windows 7 和 Windows 8(台式机/Metro)之间的 Internet Explorer 10 有何不同?

javascript - 重构 Crossrider 扩展代码

java - 在 jsp 中显示来自 servlet 的数据但不覆盖当前值

java - JAX-WS : Return String as CDATA

java - java中如何在主程序后台实现事件监听?

java - cucumber Java 屏幕截图

java - Android HttpResponse 不能在 IF 语句中使用来检查内容

java - 为 Junit 测试创建模拟枚举值时出错

java - 如何使用 Junit 在 Java 中测试打印方法

reactjs - 如果用户的浏览器不受支持,我可以呈现警告消息吗?