如何解决Cucumber JVM - 如何正确重用在其他要素类中编写的方法?
我有 2 个特征文件(FeatureA
和 FeatureB
)。这些特性文件中的前 2 个步骤完全相同,所以我只想编写一次实现,然后可以被其他特性文件重复使用。
Feature A
Given I use "<browser>"
When I navigate to the "www.google.com"
Then I will be able to do A
Feature B
Given I use "<browser>"
When I navigate to the "www.google.com"
Then I will be able to do B
我有父类Base
public class Base {
public LandingPage landingPage;
public synchronized void initiate(String browser) {
// Do usual Chrome / Firefox WebDriver instantiation
this.landingPage = new LandingPage(); // Instantiate landingPage
}
public synchronized void navigateTo(String URL,Scenario scenario) throws InterruptedException {
driver.get(URL);
}
}
我实现了下面的 Feature A
public class FeatureA extends Base {
@Given("I use {string}")
public void i_use(String browser) {
initiate(browser,scenario); // Call the method in parent class Base. This should instantiate this.landingPage = new LandingPage();
}
@When("I navigate to the {string}")
public void i_navigate_to_the(String URL) throws InterruptedException {
navigateTo(URL,scenario);
}
@Then("Then I will be able to do A")
public void then_i_will_be_able_to_do_A() {
//Do something specific to Feature A only
}
}
我在下面实现了 Feature B
。 (我在 Feature A
中重复使用第 1 步和第 2 步,因此不需要在 Feature B
中复制它)
public class FeatureB extends Base {
@Then("Then I will be able to do B")
public void then_i_will_be_able_to_do_B() {
//Do something specific to Feature B only
log.debug("landingpage : " + super.landingPage); // Null. Not sure why?
}
}
当我运行 Feature A
小黄瓜文件时,一切都很好。
当我运行 Feature B
小黄瓜文件时,变量/引用 super.landingPage
返回我 null
。
我运行调试模式,可以看到 initiate(browser,scenario);
已被调用,因此 public LandingPage landingPage;
已被其他类 (Class FeatureA
) 实例化,在步骤之前在 Feature B
中执行。但是Class FeatureB
看到null
那么,我如何使用位于功能 A 中的方法并在功能 B、C、D 中重新使用它?
解决方法
您可以通过依赖注入来实现这一点。依赖注入允许您组合更小的组件的步骤定义,这些组件可以通过其他组件的构造函数注入。
最简单的入门方法是使用 cucumber-picocontainer
。
<dependencies>
[...]
<dependency>
<groupId>io.cucumber</groupId>
<artifactId>cucumber-picocontainer</artifactId>
<version>${cucumber.version}</version>
<scope>test</scope>
</dependency>
[...]
</dependencies>
public class WebDriverContainer {
private final Webdriver delegate;
// Create webdriver instance here
// as needed by invoking a method from your step definitions.
// or something more advanced: https://github.com/cucumber/cucumber-jvm/tree/main/picocontainer#step-scope-and-lifecycle
// note that before and after hooks can also be used here to do the clean up/setup.
}
public class FeatureA {
private WebDriverContainer webdriver;
public FeatureA(WebDriverContainer webdriver){
this.webdriver = webdriver;
}
// Use webdriver container in your steps
}
public class FeatureB {
private WebDriverContainer webdriver;
public FeatureB(WebDriverContainer webdriver){
this.webdriver = webdriver;
}
// Use webdriver container in your steps
}
https://github.com/cucumber/cucumber-jvm/tree/main/picocontainer
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。