2

问题:如何使用构造注入、依赖注入/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 类下。

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

4

1 回答 1

2

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步骤可以做出断言。

于 2019-10-18T14:12:36.140 回答