如何使用behave context.table与键值表?

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

我看到当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 "[email protected]" 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

我怎么能在behave中使用下面的表格呢?

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

我的步骤定义会是怎样的?

python bdd python-behave
1个回答
0
投票

要使用垂直表而不是水平表,你需要把每一行作为自己的字段来处理。该表仍然需要一个头行。

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')
© www.soinside.com 2019 - 2024. All rights reserved.