2017-07-26 56 views
0

我想在我的自動化項目中實現頁面對象模型的原理,以便將它變成一個真正的框架。頁面對象模型實現期間發生某些異常。我究竟做錯了什麼?

被測對象是一個設備WEB GUI。該設備的GUI可以分爲幾個框架。每個框架都包含一些元素。爲了在POM(頁面對象模型)中表示設備GUI,我打算爲我的GUI中的每個框架構建一個單獨的文件(POM)。

只有其中一個框架會有很多方法,其餘的大部分都會包含元素(順便我想知道根據POM原理實現沒有方法的框架是否正確? )。

在下面的例子中,我開始描述幾個框架,並且我寫了一個與它們交互的測試用例。我的問題是,在測試用例腳本(Simple_Test.py)中的某個點,我可能會遇到異常,我不知道爲什麼。我調試了Simple_Test.py,發現只要我達到Pwr, OSNR = Main_Screen.Get_Rx_Pwr(browser),下一步就是execute_code(i, browser),然後下一步是browser.quit()(在except:下)。

有人可以幫我解決這個問題嗎?

以下是相關的腳本:

Simple_Test.py

from selenium import webdriver 
from WEB_Pages.Login_POM import Login_Page 
#from WEB_Pages.Main_Screen_POM.Main_Screen import Get_Rx_Pwr 
from WEB_Pages.Main_Screen_POM import * 


def main(): 
    i = 0 
    while True: 
     i = i +1 
     profile = webdriver.FirefoxProfile() 
     profile.accept_untrusted_certs = True 
     browser = webdriver.Firefox(firefox_profile = profile) 
     browser.implicitly_wait(20) # Implicit wait 
     try: 
      execute_code(i, browser) 
      browser.quit() 
      if i == 2: 
       break 
     except: 
      browser.quit() 



def execute_code(i, browser): 
    browser.get('http://10.0.1.131') 

    login = Login_Page(browser) 

    login.Login('admin', 'admin') 

# Port = Device_Panel_Frame.Port_19 

    Pwr, OSNR = Main_Screen.Get_Rx_Pwr(browser) 

# print (Port) 
    print (Pwr) 
    print (OSNR) 

# print('The measured Uplink 1 Power is', Pwr) 
# print('The measured Uplink 1 OSNR is', OSNR) 

if __name__ == '__main__': 
    main() 

Login_POM.py

from selenium import webdriver 

class Login_Page(object): 
    ''' 
    classdocs 
    ''' 

    def __init__(self, driver): 
     ''' 
     Constructor 
     ''' 
     self.driver = driver 

    def Login(self, userName, pas): 
     user_name = self.driver.find_element_by_id('u_name_box') 
     user_name.send_keys(userName) 

     password = self.driver.find_element_by_id('u_pass_box') 
     password.send_keys(pas) 

     login_button = self.driver.find_element_by_id('login_but') 
     login_button.click() 

Device_Panel_POM.py

from selenium import webdriver 



class Device_Panel_Frame(object): 
    ''' 
    classdocs 
    ''' 

    def __init__(self, driver): 
     ''' 
     Constructor 
     ''' 
     self.driver = driver 

     self.driver.switch_to.default_content() 


     self.driver.switch_to.frame('box_menu') 
     self.driver.switch_to.frame('box_menu') 
     Port_19 = self.driver.find_element_by_id('Port-19') 
     Port_19.click() 

Main_Screen_POM.py

from WEB_Pages.Device_Panel_POM import Device_Panel_Frame 

class Main_Screen(object): 
    ''' 
    classdocs 
    ''' 


    def __init__(self, driver): 
     ''' 
     Constructor 
     ''' 
     self.driver = driver 


    def Get_Rx_Pwr(self): 
     Device_Panel_Frame.__init__(self, self.driver).Port_19 

     self.driver.switch_to.default_content() 

     # Show the Optic Module information TAB 
     self.driver.switch_to.frame('main_body') 
     CFP2_Info = self.driver.find_element_by_id('tab_XFP') 
     CFP2_Info.click() 

     # Collect the Rx Pwr from the CFP2 Info screen 
     self.driver.switch_to.frame('config_port') # Move to the inner frame that holds all the tables 
     #browser.find_element_by_class_name('table_round_corner') 
     Rx_Pwr = self.driver.find_element_by_xpath('html/body/form/div[1]/div/table/tbody/tr[2]/td[2]') # Take the Rx Pwr according to its Xpath 
     # print (Rx_Pwr.text) # print the Rx Pwr result to screen 
     RcvPwr = Rx_Pwr.text 

     # Collect the OSNR measurement from the CFP2 Info screen 
     OSNR = self.driver.find_element_by_xpath('html/body/form/div[1]/div/table/tbody/tr[4]/td[2]') 
     OSNR_Lvl = OSNR.text 

     return RcvPwr, OSNR_Lvl 

順便說一句,腳本:Device_Panel_POM,Login_POM和Main_Screen_POM都在被稱爲WEB_Pages同一個包。 Simple_Test位於一個名爲Test_Cases的單獨包中。

回答

0

我終於理解了我的項目中發生的異常。這個問題與我沒有構建(我希望我使用正確的術語)Main_Screen_POM相反,而是我試圖激活其中的一個功能。

另一個錯誤是在調用函數的過程中,我將一個變量(瀏覽器)導出到Get_Rx_Pwr函數,該函數沒有得到任何參數。

修復這些問題後,我發現另一個與Get_Rx_Pwr函數中的第一行有關的問題。初始化沒有完成。

相關問題