2015-06-02 76 views
2

我有很多我正在測試的應用程序的頁面對象。頁面對象具有頁面上的所有元素。我正在爲每個頁面對象編寫一個validate_fields方法,以便當另一個測試人員導航到頁面時,他們可以調用此方法來驗證應該在頁面上的所有項目實際上都在頁面上。簡化`if`語句的長列表

我遇到的問題是,這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") 
    ... 

回答

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) 
+0

優越的工作,非常感謝你。當然清理了我的頁面對象很多。 – DarthOpto