问题
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)
Descrption:
I am working on Selenium by using Maven Project. I have ran the above mentioned code for multiple times but the web page is unable to receive inputs. While executing the same I am getting java.lang.NullPointerException.
The Packages I have Used: 1.Resources 2.Features 3.stepDefinitions 4.Runner
Note: The Interesting thing is that If I execute the code by creating Java project the code is working absolutely fine.
Thanks in Advance:)
回答1:
Within RegisterSteps
Class you have declared a global WebDriver instance as:
public WebDriver driver;
But moving ahead within void the_User_is_on_the_Registration_Page()
method you have initialized another WebDriver instance as:
WebDriver driver = new ChromeDriver();
Which have no scope outside void the_User_is_on_the_Registration_Page()
method. Hence you see java.lang.NullPointerException
Solution
Within void the_User_is_on_the_Registration_Page()
method change it as:
driver = new ChromeDriver();
来源:https://stackoverflow.com/questions/51784274/how-to-resolve-java-lang-nullpointerexception-in-selenium-with-cucumber