0

I try to make me testing code more readable, but I have some problems with the availability of my driver. I will show you the code:

Here I implement my firefox webdriver:

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();
    }

}

Next I wrote a class that make a class to get the url and is more readable:

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);
    }
}

After that I have the readable method "go.to" in my test. But it doesn't work :(

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

Have you an idea?

Edit: stacktrace of:

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)
Deviasa
  • 153
  • 1
  • 13
  • 2
    provide more details like what issue or exception occurred. Also provide exception trace. – VKR Jan 07 '19 at 10:48
  • Could you tell the version of junit which you are using? – Echoinacup Jan 07 '19 at 10:52
  • JUnit 4.12. When I try: public static void to (String url) { driver driver = new driver(); try{ driver.driver.get(url);} catch(Exception exc) { exc.printStackTrace(); } } I get a green test, but it still doesnt work. Firefox dont open – Deviasa Jan 07 '19 at 11:27
  • Please follow Java naming conventions and start classes with upper case letters - that way your code will be easier to read, and synatax highlighting will also work correctly. – Hulk Jan 07 '19 at 11:27
  • Show us the stacktrace - and show us how `driver.driver` is initialized – Hulk Jan 07 '19 at 11:30
  • Because it is so lang I edit my questions and add the stacktrace – Deviasa Jan 07 '19 at 11:33
  • 1
    You're not using the driver class as a test class, so the `@Before` annotation has no effect. You need to move code from the `@Before` annotated method to a constructor. – yole Jan 07 '19 at 12:01
  • What a stupid fail... Thank you very much! – Deviasa Jan 07 '19 at 12:06
  • Okay that works now, but in my other steps I cant "reuse" my browser. It opens everytime a new instance... – Deviasa Jan 07 '19 at 12:16
  • You are using JUnits @Before, not the one from Cucumber. – M.P. Korstanje Jan 07 '19 at 13:41

0 Answers0