0

在我们的 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:

/* 这个通用函数用于验证里面所有的表 td 值断言 */

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..
  }
});
4

2 回答 2

0

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

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

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

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

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

于 2021-07-29T08:54:33.343 回答
0

您在该步骤中拥有的代码可以改进。

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`)
  }
})
于 2021-07-29T10:46:17.923 回答