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 Project开发Selenium。我已经多次运行上述代码,但是网页无法接收输入。执行相同的操作时,我正在获取java.lang.NullPointerException。
我使用过的软件包: 1.资源 2.功能 3.stepDefinitions 4.亚军
注意: 有趣的是,如果我通过创建Java项目执行代码,则代码可以正常运行。
预先感谢:)
答案 0 :(得分:4)
在RegisterSteps
类中,您已将全局 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();