Selenium2Library系列 keywords 之 _SelectElementKeywords 之 list_selection_should_be(self, locator, *items)

时间:2021-04-01 16:21:31
     def list_selection_should_be(self, locator, *items):
"""Verifies the selection of select list identified by `locator` is exactly `*items`. If you want to test that no option is selected, simply give no `items`. Select list keywords work on both lists and combo boxes. Key attributes for
select lists are `id` and `name`. See `introduction` for details about
locating elements.
"""
items_str = items and "option(s) [ %s ]" % " | ".join(items) or "no options"
self._info("Verifying list '%s' has %s selected." % (locator, items_str))
items = list(items)
self.page_should_contain_list(locator)
select, options = self._get_select_list_options_selected(locator)
if not items and len(options) == 0:
return
selected_values = self._get_values_for_options(options)
selected_labels = self._get_labels_for_options(options)
err = "List '%s' should have had selection [ %s ] but it was [ %s ]" \
% (locator, ' | '.join(items), ' | '.join(selected_labels))
for item in items:
if item not in selected_values + selected_labels:
raise AssertionError(err)
for selected_value, selected_label in zip(selected_values, selected_labels):
if selected_value not in items and selected_label not in items:
raise AssertionError(err)

方法名:list_selection_should_be(self, locator, *items)

相似方法:

公共方法 验证指定labels或values的选项应该被选中

接收参数:locator,*items(labels/values)

13行:使用page_should_contain_list(self, locator, message='', loglevel='INFO')方法,验证select list 存在于当前页面

14行:使用_get_select_list_options_selected(self, locator)方法返回Select 元素对象和选中options数组

17行:使用_get_values_for_options(self, options)方法返回选中options的values数组

18行:使用_get_labels_for_options(self, options)返回选中options的labels数组

20、21行: 遍历所传入的items,判断item是否为选中项

24、25行: 遍历当前选中项的value和label,如果不在传入items中存在则报错

使用:

Selenium2Library系列 keywords 之 _SelectElementKeywords 之 list_selection_should_be(self, locator, *items)

输出结果:

FAIL : List 'id=creOutTime' should have had selection [ 0 | 1 ] but it was [ 无逾期 | 无记录(征信空白) | 贷款或贷记卡当前逾期金额>=300元 ]