代码之家  ›  专栏  ›  技术社区  ›  soccerway

使步骤定义动态化以处理任何传入文本

  •  0
  • soccerway  · 技术社区  · 4 年前

    我得走了 test1.feature EIN ,但在第二个url中,名为 ABN 如何使步骤定义动态化以处理第二个字符串中的任何文本。

    Url 1: https://testsite.us.com/student

    该字段名为“EIN”

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

    test1.1特性

    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 });
    
      }
    });
    
    0 回复  |  直到 4 年前
        1
  •  1
  •   Ewald    4 年前

    首先是一个很好的技巧:利用页面对象(POM设计模式)。页面对象是一个对象(在功能和步骤定义之外的第三个.js文件中),您可以将其导入包含所有选择器代码的步骤定义文件中( cy.get(......) ). 在步骤定义中不需要选择器代码。使其凌乱且更难阅读。

    关于您的问题:如果您想对(所有类型的)输入值重复相同的逻辑。为什么不把你的逻辑写成一个(所以没有所有的case语句)并使用它呢 情景大纲 要对多个输入重复您的步骤?如果你的逻辑每次都不一样,那就不要费心去解决这个问题。然后,您应该简单地为不同的逻辑编写不同的步骤。。

    下面是一个场景大纲示例(在.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  |
    

    :使用场景大纲重复逻辑。如果不同的输入需要不同的逻辑,那么请编写另一个步骤定义,不要试图将不同的逻辑组合并剪裁到一个步骤中。

    推荐文章