在黄瓜微微容器中获取空指针异常

Getting nullpointer exception in cucumber picocontainer

我正在尝试使用 picocontainer 将 webdriver 传递到我的步骤定义中,但得到的是空指针

请参考下面我的实用程序 class 将 webdriver 实例声明为 public:-

package base;
import org.openqa.selenium.WebDriver;

public class Utils {
  public WebDriver driver;
}

请参考下面我的实用程序 class 具有调用和销毁驱动程序实例的挂钩:-

    public class Baseutils extends Utils {

    private Utils obj;

    public Baseutils(Utils obj) {
        this.obj=obj;
    }

    @Before("@chrome")
    public void chromeSetup()
    {
        if(obj.driver==null)
        {
            System.out.println("Chrome instance has been instantiated");
            System.setProperty("webdriver.chrome.driver", 
                  PropertiesfileReader.getchromepath());
            ChromeOptions options=new ChromeOptions();
            options.addArguments("--start-maximized");
            options.addArguments("--disable-notifications");
            options.addArguments("--no-proxy-server");
            obj.driver=new ChromeDriver(options);
        }
    }

    @Before("@firefox")
    public void firefoxSetup()
    {
        if(obj.driver==null)
        {
            System.out.println("Firefox instance has been instantiated");
            System.setProperty("webdriver.gecko.driver",PropertiesfileReader.getgeckopath());
            obj.driver=new FirefoxDriver();
            obj.driver.manage().window().maximize();
        }
    }

    @After
    public void teardown()
    {
        if(obj.driver!=null)
        {
            System.out.println("Browser instance has been closed");
            obj.driver.close();

        }
    }
}

我的特征文件描述如下:-

Feature: To verify and validate login functionality of Orangehrmdemo website

Background:
Given User navigates to Orangehrm demo

@chrome
Scenario: To verify login for valid user credentials

 When Homepage of website is displayed
 Then User validates login page contents
 Then User enters "Admin" and "admin123" and click on login button

特征文件对应的步骤定义文件如下:-

 public class orangehrmdemoSteps extends Utils {

 private Utils obj;

public orangehrmdemoSteps(Utils obj) {
    this.obj = obj;
 }

WebElement username= obj.driver.findElement(By.id("txtUsername"));
WebElement password=obj.driver.findElement(By.id("txtPassword"));
WebElement loginbutton=obj.driver.findElement(By.id("btnLogin"));

@Given("^User navigates to Orangehrm demo$")
public void user_navigates_to_Orangehrm_demo()  {
    obj.driver.get(PropertiesfileReader.geturltwo());
    obj.driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);
}

@When("^Homepage of website is displayed$")
public void homepage_of_website_is_displayed()  {
    WebElement logo=obj.driver.findElement(By.cssSelector("div#divLogo img"));
    Assert.assertTrue("Logo on orange hrm is not displayed", logo.isDisplayed());
}

@Then("^User validates login page contents$")
public void user_validates_login_page_contents() {
  Assert.assertTrue("username field not displayed", username.isDisplayed());
  Assert.assertTrue("password field not displayed", password.isDisplayed());
  Assert.assertTrue("login button not displayed", loginbutton.isDisplayed());
}

@Then("^User enters \"([^\"]*)\" and \"([^\"]*)\" and click on login button$")
public void user_enters_and_and_click_on_login_button(String uname, String pwd)  {
   username.sendKeys(uname);
   password.sendKeys(pwd);
   loginbutton.submit();
  }
}

我的跑步者文件如下:-

@RunWith(Cucumber.class)
@CucumberOptions(strict = true,plugin = 
{"pretty","html:target/html/automation"},features = 
{"src/test/java/featurefiles"},glue ={"stepDefinitions"},tags = "@chrome")

public class one { }

我的 POM 文件如下:-

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
     xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 
 http://maven.apache.org/xsd/maven-4.0.0.xsd">
 <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
 </properties>
 <modelVersion>4.0.0</modelVersion>
 <groupId>com.cucumberparellel</groupId>
 <artifactId>com.Parellel</artifactId>
 <version>1.0-SNAPSHOT</version>
 <dependencies>
    <!--Adding cucumber dependencies-->
    <dependency>
        <groupId>info.cukes</groupId>
        <artifactId>cucumber-core</artifactId>
        <version>1.2.5</version>
    </dependency>
    <dependency>
        <groupId>info.cukes</groupId>
        <artifactId>cucumber-java</artifactId>
        <version>1.2.5</version>
    </dependency>
    <dependency>
        <groupId>info.cukes</groupId>
        <artifactId>cucumber-jvm</artifactId>
        <version>1.2.5</version>
        <type>pom</type>
    </dependency>
    <dependency>
        <groupId>info.cukes</groupId>
        <artifactId>cucumber-junit</artifactId>
        <version>1.2.5</version>
    </dependency>
    <dependency>
        <groupId>info.cukes</groupId>
        <artifactId>cucumber-picocontainer</artifactId>
        <version>1.2.5</version>
    </dependency>
    <!--Adding selenium server dependencies-->
    <dependency>
        <groupId>org.seleniumhq.selenium</groupId>
        <artifactId>selenium-server</artifactId>
        <version>3.14.0</version>
    </dependency>
    <!-- Adding Junit dependency-->
    <dependency>
        <groupId>junit</groupId>
        <artifactId>junit</artifactId>
        <version>4.12</version>
    </dependency>

    <build>
    <plugins>
     <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-compiler-plugin</artifactId>
            <version>3.0</version>
            <configuration>
                <fork>true</fork>
                <executable> 
               C:\ProgramFiles\Java\jdk1.8.0_171\bin\javac.exe
                 </executable>
                <source>1.8</source>
                <target>1.8</target>
            </configuration>
        </plugin>
        <!--*******Maven surefire plugin added **-->
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-surefire-plugin</artifactId>
            <version>2.21.0</version>
            <configuration>
                <forkCount>2</forkCount>
                <reuseForks>true</reuseForks>
                <parallel>classes</parallel>
                <threadCount>2</threadCount>
                <includes>
                    <include>**/Parallel*IT.java</include>
                </includes>
            </configuration>
        </plugin>

The console Error are given as follows :- 



 Feature: To verify and validate login functionality of Orangehrmdemo website

    java.lang.NullPointerException
    at stepDefinitions.orangehrmdemoSteps.<init>(orangehrmdemoSteps.java:21)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
    at 
 sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
    at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
    at java.lang.reflect.Constructor.newInstance(Constructor.java:423)
    at org.picocontainer.injectors.AbstractInjector.newInstance(AbstractInjector.java:145)
    at org.picocontainer.injectors.ConstructorInjector.run(ConstructorInjector.java:342)
    at org.picocontainer.injectors.AbstractInjector$ThreadLocalCyclicDependencyGuard.observe(AbstractInjector.java:270)
    at org.picocontainer.injectors.ConstructorInjector.getComponentInstance(ConstructorInjector.java:364)
    at org.picocontainer.injectors.AbstractInjectionFactory$LifecycleAdapter.getComponentInstance(AbstractInjectionFactory.java:56)
    at org.picocontainer.behaviors.AbstractBehavior.getComponentInstance(AbstractBehavior.java:64)
    at org.picocontainer.behaviors.Stored.getComponentInstance(Stored.java:91)
    at org.picocontainer.DefaultPicoContainer.getInstance(DefaultPicoContainer.java:699)
    at org.picocontainer.DefaultPicoContainer.getComponent(DefaultPicoContainer.java:647)
    at org.picocontainer.DefaultPicoContainer.getComponent(DefaultPicoContainer.java:678)
    at cucumber.runtime.java.picocontainer.PicoFactory.getInstance(PicoFactory.java:40)
    at cucumber.runtime.java.JavaStepDefinition.execute(JavaStepDefinition.java:38)
    at cucumber.runtime.StepDefinitionMatch.runStep(StepDefinitionMatch.java:37)
    at cucumber.runtime.Runtime.runStep(Runtime.java:300)
    at cucumber.runtime.model.StepContainer.runStep(StepContainer.java:44)
    at cucumber.runtime.model.StepContainer.runSteps(StepContainer.java:39)
    at cucumber.runtime.model.CucumberScenario.runBackground(CucumberScenario.java:59)
    at cucumber.runtime.model.CucumberScenario.run(CucumberScenario.java:42)
    at cucumber.runtime.junit.ExecutionUnitRunner.run(ExecutionUnitRunner.java:102)
    at cucumber.runtime.junit.FeatureRunner.runChild(FeatureRunner.java:63)
    at cucumber.runtime.junit.FeatureRunner.runChild(FeatureRunner.java:18)
    at org.junit.runners.ParentRunner.run(ParentRunner.java:290)
    at org.junit.runners.ParentRunner.schedule(ParentRunner.java:71)
    at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
    at org.junit.runners.ParentRunner.access[=15=]0(ParentRunner.java:58)
    at org.junit.runners.ParentRunner.evaluate(ParentRunner.java:268)
    at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
    at cucumber.runtime.junit.FeatureRunner.run(FeatureRunner.java:70)
    at cucumber.api.junit.Cucumber.runChild(Cucumber.java:95)
    at cucumber.api.junit.Cucumber.runChild(Cucumber.java:38)
    at org.junit.runners.ParentRunner.run(ParentRunner.java:290)
    at org.junit.runners.ParentRunner.schedule(ParentRunner.java:71)
    at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
    at org.junit.runners.ParentRunner.access[=15=]0(ParentRunner.java:58)
    at org.junit.runners.ParentRunner.evaluate(ParentRunner.java:268)
    at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
    at cucumber.api.junit.Cucumber.run(Cucumber.java:100)
    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)
    at ✽.Given User navigates to Orangehrm demo(orangehrmDemo.feature:4)


java.lang.NullPointerException
    at stepDefinitions.orangehrmdemoSteps.<init>(orangehrmdemoSteps.java:21)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
    at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
    at java.lang.reflect.Constructor.newInstance(Constructor.java:423)
    at org.picocontainer.injectors.AbstractInjector.newInstance(AbstractInjector.java:145)
    at org.picocontainer.injectors.ConstructorInjector.run(ConstructorInjector.java:342)
    at org.picocontainer.injectors.AbstractInjector$ThreadLocalCyclicDependencyGuard.observe(AbstractInjector.java:270)
    at org.picocontainer.injectors.ConstructorInjector.getComponentInstance(ConstructorInjector.java:364)
    at org.picocontainer.injectors.AbstractInjectionFactory$LifecycleAdapter.getComponentInstance(AbstractInjectionFactory.java:56)
    at org.picocontainer.behaviors.AbstractBehavior.getComponentInstance(AbstractBehavior.java:64)
    at org.picocontainer.behaviors.Stored.getComponentInstance(Stored.java:91)
    at org.picocontainer.DefaultPicoContainer.getInstance(DefaultPicoContainer.java:699)
    at org.picocontainer.DefaultPicoContainer.getComponent(DefaultPicoContainer.java:647)
    at org.picocontainer.DefaultPicoContainer.getComponent(DefaultPicoContainer.java:678)
    at cucumber.runtime.java.picocontainer.PicoFactory.getInstance(PicoFactory.java:40)
    at cucumber.runtime.java.JavaStepDefinition.execute(JavaStepDefinition.java:38)
    at cucumber.runtime.StepDefinitionMatch.runStep(StepDefinitionMatch.java:37)
    at cucumber.runtime.Runtime.runStep(Runtime.java:300)
    at cucumber.runtime.model.StepContainer.runStep(StepContainer.java:44)
    at cucumber.runtime.model.StepContainer.runSteps(StepContainer.java:39)
    at cucumber.runtime.model.CucumberScenario.runBackground(CucumberScenario.java:59)
    at cucumber.runtime.model.CucumberScenario.run(CucumberScenario.java:42)
    at cucumber.runtime.junit.ExecutionUnitRunner.run(ExecutionUnitRunner.java:102)
    at cucumber.runtime.junit.FeatureRunner.runChild(FeatureRunner.java:63)
    at cucumber.runtime.junit.FeatureRunner.runChild(FeatureRunner.java:18)
    at org.junit.runners.ParentRunner.run(ParentRunner.java:290)
    at org.junit.runners.ParentRunner.schedule(ParentRunner.java:71)
    at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
    at org.junit.runners.ParentRunner.access[=15=]0(ParentRunner.java:58)
    at org.junit.runners.ParentRunner.evaluate(ParentRunner.java:268)
    at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
    at cucumber.runtime.junit.FeatureRunner.run(FeatureRunner.java:70)
    at cucumber.api.junit.Cucumber.runChild(Cucumber.java:95)
    at cucumber.api.junit.Cucumber.runChild(Cucumber.java:38)
    at org.junit.runners.ParentRunner.run(ParentRunner.java:290)
    at org.junit.runners.ParentRunner.schedule(ParentRunner.java:71)
    at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
    at org.junit.runners.ParentRunner.access[=15=]0(ParentRunner.java:58)
    at org.junit.runners.ParentRunner.evaluate(ParentRunner.java:268)
    at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
    at cucumber.api.junit.Cucumber.run(Cucumber.java:100)
    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)
    at ✽.Given User navigates to Orangehrm demo(orangehrmDemo.feature:4)


  Background:                              # orangehrmDemo.feature:3
    Given User navigates to Orangehrm demo # orangehrmdemoSteps.user_navigates_to_Orangehrm_demo()
      java.lang.NullPointerException
        at stepDefinitions.orangehrmdemoSteps.<init>(orangehrmdemoSteps.java:21)
        at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
        at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
        at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
        at java.lang.reflect.Constructor.newInstance(Constructor.java:423)
        at org.picocontainer.injectors.AbstractInjector.newInstance(AbstractInjector.java:145)
        at org.picocontainer.injectors.ConstructorInjector.run(ConstructorInjector.java:342)
        at org.picocontainer.injectors.AbstractInjector$ThreadLocalCyclicDependencyGuard.observe(AbstractInjector.java:270)
        at org.picocontainer.injectors.ConstructorInjector.getComponentInstance(ConstructorInjector.java:364)
        at org.picocontainer.injectors.AbstractInjectionFactory$LifecycleAdapter.getComponentInstance(AbstractInjectionFactory.java:56)
        at org.picocontainer.behaviors.AbstractBehavior.getComponentInstance(AbstractBehavior.java:64)
        at org.picocontainer.behaviors.Stored.getComponentInstance(Stored.java:91)
        at org.picocontainer.DefaultPicoContainer.getInstance(DefaultPicoContainer.java:699)
        at org.picocontainer.DefaultPicoContainer.getComponent(DefaultPicoContainer.java:647)
        at org.picocontainer.DefaultPicoContainer.getComponent(DefaultPicoContainer.java:678)
        at cucumber.runtime.java.picocontainer.PicoFactory.getInstance(PicoFactory.java:40)
        at cucumber.runtime.java.JavaStepDefinition.execute(JavaStepDefinition.java:38)
        at cucumber.runtime.StepDefinitionMatch.runStep(StepDefinitionMatch.java:37)
        at cucumber.runtime.Runtime.runStep(Runtime.java:300)
        at cucumber.runtime.model.StepContainer.runStep(StepContainer.java:44)
        at cucumber.runtime.model.StepContainer.runSteps(StepContainer.java:39)
        at cucumber.runtime.model.CucumberScenario.runBackground(CucumberScenario.java:59)
        at cucumber.runtime.model.CucumberScenario.run(CucumberScenario.java:42)
        at cucumber.runtime.junit.ExecutionUnitRunner.run(ExecutionUnitRunner.java:102)
        at cucumber.runtime.junit.FeatureRunner.runChild(FeatureRunner.java:63)
        at cucumber.runtime.junit.FeatureRunner.runChild(FeatureRunner.java:18)
        at org.junit.runners.ParentRunner.run(ParentRunner.java:290)
        at org.junit.runners.ParentRunner.schedule(ParentRunner.java:71)
        at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
        at org.junit.runners.ParentRunner.access[=15=]0(ParentRunner.java:58)
        at org.junit.runners.ParentRunner.evaluate(ParentRunner.java:268)
        at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
        at cucumber.runtime.junit.FeatureRunner.run(FeatureRunner.java:70)
        at cucumber.api.junit.Cucumber.runChild(Cucumber.java:95)
        at cucumber.api.junit.Cucumber.runChild(Cucumber.java:38)
        at org.junit.runners.ParentRunner.run(ParentRunner.java:290)
        at org.junit.runners.ParentRunner.schedule(ParentRunner.java:71)
        at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
        at org.junit.runners.ParentRunner.access[=15=]0(ParentRunner.java:58)
        at org.junit.runners.ParentRunner.evaluate(ParentRunner.java:268)
        at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
        at cucumber.api.junit.Cucumber.run(Cucumber.java:100)
        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)
        at ✽.Given User navigates to Orangehrm demo(orangehrmDemo.feature:4)


Test ignored.

Test ignored.

Test ignored.

  @chrome
  Scenario: To verify login for valid user credentials                # orangehrmDemo.feature:7
    When Homepage of website is displayed                             # orangehrmdemoSteps.homepage_of_website_is_displayed()
    Then User validates login page contents                           # orangehrmdemoSteps.user_validates_login_page_contents()
    Then User enters "Admin" and "admin123" and click on login button # orangehrmdemoSteps.user_enters_and_and_click_on_login_button(String,String)

Failed scenarios:
orangehrmDemo.feature:7 # Scenario: To verify login for valid user credentials

1 Scenarios (1 failed)
4 Steps (1 failed, 3 skipped)
0m0.089s

java.lang.NullPointerException
    at stepDefinitions.orangehrmdemoSteps.<init>(orangehrmdemoSteps.java:21)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
    at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
    at java.lang.reflect.Constructor.newInstance(Constructor.java:423)
    at org.picocontainer.injectors.AbstractInjector.newInstance(AbstractInjector.java:145)
    at org.picocontainer.injectors.ConstructorInjector.run(ConstructorInjector.java:342)
    at org.picocontainer.injectors.AbstractInjector$ThreadLocalCyclicDependencyGuard.observe(AbstractInjector.java:270)
    at org.picocontainer.injectors.ConstructorInjector.getComponentInstance(ConstructorInjector.java:364)
    at org.picocontainer.injectors.AbstractInjectionFactory$LifecycleAdapter.getComponentInstance(AbstractInjectionFactory.java:56)
    at org.picocontainer.behaviors.AbstractBehavior.getComponentInstance(AbstractBehavior.java:64)
    at org.picocontainer.behaviors.Stored.getComponentInstance(Stored.java:91)
    at org.picocontainer.DefaultPicoContainer.getInstance(DefaultPicoContainer.java:699)
    at org.picocontainer.DefaultPicoContainer.getComponent(DefaultPicoContainer.java:647)
    at org.picocontainer.DefaultPicoContainer.getComponent(DefaultPicoContainer.java:678)
    at cucumber.runtime.java.picocontainer.PicoFactory.getInstance(PicoFactory.java:40)
    at cucumber.runtime.java.JavaStepDefinition.execute(JavaStepDefinition.java:38)
    at cucumber.runtime.StepDefinitionMatch.runStep(StepDefinitionMatch.java:37)
    at cucumber.runtime.Runtime.runStep(Runtime.java:300)
    at cucumber.runtime.model.StepContainer.runStep(StepContainer.java:44)
    at cucumber.runtime.model.StepContainer.runSteps(StepContainer.java:39)
    at cucumber.runtime.model.CucumberScenario.runBackground(CucumberScenario.java:59)
    at cucumber.runtime.model.CucumberScenario.run(CucumberScenario.java:42)
    at cucumber.runtime.junit.ExecutionUnitRunner.run(ExecutionUnitRunner.java:102)
    at cucumber.runtime.junit.FeatureRunner.runChild(FeatureRunner.java:63)
    at cucumber.runtime.junit.FeatureRunner.runChild(FeatureRunner.java:18)
    at org.junit.runners.ParentRunner.run(ParentRunner.java:290)
    at org.junit.runners.ParentRunner.schedule(ParentRunner.java:71)
    at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
    at org.junit.runners.ParentRunner.access[=15=]0(ParentRunner.java:58)
    at org.junit.runners.ParentRunner.evaluate(ParentRunner.java:268)
    at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
    at cucumber.runtime.junit.FeatureRunner.run(FeatureRunner.java:70)
    at cucumber.api.junit.Cucumber.runChild(Cucumber.java:95)
    at cucumber.api.junit.Cucumber.runChild(Cucumber.java:38)
    at org.junit.runners.ParentRunner.run(ParentRunner.java:290)
    at org.junit.runners.ParentRunner.schedule(ParentRunner.java:71)
    at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
    at org.junit.runners.ParentRunner.access[=15=]0(ParentRunner.java:58)
    at org.junit.runners.ParentRunner.evaluate(ParentRunner.java:268)
    at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
    at cucumber.api.junit.Cucumber.run(Cucumber.java:100)
    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)
    at ✽.Given User navigates to Orangehrm demo(orangehrmDemo.feature:4)


    Process finished with exit code -1

给出错误的行号是因为下面突出显示的行的驱动程序为空:-

    public class orangehrmdemoSteps extends Utils {

   private Utils obj;

    public orangehrmdemoSteps(Utils obj) {
        this.obj = obj;
    }

    **WebElement username= obj.driver.findElement(By.id("txtUsername"));**
    //line on which nullpointer occurs
    WebElement password=obj.driver.findElement(By.id("txtPassword"));
    WebElement loginbutton=obj.driver.findElement(By.id("btnLogin"));

您已尝试在实际导航到包含这些元素的页面之前识别这些元素。 正如您在下面所写,

WebElement username= obj.driver.findElement(By.id("txtUsername"));
WebElement password=obj.driver.findElement(By.id("txtPassword"));
WebElement loginbutton=obj.driver.findElement(By.id("btnLogin"));

@Given("^User navigates to Orangehrm demo$")
public void user_navigates_to_Orangehrm_demo()  {
    obj.driver.get(PropertiesfileReader.geturltwo());
    obj.driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);

首先,您应该转到 URL,然后是相关页面,然后尝试识别 WebElement,而不是在此之前。 仅将所有 WebElement 放在该相关步骤中。

问题现已解决,因为基类是在步骤文件包之外的包中声明的,因此无法访问驱动程序实例。