如何用 Cucumber 解析 Selenium 中的 java.lang.NullPointerException?

How to resolve java.lang.NullPointerException in Selenium with Cucumber?

Register.feature
Feature: The Registration Page

Background:
Given The User is on the Registration Page

Scenario: The User Should be able to Register
When The User Enters Contact Information
And The User Enters Mailing Information
When The User Enters User Information

ReisterSteps.java
public class RegisterSteps {

    public  WebDriver driver;

    @Given("^The User is on the Registration Page$")
    public void the_User_is_on_the_Registration_Page() throws Throwable {
        System.out.println("Background:- The User is on the Home Page");
        System.setProperty("webdriver.chrome.driver", "C:\Selenium\cucumberBDDProject\src\Resources\chromedriver.exe");
        //System.setProperty("webdriver.chrome.driver", "C:\Selenium\chromedriver_win32\chromedriver.exe");
        WebDriver driver = new ChromeDriver();
        driver.get("http://www.newtours.demoaut.com/");
        Thread.sleep(3000);
        driver.navigate().to("http://www.newtours.demoaut.com/mercuryregister.php");
        Thread.sleep(3000);
    }

         @When("^The User Enters Contact Information$")
            public void the_User_Enters_Contact_Information() throws Throwable {
                System.out.println("Step1:- The User Enters Contact Information");
                try 
                {

                WebElement e = driver.findElement(By.name("firstName"));
                e.click();
                e.sendKeys("Manideep");
                driver.findElement(By.name("lastName")).sendKeys("Latchupatula");
                driver.findElement(By.name("phone")).sendKeys("9052324348");
                driver.findElement(By.name("userName")).sendKeys("mastro1729@gmail.com");
    }

        catch(Exception e)
                {
                    System.out.println("Exception has Caught");
                }
            } 
    @And("^The User Enters Mailing Information$")
    public void the_User_Enters_Mailing_Information() throws Throwable {
        System.out.println("Step2:- The User Enters Mailing Information");
        driver.findElement(By.name("address1")).sendKeys("Chinareddy Street");
        driver.findElement(By.name("address2")).sendKeys("Alajangi");
        driver.findElement(By.name("city")).sendKeys("Bobbili");
        driver.findElement(By.name("state")).sendKeys("Andhra Pradesh");
        driver.findElement(By.name("postalCode")).sendKeys("535558");
        Thread.sleep(3000);
        WebElement e = driver.findElement(By.name("country"));
        Select s = new Select(e);
        s.selectByValue("India");
        Thread.sleep(3000);

    }

    @When("^The User Enters User Information$")
    public void the_User_Enters_User_Information() throws Throwable {
        System.out.println("Step3:- The User Enters User Information");
        driver.findElement(By.name("email")).sendKeys("mastro1729@gmail.com");
        driver.findElement(By.name("password")).sendKeys("Pass@123");
        driver.findElement(By.name("confirmPassword")).sendKeys("Pass@123");
    }

}

Runner.java
package runners;

import org.junit.runner.RunWith;

import cucumber.api.CucumberOptions;
import cucumber.api.junit.Cucumber;

@RunWith(Cucumber.class)
@CucumberOptions(features="C:\Selenium\cucumberBDDProject\ProjectFeatures",glue="stepDefinitions")

public class LoginRunner {


}

Error Message:
Background:- The User is on the Home Page
Starting ChromeDriver 2.41.578737 (49da6702b16031c40d63e5618de03a32ff6c197e) on port 39832
Only local connections are allowed.
Aug 10, 2018 3:13:16 PM org.openqa.selenium.remote.ProtocolHandshake createSession
INFO: Detected dialect: OSS
Step1:- The User Enters Contact Information
Exception has Caught
Step2:- The User Enters Mailing Information
Step1:- The User is on the Login Page
Starting ChromeDriver 2.41.578737 (49da6702b16031c40d63e5618de03a32ff6c197e) on port 35493
Only local connections are allowed.
Aug 10, 2018 3:13:27 PM org.openqa.selenium.remote.ProtocolHandshake createSession
INFO: Detected dialect: OSS
[1533894216.869][WARNING]: Timed out connecting to Chrome, retrying...
[1533894220.887][WARNING]: Timed out connecting to Chrome, retrying...

[31mFailed scenarios:[0m
[31mRegister.feature:6 [0m# Scenario: The User Should be able to Register
[31mlogin.feature:2 [0m# Scenario: The User should be able to Login with Valid Credentials.

2 Scenarios ([31m2 failed[0m)
9 Steps ([31m2 failed[0m, [36m5 skipped[0m, [32m2 passed[0m)
0m37.335s

java.lang.NullPointerException
    at stepDefinitions.RegisterSteps.the_User_Enters_Mailing_Information(RegisterSteps.java:56)
    at ?.And The User Enters Mailing Information(Register.feature:8)

说明:

我正在使用 Maven 项目开发 Selenium。我有多次 运行 上述代码,但网页无法接收输入。在执行相同的操作时,我得到 java.lang.NullPointerException.

我用过的包: 1.Resources 2.Features 3.stepDefinitions 4.Runner

注意: 有趣的是,如果我通过创建 Java 项目来执行代码,代码工作绝对正常。

提前致谢:)

RegisterSteps Class 中,您已将全局 WebDriver 实例声明为:

public  WebDriver driver;

但是在 void the_User_is_on_the_Registration_Page() 方法中继续前进,您已经将另一个 WebDriver 实例初始化为:

WebDriver driver = new ChromeDriver();

void the_User_is_on_the_Registration_Page() 方法之外没有作用域。因此你看到 java.lang.NullPointerException

解决方案

void the_User_is_on_the_Registration_Page() 方法中将其更改为:

driver = new ChromeDriver();