在功能文件中的 "Examples:" table of "Scenario Outline:" 中传递参数

Passing parameter in "Examples:" table of "Scenario Outline:" in a feature file

如您所见,我正在尝试从位于 config/environments Examples: table.

中的 .yml 文件中获取值

但不是获取它而是按原样发送值?

这样传参可以吗?如果是,如何?

如果不是,哪个 Ruby 或 Cucumber feature/concept 会阻止用户这样做,为什么?

Feature: Verify login of all test users
I want to verify all test users can login.
  Scenario Outline: Login as different users on the website
    Given I am on login page
    When I enter "<username>" and password
    Then I click Login button
    And I see "<user>" successfully logged in
    Examples:
    |user|username|
    |testuser1|#{FigNewton.test1_email}|
    |testuser2|FigNewton.test2_email|

查询 1 的答案: 您可以通过 Examples: table 进行参数化,但不能使用 FigNewton gem 直接传递值,因为它是 .feature 文件而不是 Ruby .rb 文件。

查询 2 的答案: 你是怎么做到的: 对用户名进行参数化和循环,并在您 steps definition 中提及找到特定用户名时要做什么。通过这个你可以很容易地参数化。

  1. 示例:

    |user|username|
    |testuser1|test1|
    |testuser2|test2|
    
  2. 步骤定义

    When(/^I enter "([^"]*)" and password$/) do |username|
    case username
    when 'test1'
      on(LoginPage).user_email = FigNewton.test1_email
    when 'test'
      on(LoginPage).user_email = FigNewton.test2_email
    end
    ....
    ....
    end
    

首先,这是一个非常糟糕的功能,更好的是

Scenario: Test Users can login
   Given there are some test users
   When the test users login
   Then all test users should be logged in

或类似的东西。功能是为了说明你想做什么和为什么,而不是你是怎么做的。

如果您执行上述操作,那么所有编程都将在步骤定义中完成。这将允许你做任何你想做的事。

你可以很容易地实现这个,例如

Given 'there are some test users' do
  @test_users = create_test_users
end

When 'the test users login' do
  @login_results = login_each(@test_users)
end

Then 'all test users should be logged in' do
  expect(check_for_errors(@login_results).count).to eql 0
end

then implement the methods you need in a step helper e.g

module TestUsersLoginStepHelper
  def create_test_users
    ...

  def login_each(users)
    users.each do 
      ...
  ...
end
World TestUsersLoginStepHelper

通过将所有工作都放在步骤定义中,您可以让生活变得更轻松,因为您可以充分利用 ruby 的全部力量来做您需要的事情

您可以在需要时在项目中使用这个 DDD 场景 - 通过使用这个我们不需要创建多个测试用例,它将从示例大纲中获取数据值。

特征文件:测试用例

场景大纲:登录到应用程序

当我输入"username>"和"password>"

然后我点击登录按钮

我看到用户成功登录

示例:

|用户名|密码|

|abc@gmail.com|Test1234!|

|abc@yahoo.com|Test1234!|


步骤定义:

When(/^I enter "([^"])" and "([^"])"$/) do |username,密码|

睡 20

on(Login).email_edit_text_element.send_keys 用户名 on(登录).password_edit_text_element.send_keys 密码

结束

然后(/^我点击登录按钮$/)做 睡 20

开(登录)。login_button_element.click

结束

然后(/^我看到用户成功登录$/) do

期待(在(登录)。account_bg_cover_element.displayed?)。到be_truthy

放'Login Success' 结束

在 ruby 文件中,您已经创建了方法,您正在步骤定义中调用该方法。 它会起作用。确保您传递的参数名称。