首页 > 解决方案 > 使步骤定义动态处理任何传入的文本

问题描述

我必须test1.feature针对两个 url 运行文件。在其中一个 url 中,我有一个名为 的字段EIN,但在第二个 url 中,名为ABN How can we make step definition dynamic 处理第二个字符串中的任何文本的相同字段。

网址 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 });

  }
});

标签: cypresscypress-cucumber-preprocessor

解决方案


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

关于您的问题:如果您想为(各种)输入值重复相同的逻辑。为什么不只写你的逻辑(所以没有长 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  |

结论:使用场景大纲重复逻辑。如果不同输入的逻辑需要不同,则编写另一个步骤定义,不要尝试将不同的逻辑组合和剪辑到一个步骤中。


推荐阅读