静态和非静态方法以及驱动程序存在问题(NullPointerException)

时间:2019-01-07 10:45:13

标签: java testing intellij-idea junit cucumber

我试图使我的测试代码更具可读性,但是我的驱动程序可用性存在一些问题。 我将向您显示代码:

我在这里实现了我的firefox网络驱动程序:

package journey.setup;

import org.junit.Before;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;

public class driver
{
    public WebDriver driver;

    @Before
    public void driver()
    {
        System.setProperty("webdriver.gecko.driver",
                "C:\\...\\geckodriver-v0.23.0-win64\\geckodriver.exe");
        driver = new FirefoxDriver();
    }

}

接下来,我编写了一个类,该类使获取URL的类更易读:

package journey.actions;

import journey.setup.driver;

public class go extends driver
{
    public static void to (String url)
    {
        driver driver = new driver();
        driver.driver.get(url);
    }
}

在那之后,我在测试中有了可读的方法“ go.to”。 但这不起作用:(

public class login
{
    @Given("^You visit: \"([^\"]*)\"$")
    public void youVisit(String url) throws Throwable
    {
        go.to(url);
    }
}

你有个主意吗?

编辑:堆栈跟踪:

import journey.setup.Driver;

public class Go extends Driver
{
    public static void to (String url)
    {
        Driver driver = new Driver();

        try
        {
        driver.driver.get(url);
        }

        catch(Exception exc)
        {
            exc.printStackTrace();
        }

    }
}


java.lang.NullPointerException
    at journey.actions.go.to(go.java:12)
    at steps.login.youVisit(login.java:16)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at cucumber.runtime.Utils$1.call(Utils.java:31)
    at cucumber.runtime.Timeout.timeout(Timeout.java:16)
    at cucumber.runtime.Utils.invoke(Utils.java:25)
    at cucumber.runtime.java.JavaStepDefinition.execute(JavaStepDefinition.java:37)
    at cucumber.runtime.StepDefinitionMatch.runStep(StepDefinitionMatch.java:40)
    at cucumber.api.TestStep.executeStep(TestStep.java:102)
    at cucumber.api.TestStep.run(TestStep.java:83)
    at cucumber.api.TestCase.run(TestCase.java:58)
    at cucumber.runner.Runner.runPickle(Runner.java:80)
    at cucumber.runtime.junit.PickleRunners$NoStepDescriptions.run(PickleRunners.java:140)
    at cucumber.runtime.junit.FeatureRunner.runChild(FeatureRunner.java:68)
    at cucumber.runtime.junit.FeatureRunner.runChild(FeatureRunner.java:23)
    at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
    at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
    at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
    at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
    at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
    at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
    at cucumber.runtime.junit.FeatureRunner.run(FeatureRunner.java:73)
    at cucumber.api.junit.Cucumber.runChild(Cucumber.java:118)
    at cucumber.api.junit.Cucumber.runChild(Cucumber.java:56)
    at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
    at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
    at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
    at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
    at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
    at cucumber.api.junit.Cucumber$1.evaluate(Cucumber.java:127)
    at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
    at org.junit.runner.JUnitCore.run(JUnitCore.java:137)
    at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:68)
    at com.intellij.rt.execution.junit.IdeaTestRunner$Repeater.startRunnerWithArgs(IdeaTestRunner.java:47)
    at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:242)
    at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:70)

0 个答案:

没有答案