我有一个使用Cucumber-Selenium
编写的基于Spring Boot
的测试。问题是,如果我只有一个步骤定义文件GoolgeCalcStepDefinition.java
,那么程序可以正常工作并且测试通过,并且没有任何问题,但是一旦我将BingSearchStepDefinition.java
与功能文件一起添加,就会出现以下错误。 >
我用Spring Boot
配置了Cucumber
的方法,但是网上提供的大多数示例/文章仅显示一个步骤定义文件。
mvn验证
-------------------------------------------------------
T E S T S
-------------------------------------------------------
Running com.example.TestRunner
Tests run: 1, Failures: 0, Errors: 1, Skipped: 0, Time elapsed: 0.368 sec <<< FAILURE! - in com.example.TestRunner
initializationError(com.example.TestRunner) Time elapsed: 0.004 sec <<< ERROR!
cucumber.runtime.CucumberException: Glue class class com.example.stepdefs.GoogleCalcStepDefinition and class com.example.stepdefs.BingSearchStepDefinition both attempt to configure the spring context. Please ensure only one glue class configures the spring context
Results :
Tests in error:
TestRunner.initializationError » Cucumber Glue class class com.example.stepdef...
Tests run: 1, Failures: 0, Errors: 1, Skipped: 0
[ERROR] There are test failures.
Please refer to I:\pet-projects\junit-cucumber-demo\target\surefire-reports for the individual test results.
[INFO]
[INFO] --- maven-jar-plugin:3.0.2:jar (default-jar) @ junit-cucumber-demo ---
[INFO] Building jar: I:\pet-projects\junit-cucumber-demo\target\junit-cucumber-demo-0.0.1-SNAPSHOT.jar
[INFO]
[INFO] --- spring-boot-maven-plugin:2.0.6.RELEASE:repackage (default) @ junit-cucumber-demo ---
[INFO]
[INFO] --- maven-cucumber-reporting:3.14.0:generate (execution) @ junit-cucumber-demo ---
ERROR StatusLogger No log4j2 configuration file found. Using default configuration: logging only errors to the console. Set system property 'org.apache.logging.log4j.simplelog.StatusLogger.level' to TRACE to show Log4j2 internal initialization logging.
[INFO] About to generate Cucumber report.
[INFO] ------------------------------------------------------------------------
[INFO] BUILD FAILURE
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 9.718 s
[INFO] Finished at: 2019-04-14T16:04:55-04:00
[INFO] ------------------------------------------------------------------------
TestRunner.java
@RunWith(Cucumber.class)
@CucumberOptions(
plugin = {"pretty", "json:target/cucumber-reports/cucumber.json"},
glue = {"com.example.stepdefs"},
features = {"src/test/resources/features"})
public class TestRunner {
}
DemoApplicationTests.java
@RunWith(SpringRunner.class)
@SpringBootTest
public abstract class DemoApplicationTests {
}
GoogleCalcStepDefinition.java
@Ignore
public class GoogleCalcStepDefinition extends DemoApplicationTests {
WebDriver driver;
GoogleSearchPage googleSearchPage;
@Given("^I open Google$")
public void I_open_google() {
this.driver = BrowserConfig.getWebDriver();
this.googleSearchPage = PageFactory.initElements(driver, GoogleSearchPage.class);
driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);
driver.get("https://www.google.com");
}
@When("^I enter \"([^\"]*)\" in search textbox$")
public void I_enter_in_search_textbox(String input) {
googleSearchPage.searchBox.sendKeys(input); //passing 2+2 and 5*5 here
googleSearchPage.searchBtn.click();
}
@Then("^I should get result as \"([^\"]*)\"$")
public void I_should_get_correct_result(String expectedResult) {
String result = googleSearchPage.calculatorTextBox.getText();
assertEquals(result, expectedResult); //Verify that result of 2+2 is 4 and 5*5 is 25
BrowserConfig.releaseResources(driver);
}
}
BingSearchStepDefinition.java
@Ignore
public class BingSearchStepDefinition extends DemoApplicationTests {
WebDriver driver;
BingSearchPage bingSearchPage;
@Given("^I open Bing$")
public void I_open_bing() {
this.driver = BrowserConfig.getWebDriver();
this.bingSearchPage = PageFactory.initElements(driver, BingSearchPage.class);
driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);
driver.get("https://www.bing.com");
}
@When("^I enter \"([^\"]*)\" in search textbox$")
public void I_enter_in_search_textbox(String input) {
bingSearchPage.searchBox.sendKeys(input); //passing searchTerm = Ostrich
bingSearchPage.searchBtn.click();
}
@Then("^I should get result as \"([^\"]*)\"$")
public void I_should_get_correct_result(String input) {
String result = driver.getTitle();
System.out.println("result: " + result);
assertEquals(result, input); //Verify that result = Bing - <searchTerm>
BrowserConfig.releaseResources(driver);
}
}
google.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
Background: Do some arithmetic on Google
Given I open Google
Scenario: Addition
When I enter "2+2" in search textbox
Then I should get result as "4"
Scenario: Multiplication
When I enter "5*5" in search textbox
Then I should get result as "25"
bing.feature
Feature: Check search in Bing
In order to verify that Bing search works correctly
As a user of Bing
I should be able to get correct search result
Scenario: Bird
Given I open Bing
When I enter "Ostrich" in search textbox
Then I should get page title result as Bing " - Ostrich"
pom.xml
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.6.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<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>
答案 0 :(得分:1)
您的两个运行器类都扩展了'DemoApplicationTests.java',它本身是一个@SpringBootTest。 黄瓜无法确定在启动时要加载哪个SpringBootContext ..从stepdefs中删除该类的扩展,而让'TestRunner'扩展为'DemoApplicationTests'