【问题标题】:How to use behave context.table with key value table?如何将行为 context.table 与键值表一起使用?
【发布时间】:2020-06-10 08:57:16
【问题描述】:

我看到当 BDD 中描述的表有标题时,可以从 Behave 访问 context.table 中的数据。例如:

Scenario: Add new Expense
  Given the user fill out the required fields
    | item | name  | amount |
    | Wine | Julie | 30.00  |

要访问此代码,只需:

for row in context.table:
  context.page.fill_item(row['item'])
  context.page.fill_name(row['name'])
  context.page.fill_amount(row['amount'])

这很好用,而且非常干净,但是,当我有大量输入数据行时,我必须重构代码。例如:

Given I am on registration page
When I fill "test@test.com" for email address
And I fill "test" for password
And I fill "Didier" for first name 
And I fill "Dubois" for last name
And I fill "946132795" for phone number
And I fill "456456456" for mobile phon
And I fill "Company name" for company name
And I fill "Avenue Victor Hugo 1" for address
And I fill "97123" for postal code
And I fill "Lyon" for city
And I select "France" country
...
15 more lines for filling the form

如何在行为中使用下表:

|first name | didier |
|last name  | Dubois |
|phone| 4564564564   |
So on ...

我的步骤定义如何?

【问题讨论】:

    标签: python bdd python-behave


    【解决方案1】:

    要使用垂直表而不是水平表,您需要将每一行作为其自己的字段来处理。表格还需要一个标题行:

    When I fill in the registration form with:
        | Field      | Value  |
        | first name | Didier |
        | last name  | Dubois |
        | country    | France |
        | ...        | ...    |
    

    在您的步骤定义中,遍历表格行并在您的 selenium 页面模型上调用一个方法:

    for row in context.table
      context.page.fill_field(row['Field'], row['Value'])
    

    Selenium 页面模型方法需要根据字段名做一些事情:

    def fill_field(self, field, value)
      if field == 'first name':
        self.first_name.send_keys(value)
      elif field == 'last name':
        self.last_name.send_keys(value)
      elif field == 'country':
        # should be instance of SelectElement
        self.country.select_by_text(value)
      elif
        ...
      else:
        raise NameError(f'Field {field} is not valid')
    

    【讨论】:

      猜你喜欢
      • 2019-07-15
      • 2017-03-19
      • 2020-09-13
      • 1970-01-01
      • 2014-01-02
      • 1970-01-01
      • 2020-09-11
      • 2016-04-21
      • 2015-12-09
      相关资源
      最近更新 更多