如何从数据表中获取值来循环多个测试场景?

问题描述 投票:0回答:1

我正在尝试使用数据表循环多个场景,但我的步骤定义没有从数据表获取值

功能文件:


  Scenario Outline: Try login with invalid <username> and <password> should throw an error <message>
  Given I am on the login screen.
  When I login with a <username> and <password>
  Then the system should throw a relevant error <message>
  Example: 
          | username                      | password        | message                     |
          | aminster                      | test@12         | Invalid Login. Try again.   |
          | s                             | 3               | Invalid Login. Try again.   |

我想在多个场景下验证登录屏幕上的错误是否存在无效数据

以下是我的步骤定义

Given("I am on the login screen.", () => {
  cy.visit("https://demotest.com/login#login");
  cy.wait(1000);
});

When(/^I login with a (.+) and (.+)$/, (username, password) => {
    cy.get('input[id="login_email"]').type(username);
    cy.get('input[id="login_password"]').type(password);
    cy.get('button[type="submit"]').eq(0).click();
    cy.wait(5000);
});

Then(/^the system should throw a relevant error (.+)$/, (message) => {
  cy.get('button[type="submit"]').eq(0).should('include', message)
})

但无法从数据表中获取用户名、密码和消息的值。

datatable cucumber cypress
1个回答
0
投票

您的特征文件和步骤定义需要更新才能使场景大纲发挥作用。

  1. 应该是“示例”而不是“示例”
  2. 在示例表功能文件中用双引号“”将值括起来,并更新步骤定义文件以仅捕获为字符串,无需使用正则表达式。

功能文件:

    Scenario Outline: Try login with invalid <username> and <password> should throw an error <message>
       Given I am on the login screen.
       When I login with a <username> and <password>
       Then the system should throw a relevant error <message>
       Examples:
           | username   | password  | message                     |
           | "aminster" | "test@12" | "Invalid Login. Try again." |
           | "s"        | "3"       | "Invalid Login. Try again." |

步骤定义文件:

Given("I am on the login screen.", () => {
  cy.visit("https://demotest.com/login#login");
  cy.wait(1000);
});

When("I login with a {string} and {string}", (username, password) => {
    cy.get('input[id="login_email"]').type(username);
    cy.get('input[id="login_password"]').type(password);
    cy.get('button[type="submit"]').eq(0).click();
    cy.wait(5000);
});

Then("the system should throw a relevant error {string}", (message) => {
  cy.get('button[type="submit"]').eq(0).should('include', message)
})
© www.soinside.com 2019 - 2024. All rights reserved.