cypress - 使步骤定义动态处理任何传入的文本
问题描述
我必须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 });
}
});
解决方案
首先是一个很好的提示:利用页面对象(POM 设计模式)。页面对象是您在包含所有选择器代码 ( ) 的 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 |
结论:使用场景大纲重复逻辑。如果不同输入的逻辑需要不同,则编写另一个步骤定义,不要尝试将不同的逻辑组合和剪辑到一个步骤中。
推荐阅读
- json - React-Native render json from fetch
- html - 是什么让第一段和第二段向左浮动?
- javascript - 淘汰赛:表分页
- android - Filter JSON via its array's name in same fragment using Retrofit
- javascript - Javascript更改输入字段禁用按钮
- r - 在 R Shiny 中设置数据表选项
- google-cloud-platform - 什么是 Google Cloud Storage 公共读取 uri?
- jquery - jquery array.push() 出错
- excel - SUMPRODUCT 公式不起作用
- python-asyncio - 使用 fetchmany 从 pyodbc 异步提取大数据