在黄瓜中使用 pojo class 时出现空指针异常
Getting Nullpointer Exception while using pojo class in cucumber
我正在尝试通过 cucumber 中的 pojo class 传递数据,但出现空指针异常
我的特征文件如下-:
Feature: Registeration in Mercuryflight site
Background:
Given I've a valid set of data and access pojo
@Registrationpojo
Scenario: Multiple user Registration process using pojo
When Registeration page Display for pojo
Then Enter valid data for successful registration pojo
| username | password | confirmpassword |
| aditya91p | test123 | test123 |
| rakesh90p | test123 | test123 |
| preety18p | test123 | test123 |
And close
我的pojo class如下-:
package com.Cucumber_Maven.test;
public class UserData {
private String username;
private String password;
private String confirmpassword;
public UserData(String username, String password, String confirmpassword) {
this.username = username;
this.password = password;
this.confirmpassword = confirmpassword;
}
public String getUserName() {
return username;
}
public String getPassword() {
return password;
}
public String getConfirmPassword() {
return confirmpassword;
}
}
我的Step定义如下-:
package com.Cucumber_Maven.test;
import java.util.List;
import org.openqa.selenium.By;
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import cucumber.api.DataTable;
import cucumber.api.PendingException;
import cucumber.api.Scenario;
import cucumber.api.java.en.Given;
import cucumber.api.java.en.Then;
import cucumber.api.java.en.When;
import junit.framework.Assert;
public class StepPojo {
WebDriver driver;
Scenario scenario;
@Given("^I've a valid set of data and access pojo$")
public void i_ve_a_valid_set_of_data_and_access_pojo() throws Throwable {
System.out.println("Pojo class demo ");
System.setProperty("webdriver.chrome.driver", System.getProperty("user.dir")+"/src/test/resource/driver/chromedriver.exe");
driver = new ChromeDriver();
this.scenario= scenario;
}
@When("^Registeration page Display for pojo$")
public void registeration_page_Display_for_pojo() throws Throwable {
driver.manage().window().maximize();
driver.navigate().to("http://newtours.demoaut.com/");
Thread.sleep(5000);
WebElement register=driver.findElement(By.linkText("REGISTER"));
register.click();
}
/*@Then("^Enter valid data for successful registration pojo$")
public void enter_valid_data_for_successful_registration_pojo(List<UserData> pojolist) throws Throwable {
scenario.write("entering user registeration details...");
System.out.println("Total user..."+pojolist.size());
for(UserData data :pojolist) {
System.out.println(data.getUserName() +" with pswd is "+data.getPassword());
driver.findElement(By.xpath(".//*[@id='email']")).sendKeys(data.getUserName());
driver.findElement(By.xpath(".//*[@name='password']")).sendKeys(data.getPassword());
driver.findElement(By.xpath("")).sendKeys(data.getConfirmPassword());
driver.findElement(By.xpath(".//*[@name='register']")).click();
//assertion
Thread.sleep(3000);
String msg=driver.findElement(By.xpath(".//*[contains(text(),'Dear')]")).getText();
System.out.println(msg);
Assert.assertTrue("text is getting displayed", msg.contains("Dear"));
//clicking on register
WebElement register=driver.findElement(By.linkText("REGISTER"));
JavascriptExecutor js=(JavascriptExecutor) driver;
js.executeScript("arguments[0].setAttribute('target','_self');", register);
register.click();
}
}*/
@Then("^Enter valid data for successful registration pojo$")
public void enter_valid_data_for_successful_registration_pojo(List<UserData> pojolist) throws Throwable {
scenario.write("entering user registeration details...");
System.out.println("Total user..."+pojolist.size());
for(UserData data :pojolist) {
System.out.println(data.getUserName() +" with pswd is "+data.getPassword());
driver.findElement(By.xpath(".//*[@id='email']")).sendKeys(data.getUserName());
driver.findElement(By.xpath(".//*[@name='password']")).sendKeys(data.getPassword());
driver.findElement(By.xpath("")).sendKeys(data.getConfirmPassword());
driver.findElement(By.xpath(".//*[@name='register']")).click();
//assertion
Thread.sleep(3000);
String msg=driver.findElement(By.xpath(".//*[contains(text(),'Dear')]")).getText();
System.out.println(msg);
Assert.assertTrue("text is getting displayed", msg.contains("Dear"));
//clicking on register
WebElement register=driver.findElement(By.linkText("REGISTER"));
JavascriptExecutor js=(JavascriptExecutor) driver;
js.executeScript("arguments[0].setAttribute('target','_self');", register);
register.click();
}
}
}
我的错误Trace是-:
Background: # C:/Users/krsna/eclipse-workspace/Cucumber_Maven/src/test/resource/mercuryflight.feature:3
Given I've a valid set of data and access pojo # StepPojo.i_ve_a_valid_set_of_data_and_access_pojo()
@Registrationpojo
Scenario: Multiple user Registration process using pojo # C:/Users/krsna/eclipse-workspace/Cucumber_Maven/src/test/resource/mercuryflight.feature:7
When Registeration page Display for pojo # StepPojo.registeration_page_Display_for_pojo()
Then Enter valid data for successful registration pojo # StepPojo.enter_valid_data_for_successful_registration_pojo(UserData>)
java.lang.NullPointerException
at com.Cucumber_Maven.test.StepPojo.enter_valid_data_for_successful_registration_pojo(StepPojo.java:79)
at ✽.Then Enter valid data for successful registration pojo(C:/Users/krsna/eclipse-workspace/Cucumber_Maven/src/test/resource/mercuryflight.feature:9)
And close # StepDefinitionDemo.close()
Failed scenarios:
C:/Users/krsna/eclipse-workspace/Cucumber_Maven/src/test/resource/mercuryflight.feature:7 # Scenario: Multiple user Registration process using pojo
1 Scenarios (1 failed)
4 Steps (1 failed, 1 skipped, 2 passed)
0m40.507s
我的文件夹结构如下-:
如能解决此问题,我们将不胜感激。
我的 pom 文件也是 -:
<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">
<modelVersion>4.0.0</modelVersion>
<groupId>com</groupId>
<artifactId>Cucumber_Maven</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>Cucumber_Maven</name>
<url>http://maven.apache.org</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
<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> <scope>test</scope> </dependency> <dependency> <groupId>info.cukes</groupId>
<artifactId>cucumber-junit</artifactId> <version>1.2.5</version> </dependency> -->
<dependency>
<groupId>info.cukes</groupId>
<artifactId>cucumber-java</artifactId>
<version>1.2.0</version>
<!-- <scope>test</scope> -->
</dependency>
<dependency>
<groupId>info.cukes</groupId>
<artifactId>cucumber-junit</artifactId>
<version>1.2.0</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-jvm-deps</artifactId>
<version>1.0.5</version>
</dependency>
<dependency>
<groupId>org.seleniumhq.selenium</groupId>
<artifactId>selenium-java</artifactId>
<version>3.9.1</version>
</dependency>
</dependencies>
</project>
我在 -:
中收到此行的错误
@Then("^Enter valid data for successful registration pojo$")
public void enter_valid_data_for_successful_registration_pojo(List<UserData> pojolist) throws Throwable {
scenario.write("entering user registeration details...");
System.out.println("Total user..."+pojolist.size());
也许 NPE 是在方法 enter_valid_data_for_successful_registration_pojo(...)
的第
行抛出的
scenario.write("entering user registeration details...");
变量 scenario
较新地分配了任何对象(至少在提供的代码中没有)
Scenario scenario;
在方法中 i_ve_a_valid_set_of_data_and_access_pojo()
你有这个任务
this.scenario= scenario;
将 scenario
分配给自己,因此它保持 null
。
为了在步骤之间共享状态,建议使用依赖注入 (DI) 框架。 Cucumber 支持多种 DI 框架。如果您的项目已经在使用 DI,请使用您已有的任何 DI 框架。如果没有,请使用 PicoContainer,因为它最轻巧且易于使用。有关将 DI 与 Cucumber 结合使用的更多信息,我推荐 The Cucumber for Java 一书。不幸的是,那里没有很多文档(但我们正在努力)。
我正在尝试通过 cucumber 中的 pojo class 传递数据,但出现空指针异常
我的特征文件如下-:
Feature: Registeration in Mercuryflight site
Background:
Given I've a valid set of data and access pojo
@Registrationpojo
Scenario: Multiple user Registration process using pojo
When Registeration page Display for pojo
Then Enter valid data for successful registration pojo
| username | password | confirmpassword |
| aditya91p | test123 | test123 |
| rakesh90p | test123 | test123 |
| preety18p | test123 | test123 |
And close
我的pojo class如下-:
package com.Cucumber_Maven.test;
public class UserData {
private String username;
private String password;
private String confirmpassword;
public UserData(String username, String password, String confirmpassword) {
this.username = username;
this.password = password;
this.confirmpassword = confirmpassword;
}
public String getUserName() {
return username;
}
public String getPassword() {
return password;
}
public String getConfirmPassword() {
return confirmpassword;
}
}
我的Step定义如下-:
package com.Cucumber_Maven.test;
import java.util.List;
import org.openqa.selenium.By;
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import cucumber.api.DataTable;
import cucumber.api.PendingException;
import cucumber.api.Scenario;
import cucumber.api.java.en.Given;
import cucumber.api.java.en.Then;
import cucumber.api.java.en.When;
import junit.framework.Assert;
public class StepPojo {
WebDriver driver;
Scenario scenario;
@Given("^I've a valid set of data and access pojo$")
public void i_ve_a_valid_set_of_data_and_access_pojo() throws Throwable {
System.out.println("Pojo class demo ");
System.setProperty("webdriver.chrome.driver", System.getProperty("user.dir")+"/src/test/resource/driver/chromedriver.exe");
driver = new ChromeDriver();
this.scenario= scenario;
}
@When("^Registeration page Display for pojo$")
public void registeration_page_Display_for_pojo() throws Throwable {
driver.manage().window().maximize();
driver.navigate().to("http://newtours.demoaut.com/");
Thread.sleep(5000);
WebElement register=driver.findElement(By.linkText("REGISTER"));
register.click();
}
/*@Then("^Enter valid data for successful registration pojo$")
public void enter_valid_data_for_successful_registration_pojo(List<UserData> pojolist) throws Throwable {
scenario.write("entering user registeration details...");
System.out.println("Total user..."+pojolist.size());
for(UserData data :pojolist) {
System.out.println(data.getUserName() +" with pswd is "+data.getPassword());
driver.findElement(By.xpath(".//*[@id='email']")).sendKeys(data.getUserName());
driver.findElement(By.xpath(".//*[@name='password']")).sendKeys(data.getPassword());
driver.findElement(By.xpath("")).sendKeys(data.getConfirmPassword());
driver.findElement(By.xpath(".//*[@name='register']")).click();
//assertion
Thread.sleep(3000);
String msg=driver.findElement(By.xpath(".//*[contains(text(),'Dear')]")).getText();
System.out.println(msg);
Assert.assertTrue("text is getting displayed", msg.contains("Dear"));
//clicking on register
WebElement register=driver.findElement(By.linkText("REGISTER"));
JavascriptExecutor js=(JavascriptExecutor) driver;
js.executeScript("arguments[0].setAttribute('target','_self');", register);
register.click();
}
}*/
@Then("^Enter valid data for successful registration pojo$")
public void enter_valid_data_for_successful_registration_pojo(List<UserData> pojolist) throws Throwable {
scenario.write("entering user registeration details...");
System.out.println("Total user..."+pojolist.size());
for(UserData data :pojolist) {
System.out.println(data.getUserName() +" with pswd is "+data.getPassword());
driver.findElement(By.xpath(".//*[@id='email']")).sendKeys(data.getUserName());
driver.findElement(By.xpath(".//*[@name='password']")).sendKeys(data.getPassword());
driver.findElement(By.xpath("")).sendKeys(data.getConfirmPassword());
driver.findElement(By.xpath(".//*[@name='register']")).click();
//assertion
Thread.sleep(3000);
String msg=driver.findElement(By.xpath(".//*[contains(text(),'Dear')]")).getText();
System.out.println(msg);
Assert.assertTrue("text is getting displayed", msg.contains("Dear"));
//clicking on register
WebElement register=driver.findElement(By.linkText("REGISTER"));
JavascriptExecutor js=(JavascriptExecutor) driver;
js.executeScript("arguments[0].setAttribute('target','_self');", register);
register.click();
}
}
}
我的错误Trace是-:
Background: # C:/Users/krsna/eclipse-workspace/Cucumber_Maven/src/test/resource/mercuryflight.feature:3
Given I've a valid set of data and access pojo # StepPojo.i_ve_a_valid_set_of_data_and_access_pojo()
@Registrationpojo
Scenario: Multiple user Registration process using pojo # C:/Users/krsna/eclipse-workspace/Cucumber_Maven/src/test/resource/mercuryflight.feature:7
When Registeration page Display for pojo # StepPojo.registeration_page_Display_for_pojo()
Then Enter valid data for successful registration pojo # StepPojo.enter_valid_data_for_successful_registration_pojo(UserData>)
java.lang.NullPointerException
at com.Cucumber_Maven.test.StepPojo.enter_valid_data_for_successful_registration_pojo(StepPojo.java:79)
at ✽.Then Enter valid data for successful registration pojo(C:/Users/krsna/eclipse-workspace/Cucumber_Maven/src/test/resource/mercuryflight.feature:9)
And close # StepDefinitionDemo.close()
Failed scenarios:
C:/Users/krsna/eclipse-workspace/Cucumber_Maven/src/test/resource/mercuryflight.feature:7 # Scenario: Multiple user Registration process using pojo
1 Scenarios (1 failed)
4 Steps (1 failed, 1 skipped, 2 passed)
0m40.507s
我的文件夹结构如下-:
如能解决此问题,我们将不胜感激。
我的 pom 文件也是 -:
<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">
<modelVersion>4.0.0</modelVersion>
<groupId>com</groupId>
<artifactId>Cucumber_Maven</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>Cucumber_Maven</name>
<url>http://maven.apache.org</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
<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> <scope>test</scope> </dependency> <dependency> <groupId>info.cukes</groupId>
<artifactId>cucumber-junit</artifactId> <version>1.2.5</version> </dependency> -->
<dependency>
<groupId>info.cukes</groupId>
<artifactId>cucumber-java</artifactId>
<version>1.2.0</version>
<!-- <scope>test</scope> -->
</dependency>
<dependency>
<groupId>info.cukes</groupId>
<artifactId>cucumber-junit</artifactId>
<version>1.2.0</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-jvm-deps</artifactId>
<version>1.0.5</version>
</dependency>
<dependency>
<groupId>org.seleniumhq.selenium</groupId>
<artifactId>selenium-java</artifactId>
<version>3.9.1</version>
</dependency>
</dependencies>
</project>
我在 -:
中收到此行的错误 @Then("^Enter valid data for successful registration pojo$")
public void enter_valid_data_for_successful_registration_pojo(List<UserData> pojolist) throws Throwable {
scenario.write("entering user registeration details...");
System.out.println("Total user..."+pojolist.size());
也许 NPE 是在方法 enter_valid_data_for_successful_registration_pojo(...)
的第
scenario.write("entering user registeration details...");
变量 scenario
较新地分配了任何对象(至少在提供的代码中没有)
Scenario scenario;
在方法中 i_ve_a_valid_set_of_data_and_access_pojo()
你有这个任务
this.scenario= scenario;
将 scenario
分配给自己,因此它保持 null
。
为了在步骤之间共享状态,建议使用依赖注入 (DI) 框架。 Cucumber 支持多种 DI 框架。如果您的项目已经在使用 DI,请使用您已有的任何 DI 框架。如果没有,请使用 PicoContainer,因为它最轻巧且易于使用。有关将 DI 与 Cucumber 结合使用的更多信息,我推荐 The Cucumber for Java 一书。不幸的是,那里没有很多文档(但我们正在努力)。