问题:如何使用构造注入、依赖注入/Picocontainer 在多个步骤定义之间共享多个变量/数据以及状态(如果需要)。
背景:我们有一个非常大的步骤定义,并且越来越难以管理。因此,我们决定使用这个新的自动化基础将步骤定义拆分为多个小定义。
堆栈:Selenium、Java、Cucumber、Junit、Picocontainer。
为了实现上述目的,我们在各种网站和stackoverflow讨论中寻找了很多建议,有很多建议,比如使用依赖注入(Picocontainer),使用构造函数注入,spring等。
在浏览了所有这些建议和页面之后,我们发现了一些灰色区域(在上面的问题中提到)在 stackoverflow 的一个位置/一个答案/一个页面的任何地方都没有得到回答,所以我分享这个例子以获得更多信息将帮助初学者和每个人。项目文件结构:
src
|->features
|----- login.feature
|----- product.feature
|----- payment.feature
|->java
|-->pagefactory
| |----- LoginPage.class
| |----- ProductPage.class
| |----- PaymentPage.class
|-->picoHelper
| |-----TestContext.class
|-->stepDefinition
| |-----LoginStepDef.class
| |-----SearchStepDef.class
| |-----ProductStepDef.class
| |-----PaymentStepDef.class
|-->helpers
| |-->wait
| |-----waitHelper.class
| |-->util
| |-----DriverFactoryManager.class
| |-----PageFactoryManager.class
通过在步骤定义中注入上述 testcontext 构造函数,我们能够拆分我们的大定义,并且大多数测试用例都可以正常工作。但是,当我们尝试使用在两步定义之间具有一些共享数据的方法时,问题就来了。
功能文件:
Feature: Verify Product Names and ID are being transferred to Payment Page
This feature will be used to validate Data
Scenario: Successful Login with Valid Credentials
Given User navigate to homepage of portal
When user enter valid credentials
Then user should be redirected to homepage
Scenario: To select the product
Given User is on product page
When User select product
And filterit via productsearch
Then user should be able to search this product
Scenario: Payment
Given User has selected product
When User click add to cart
Then System should display all related info for user to verify
TestContext 看起来像:
public class TestContext {
private DriverFactoryManager driverFactoryManager;
private PageObjectManager pageObjectManager;
public ScenarioContext scenarioContext;
public TestContext() {
driverFactoryManager = new DriverFactoryManager();
pageObjectManager = new PageObjectManager(driverFactoryManager.getDriver());
}
public DriverFactoryManager getDriverFactoryManager() {
return driverFactoryManager;
}
public PageObjectManager getPageObjectManager() {
return pageObjectManager;
}
}
步骤定义:LoginStepDef
public class LoginStepDef {
LoginPage lp;
TestContext testContext;
private Logger log = LoggerHelper.getLogger(LoginStepDef.class);
public LoginStepDef(TestContext testContext) throws IOException {
lp = testContext.getPageObjectManager().getLoginPage();
}
//
methods to login to portal
步骤定义:ProductStepDefs
public class ProductStepDef {
private Logger log = LoggerHelper.getLogger(ProductStepDef.class);
TestContext testContext;
private LoginPage lp;
private ProductPage objPM;
String[] prodCodeName = new String[2];
String[] productDetails;
public ProductStepDef(TestContext testContext) {
this.testContext = testContext;
lp = testContext.getPageObjectManager().getLoginPage();
objPM = testContext.getPageObjectManager().getProductPage();
@Then("^user should be able to search this product$")
public void advancedSearchProduct {
objPM.advancedSearchProduct(searchKeyword);
prodCodeName = objPM.productDataProdCodeName();
log.info("product is: " + prodCodeName[0] + ". Its name is " + prodCodeName[1]); //expected 0 to show id and 1 to show name of product
productDetails = prodCodeName;
log.info("productDetails are : " + productDetails);
}
}
步骤定义:PaymentStepDefs
public class PaymentStepDef {
Logger log = LoggerHelper.getLogger(PaymentStepDef.class);
LoginPage lp;
Product objPM;
PaymentPage objPay;
String[] prodCodeName = new String[2];
String[] productDetails;
public PaymentStepDef(TestContext testContext) {
this.testContext = testContext;
lp = testContext.getPageObjectManager().getLoginPage();
objPM = testContext.getPageObjectManager().getProductPage();
objPay = testContext.getPageObjectManager().getPaymentPage();
@Then("^System should display all related info for user to verify$")
public void verifyExportResult() {
exportInfo = objPay.ExportResult(filter1, productDetails, numberOfItems );
THis Export results, take this productDetails to perform some action to compare with various version and then give perform some validation.
}
我们想在第二种情况下访问用户选择的产品名称和 ID,并在第三种情况下进行验证。第二个场景在 ProductStepDefinition 类下,相同功能文件的第三个场景在 PaymentStepDefinition 类下。
有人可以建议一种在此框架之间添加一个类的方法,该类可以解决在多个定义之间共享不同数据类型的多个数据的问题