gpt4 book ai didi

java - Cucumber, Java Picocontainer/Constructor injection : TestContext & ScenarioContext),如何在多个步骤定义之间共享多个数据

转载 作者:行者123 更新时间:2023-11-30 09:59:57 25 4
gpt4 key购买 nike

问题:如何使用构造注入(inject)、依赖注入(inject)/Picocontainer 在多个步骤定义之间共享多个变量/数据以及状态(如果需要)。

背景:我们有一个非常大的步骤定义,管理它变得越来越困难。因此,我们决定使用这个新的自动化基础将步骤定义拆分为多个小定义。

堆栈:Selenium、Java、Cucumber、Junit、Picocontainer。

为了实现上述目标,我们在各种网站和 stackoverflow 讨论中寻找了很多建议,有很多建议,例如使用依赖注入(inject) (Picocontainer)、使用构造函数注入(inject)、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

通过在步骤定义中注入(inject)上述 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.
}

我们想在第 2 个场景中访问用户选择的产品名称和 ID,并在第 3 个场景中进行验证。第二个场景在 ProductStepDefinition 类下,同一特征文件的第三个场景在 PaymentStepDefintion 类中。

有人可以建议一种方法在这个框架之间添加一个类,可以解决在多个定义之间共享不同数据类型的多个数据的问题

最佳答案

Cucumber 不应该以这种方式工作。一种情况的结果不应用作另一种情况的基础。相反,您需要一个 Given 步骤来模拟其他场景所做的。

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 步骤会将用户带到产品详细信息页面并单击“添加到购物车”按钮。之后,您现有的 Then 步骤可以进行断言。

关于java - Cucumber, Java Picocontainer/Constructor injection : TestContext & ScenarioContext),如何在多个步骤定义之间共享多个数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58442950/

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