【问题标题】:Make step definition dynamic to handle any incoming text使步骤定义动态处理任何传入的文本
【发布时间】:2020-12-15 02:04:33
【问题描述】:

我必须针对两个 url 运行 test1.feature 文件。在其中一个 url 中,我有一个名为 EIN 的字段,但在第二个 url 中,相同的字段名为 ABN 我们如何使步骤定义动态化以处理第二个字符串中的任何文本。

网址 1:https://testsite.us.com/student

该字段被命名为“EIN”

网址 2:https://testsite.au.com/student

该字段被命名为“ABN”

test1.feature

And I type "11 234 234 444" in "ABN" field

step_definition

//在文本框输入框中输入文本值

Then('I type {string} in {string} field', (textval, textboxname) => {
  switch (textboxname) {
    case "Email":
    case "Work Phone":
    case "ABN":
    case "EIN":
    case "ACN":
      cy.get('#profile_container').parent().find('.fieldHeaderClass').contains(textboxname)
        .next().find('input')
        .clear({force:true})
        .type(textval, { force: true });
      break;
      
    //final else condition
    default:
      cy.get('#profile_container').parent().find('.fieldHeaderClass').contains(textboxname)
        .next()
        .type(textval, { force: true });

  }
});

【问题讨论】:

    标签: cypress cypress-cucumber-preprocessor


    【解决方案1】:

    首先是一个很好的提示:利用页面对象(PO​​M 设计模式)。页面对象是您在包含所有选择器代码 (cy.get(......)) 的 stepdefinitions 文件中导入的对象(在您的功能和 stepdifinitions 之外的第三个 .js 文件中)。您不希望在您的步骤定义中使用选择器代码。使其混乱且难以阅读。

    关于您的问题:如果您想对(各种)输入值重复相同的逻辑。为什么不只写你的逻辑(所以没有长 case 语句)并使用 Scenario Outline 为不同的输入重复你的步骤?如果您的逻辑每次都必须不同,那么甚至不必费心解决这个问题。那么你应该简单地为不同的逻辑编写不同的步骤..

    以下是场景大纲的示例(在 .feature 内):

    Scenario Outline: Scenario Outline name  
        Given I type "<textval>" in "<textboxname>" field
         Examples:
          | textval            | textboxname |
          | some_value         | ABN         |
          | some_other_value   | EIN         |
          | some_other_value_2 | email       |
          | some_other_value_3 | ACN         |
          | some_other_value_4 | Work Phone  |
    

    结论:使用场景大纲重复逻辑。如果不同的输入需要不同的逻辑,那就再写一个stepdefinition,不要尝试将不同的逻辑合并到一个step中。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-11-21
      • 2017-03-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-10-18
      相关资源
      最近更新 更多