gpt4 book ai didi

java - 无法使用具有多个步骤文件的 Cucumber 配置 Spring Boot

转载 作者:行者123 更新时间:2023-12-01 14:33:35 29 4
gpt4 key购买 nike

我有一个 Cucumber-Selenium使用 Spring Boot 编写的基于测试的测试.问题是如果我只有一步定义文件 GoolgeCalcStepDefinition.java然后程序可以正常工作并且测试通过没有任何问题但是一旦我添加 BingSearchStepDefinition.java连同功能文件,然后我收到以下错误。

我在 google 上搜索了如何配置 Spring BootCucumber但大多数在线可用的示例/文章仅显示一个步骤定义文件。

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>

最佳答案

您的两个运行者类都扩展了 DemoApplicationTests.java这本身就是一个 @SpringBootTest .
cucumber 无法确定哪个SpringBootContext在它启动时加载.. 从您的 stepdef 中删除类扩展并具有 TestRunner延长 DemoApplicationTests反而

关于java - 无法使用具有多个步骤文件的 Cucumber 配置 Spring Boot,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55679667/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com