我正在测试的应用程序有很多页面对象。页面对象具有页面上的所有元素。我在每个页面对象上编写validate_fields
方法,以便当另一个测试人员导航到该页面时,他们可以调用此validate_fields
方法来验证应该在页面上的所有项目是实际上在页面上。
我遇到的问题是这个validate_fields
函数真的很长,而且它只是一堆
if not x.is_displayed():
self.problems.append("The item X is missing from the page")
问题是我们在测试结束时断言的问题清单。
下面是一个代码示例,有没有办法简化这个?
def validate_fields(self):
if not self.el_page_header.is_displayed():
self.problems.append("The Page Header is missing")
if not self.el_preferred.is_displayed():
self.problems.append("The Preferred check box is missing")
if not self.el_address.is_displayed():
self.problems.append("The Address 1 field is missing")
if not self.el_address_2.is_displayed():
self.problems.append("The Address 2 field is missing")
if not self.el_address_3_city.is_displayed():
self.problems.append("The Address 3 City field is missing")
if not self.el_address_4_state.is_displayed():
self.problems.append("The Address 4 State field is missing")
if not self.el_address_5_zip_code.is_displayed():
self.problems.append("The Address 5 Zip Code field is missing")
if not self.el_contact.is_displayed():
self.problems.append("The Contact field is missing")
if not self.el_phone.is_displayed():
self.problems.append("The Phone field is missing")
if not self.el_phone_ext.is_displayed():
self.problems.append("The Phone Extension field is missing")
if not self.el_fax.is_displayed():
self.problems.append("The Fax number field is missing")
...
答案 0 :(得分:2)
您可以将所有这些小部件及其人类可读的名称放入列表中:
def validate_fields(self):
widgets = [(self.el_page_header, "Page Header"),
(self.el_preferred, "Preferred check box"),
... and many more... ]
for widget, name in widgets:
if not widget.is_displayed():
self.problems.append("The %s is missing" % name)