Cucumber,Java Picocontainer/构造函数注入:TestContext&;ScenarioContext),如何在多个步骤定义之间共享多个数据
问题: 如何使用构造注入、依赖注入/Picocontainer在多个步骤定义和状态(如果需要)之间共享多个变量/数据 背景:我们有一个非常大的步骤定义,管理起来越来越困难。因此,我们决定使用这个新的自动化库将步骤定义拆分为多个小定义 堆栈:硒,爪哇,黄瓜,Junit,Picocontainer 为了实现上述目标,我们在各种网站和stackoverflow讨论中寻找了很多建议,有很多建议,比如使用依赖注入(Picocontainer)、使用构造函数注入、spring等 在浏览了所有这些建议和页面之后,我们在stackoverflow中的一个位置/一个答案/一个页面发现了一些灰色区域(在上面的问题中提到),这些灰色区域在任何地方都没有得到回答,因此我将分享这个示例,以获得更多信息,帮助初学者和所有人。 项目文件的结构:Cucumber,Java Picocontainer/构造函数注入:TestContext&;ScenarioContext),如何在多个步骤定义之间共享多个数据,java,dependency-injection,cucumber,picocontainer,Java,Dependency Injection,Cucumber,Picocontainer,问题: 如何使用构造注入、依赖注入/Picocontainer在多个步骤定义和状态(如果需要)之间共享多个变量/数据 背景:我们有一个非常大的步骤定义,管理起来越来越困难。因此,我们决定使用这个新的自动化库将步骤定义拆分为多个小定义 堆栈:硒,爪哇,黄瓜,Junit,Picocontainer 为了实现上述目标,我们在各种网站和stackoverflow讨论中寻找了很多建议,有很多建议,比如使用依赖注入(Picocontainer)、使用构造函数注入、spring等 在浏览了所有这些建议和页面之
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类下
有人能建议一种方法,在这个框架之间添加一个类,解决在多个定义之间共享不同数据类型的多个数据的问题吗?Cumber不打算这样做。一种情况的结果不应作为另一种情况的基础。相反,您需要一个
给定的步骤来模拟其他场景所做的事情
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
# Put the product in the database
Given a product named "Jump Rope" exists
# Get the product by name, go to product page and add to cart
When the user adds the "Jump Rope" product to their shopping cart
# Assert
Then System should display all related info for user to verify
场景#3应通过在双引号内指定名称将产品放入系统中。When
步骤将使用户转到“产品详细信息”页面并单击“添加到购物车”按钮。在这之后,您现有的步骤可以做出断言。你好,格雷格,谢谢您的建议。我将尝试此解决方案。但我在上看到了共享的解决方案,此解决方案已用于两个步骤定义之间的1个shareddata。我的数据是字符串数组,它不适用于此解决方案,请分享您对此解决方案的观点。