【问题标题】:Is it possible to have a single step definition to handle the cases mentioned below from `a to d`是否可以有一个步骤定义来处理下面提到的从 `a 到 d` 的情况
【发布时间】:2021-07-29 00:11:19
【问题描述】:

在我们的 BDD 自动化框架中,我们使用的是 Cypress Cucumber 预处理器库。我们计划为以下情况编写通用步骤定义。是否可以有一个步骤定义来处理a to d 中提到的以下情况。 现在我打算在两步定义中处理这些案例,我们可以让它更通用,即在一个步骤定义中包含所有内容吗?

a) Then I "should not" see the value "Some_Value" in the "Reports_Logs" table 
b) Then I "should" see the value "Some_Value" in the "Reports_Logs" table

c) Then I "should not" see the value in the "Users_Main" table ( in this case, I will read the value from the local storage )
d) Then I "should" see the value in the "Users_Main" table ( in this case, I will read the value from the local storage )

step defintion:

/* 这个泛型函数用来验证里面所有的table td value assertions */

Then('I {string} see the value {string} in the {string} table', (checkCase, value, tableDatacy) => {
  if(checkCase === "should"){
    cy.get(`[data-cy=${tableDatacy}]`).find('tbody').find('tr').each(($tr) => {
      cy.wrap($tr).find('td').each(($td) => {
        const textName = Cypress.$($td).text();
        switch (value) {
          case textName.trim():
            console.log("Condition got matched ...!!! " + textName.trim());
            cy.wrap($tr).find('td').contains(textName.trim()).should('be.visible');
            break;
        }
      });
    });
  } else if (checkCase === "should not") {
     // rest of the check with this condition..
  }
});

【问题讨论】:

    标签: cypress bdd cypress-cucumber-preprocessor


    【解决方案1】:

    您的步骤 C 和 D 应该检查一个值,如果您准确指定应该或不应该存在哪些值,这对读者来说会更有意义,类似于 A 和 B。

    因此,如果您重新编写步骤 C 和 D,如下所示:

    c) 然后我“不应该”在“Users_Main”中看到值“Some_Value” 表
    d) 然后我“应该”在 “Users_Main”表

    然后您需要更改您的步骤定义以检查“Users_Main”表并可以从本地存储中读取。

    这样您只需一步即可涵盖您提到的所有 4 个步骤,这是定义步骤的更好方法,因为您可以准确检查哪些值应该/不应该存在。

    【讨论】:

    • 同意 Jerome 的观点,在任何情况下 c & d 都不会匹配 Then(),因为它们缺少所需数量的参数(缺少值)。因此,您至少需要两个步骤定义。
    • 在步骤 C 和 D 中,我无法从功能文件中提供“Some_Value”,因为它每次都会创建唯一值。例如:Customer_dTghed23BZv。因此,在创建值时,我保存在 localStorage 中,然后从 localStorage 中读取以在表中声明它们。
    • 每次创建并保存唯一值后。我将它们存储在 localStorage 中,并在同一场景或下一个场景中检索它们,以便在表中编辑或断言它们。所以我不能将它们作为功能文件中的第三个参数。
    【解决方案2】:

    步骤中的代码可以改进。

    Should条件

    cy.get(`[data-cy="${tableId}"] tbody td`)
      .filter(`:contains(${value})`)
      .should('be.visible')
      .log(`Success: "${value}" is in the table`)
    

    Should not条件

    cy.get(`[data-cy="${tableId}"] tbody td`).then($cells => {
      const $matchingCells = $cells.filter(`:contains(${value})`)
      if ($matchingCells.length) {
        throw `Failed: "${value}" is in the table but should not be`
      } else {
        cy.log(`Success: "${value}" is not in the table`)
      }
    })
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2019-10-29
      • 1970-01-01
      • 2021-09-20
      • 2011-11-20
      • 1970-01-01
      • 1970-01-01
      • 2017-03-03
      相关资源
      最近更新 更多