- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
问题:如何使用构造注入(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/
我的印象是它不是。我有三个集成测试在单独运行时成功,但在并行运行时,我得到 System.ArgumentException: An item with the same key has alread
我正在尝试在 ScenarioContext.Current 中插入一个值,但由于某种原因 ScenarioContext.Current 为空。 最佳答案 如果您在 [BeforeFeature]
尝试为使用 ScenarioContext 的 Specflow 步骤编写单元测试。此步骤试图从 ScenarioContext 中检索属性的值。是否可以模拟 ScenarioContext 以便我可
我有一个 hooks.cs 绑定(bind)文件,其中包含 BeforeTestRun、BeforeFeature 和 BeforeScenario。我需要当前功能和场景的标题以用于日志和报告目的。由
我正在使用 SpecFlow 测试 MortgageCalculator 类。我发现我有两种设置我的步骤文件的方法,一种是我有类级别的 MortgageCalculator,另一种是我使用 Scena
问题:如何使用构造注入(inject)、依赖注入(inject)/Picocontainer 在多个步骤定义之间共享多个变量/数据以及状态(如果需要)。 背景:我们有一个非常大的步骤定义,管理它变得越
我是一名优秀的程序员,十分优秀!