运行 使用 Spring Boot、Selenium 和 JUnit 进行 Cucumber 测试时出现 NullPointerException
Getting NullPointerException while running a Cucumber test with Spring Boot, Selenium and JUnit
我有一个简单的基于 Selenium-Cucumber
的测试,当我执行它时会抛出 NullPointerException
。
我已经初始化了 WebDriver (chrome)
但由于某种原因测试无法选择它。
不确定为什么在 ChromeDriver
上执行 new
后驱动程序仍未初始化
错误
com.intellij.rt.execution.junit.JUnitStarter -ideVersion5 -junit4 com.example.TestRunner
driver: null
java.lang.NullPointerException
at com.example.GoogleCalcStepDefinition.I_open_google(GoogleCalcStepDefinition.java:38)
at ✽.I open Google(file:src/test/resources/features/calc.feature:7)
Feature: Check addition in Google calculatorcontent
In order to verify that Google calculator work correctly
As a user of Google
I should be able to get correct addition result
Scenario: Addition # src/test/resources/features/calc.feature:6
Given I open Google # GoogleCalcStepDefinition.I_open_google()
java.lang.NullPointerException
at com.example.GoogleCalcStepDefinition.I_open_google(GoogleCalcStepDefinition.java:38)
at ✽.I open Google(file:src/test/resources/features/calc.feature:7)
When I enter "2+2" in search textbox # GoogleCalcStepDefinition.I_enter_in_search_textbox(String)
Then I should get result as "4" # GoogleCalcStepDefinition.I_should_get_correct_result(String)
Failed scenarios:
src/test/resources/features/calc.feature:6 # Addition
1 Scenarios (1 failed)
3 Steps (1 failed, 2 skipped)
0m1.364s
java.lang.NullPointerException
at com.example.GoogleCalcStepDefinition.I_open_google(GoogleCalcStepDefinition.java:38)
at ✽.I open Google(file:src/test/resources/features/calc.feature:7)
GoogleCalcStepDefinition.java
public class GoogleCalcStepDefinition extends CucumberStepDefinitions {
private static final String CHROME_DRIVER_EXE = "drivers/chromedriver.exe";
private static final String WEBDRIVER_CHROME_DRIVER = "webdriver.chrome.driver";
private static WebDriver driver;
@BeforeClass
public static void setUp() {
System.out.println("Into the setUp() method...");
String filePath = ClassLoader.getSystemClassLoader().getResource(CHROME_DRIVER_EXE).getFile();
System.setProperty(WEBDRIVER_CHROME_DRIVER, filePath);
ChromeOptions options = new ChromeOptions();
options.setExperimentalOption("useAutomationExtension", false);
driver = new ChromeDriver(options);
}
@Given("^I open Google$")
public void I_open_google() {
System.out.println("driver: " + driver);
//Set implicit wait of 10 seconds and launch google
driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);
driver.get("https://www.google.co.in");
}
@When("^I enter \"([^\"]*)\" in search textbox$")
public void I_enter_in_search_textbox(String additionTerms) {
//Write term in google textbox
WebElement googleTextBox = driver.findElement(By.id("gbqfq"));
googleTextBox.sendKeys(additionTerms);
//Click on searchButton
WebElement searchButton = driver.findElement(By.id("gbqfb"));
searchButton.click();
}
@Then("^I should get result as \"([^\"]*)\"$")
public void I_should_get_correct_result(String expectedResult) {
//Get result from calculator
WebElement calculatorTextBox = driver.findElement(By.id("cwos"));
String result = calculatorTextBox.getText();
//Verify that result of 2+2 is 4
assertEquals(result, expectedResult);
}
@AfterClass
public static void closeBrowser() {
if (null != driver) {
driver.close();
driver.quit();
}
}
}
CucumberStepDefinitions.java
@RunWith(SpringRunner.class)
@SpringBootTest
public abstract class CucumberStepDefinitions {
}
TestRunner.java
@RunWith(Cucumber.class)
@CucumberOptions(plugin = {"pretty"}, features = {"src/test/resources/features"})
public class TestRunner {
}
src/test/resources/features/calc.feature
Feature: Check addition in Google calculatorcontent
In order to verify that Google calculator work correctly
As a user of Google
I should be able to get correct addition result
Scenario: Addition
Given I open Google
When I enter "2+2" in search textbox
Then I should get result as "4"
src/test/resources/drivers/chromedriver.exe
pom.xml
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>io.cucumber</groupId>
<artifactId>cucumber-java</artifactId>
<version>4.2.3</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>io.cucumber</groupId>
<artifactId>cucumber-junit</artifactId>
<version>4.2.3</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>io.cucumber</groupId>
<artifactId>cucumber-spring</artifactId>
<version>4.2.3</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.seleniumhq.selenium</groupId>
<artifactId>selenium-java</artifactId>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>org.seleniumhq.selenium</groupId>
<artifactId>selenium-chrome-driver</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.seleniumhq.selenium</groupId>
<artifactId>selenium-chrome-driver</artifactId>
<version>2.45.0</version>
<scope>test</scope>
</dependency>
问题是您使用了 JUnit 的 @BeforeClass
注释,而 Cucumber runner 不遵守该注释。
相反,您可以使用 cucumber.api.Before
-> @Before
或 @After
注释。
如果您使用这些注释之一来注释方法挂钩,您可以使用 Scenario
接口来获取,例如,运行 场景的标签。
您还可以有多个 @Before
和 @After
方法,您可以像这样提供它们的顺序:
@Before(order = 1)
public void init(Scenario scenario) { }
我有一个简单的基于 Selenium-Cucumber
的测试,当我执行它时会抛出 NullPointerException
。
我已经初始化了 WebDriver (chrome)
但由于某种原因测试无法选择它。
不确定为什么在 ChromeDriver
new
后驱动程序仍未初始化
错误
com.intellij.rt.execution.junit.JUnitStarter -ideVersion5 -junit4 com.example.TestRunner
driver: null
java.lang.NullPointerException
at com.example.GoogleCalcStepDefinition.I_open_google(GoogleCalcStepDefinition.java:38)
at ✽.I open Google(file:src/test/resources/features/calc.feature:7)
Feature: Check addition in Google calculatorcontent
In order to verify that Google calculator work correctly
As a user of Google
I should be able to get correct addition result
Scenario: Addition # src/test/resources/features/calc.feature:6
Given I open Google # GoogleCalcStepDefinition.I_open_google()
java.lang.NullPointerException
at com.example.GoogleCalcStepDefinition.I_open_google(GoogleCalcStepDefinition.java:38)
at ✽.I open Google(file:src/test/resources/features/calc.feature:7)
When I enter "2+2" in search textbox # GoogleCalcStepDefinition.I_enter_in_search_textbox(String)
Then I should get result as "4" # GoogleCalcStepDefinition.I_should_get_correct_result(String)
Failed scenarios:
src/test/resources/features/calc.feature:6 # Addition
1 Scenarios (1 failed)
3 Steps (1 failed, 2 skipped)
0m1.364s
java.lang.NullPointerException
at com.example.GoogleCalcStepDefinition.I_open_google(GoogleCalcStepDefinition.java:38)
at ✽.I open Google(file:src/test/resources/features/calc.feature:7)
GoogleCalcStepDefinition.java
public class GoogleCalcStepDefinition extends CucumberStepDefinitions {
private static final String CHROME_DRIVER_EXE = "drivers/chromedriver.exe";
private static final String WEBDRIVER_CHROME_DRIVER = "webdriver.chrome.driver";
private static WebDriver driver;
@BeforeClass
public static void setUp() {
System.out.println("Into the setUp() method...");
String filePath = ClassLoader.getSystemClassLoader().getResource(CHROME_DRIVER_EXE).getFile();
System.setProperty(WEBDRIVER_CHROME_DRIVER, filePath);
ChromeOptions options = new ChromeOptions();
options.setExperimentalOption("useAutomationExtension", false);
driver = new ChromeDriver(options);
}
@Given("^I open Google$")
public void I_open_google() {
System.out.println("driver: " + driver);
//Set implicit wait of 10 seconds and launch google
driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);
driver.get("https://www.google.co.in");
}
@When("^I enter \"([^\"]*)\" in search textbox$")
public void I_enter_in_search_textbox(String additionTerms) {
//Write term in google textbox
WebElement googleTextBox = driver.findElement(By.id("gbqfq"));
googleTextBox.sendKeys(additionTerms);
//Click on searchButton
WebElement searchButton = driver.findElement(By.id("gbqfb"));
searchButton.click();
}
@Then("^I should get result as \"([^\"]*)\"$")
public void I_should_get_correct_result(String expectedResult) {
//Get result from calculator
WebElement calculatorTextBox = driver.findElement(By.id("cwos"));
String result = calculatorTextBox.getText();
//Verify that result of 2+2 is 4
assertEquals(result, expectedResult);
}
@AfterClass
public static void closeBrowser() {
if (null != driver) {
driver.close();
driver.quit();
}
}
}
CucumberStepDefinitions.java
@RunWith(SpringRunner.class)
@SpringBootTest
public abstract class CucumberStepDefinitions {
}
TestRunner.java
@RunWith(Cucumber.class)
@CucumberOptions(plugin = {"pretty"}, features = {"src/test/resources/features"})
public class TestRunner {
}
src/test/resources/features/calc.feature
Feature: Check addition in Google calculatorcontent
In order to verify that Google calculator work correctly
As a user of Google
I should be able to get correct addition result
Scenario: Addition
Given I open Google
When I enter "2+2" in search textbox
Then I should get result as "4"
src/test/resources/drivers/chromedriver.exe
pom.xml
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>io.cucumber</groupId>
<artifactId>cucumber-java</artifactId>
<version>4.2.3</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>io.cucumber</groupId>
<artifactId>cucumber-junit</artifactId>
<version>4.2.3</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>io.cucumber</groupId>
<artifactId>cucumber-spring</artifactId>
<version>4.2.3</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.seleniumhq.selenium</groupId>
<artifactId>selenium-java</artifactId>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>org.seleniumhq.selenium</groupId>
<artifactId>selenium-chrome-driver</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.seleniumhq.selenium</groupId>
<artifactId>selenium-chrome-driver</artifactId>
<version>2.45.0</version>
<scope>test</scope>
</dependency>
问题是您使用了 JUnit 的 @BeforeClass
注释,而 Cucumber runner 不遵守该注释。
相反,您可以使用 cucumber.api.Before
-> @Before
或 @After
注释。
如果您使用这些注释之一来注释方法挂钩,您可以使用 Scenario
接口来获取,例如,运行 场景的标签。
您还可以有多个 @Before
和 @After
方法,您可以像这样提供它们的顺序:
@Before(order = 1)
public void init(Scenario scenario) { }