在页面对象模型实现期间发生某些异常。我做错了什么?

时间:2017-07-26 14:16:49

标签: python python-3.x automation webdriver

我想在自动化项目中实现页面对象模型的原理,以便将其转化为真正的框架。

测试对象是设备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都在同一个Package下,称为WEB_Pages。 Simple_Test位于一个单独的Package中,称为Test_Cases。

1 个答案:

答案 0 :(得分:0)

我终于设法理解了我的项目中发生的异常。这个问题与我没有构建(我希望我使用正确的术语)Main_Screen_POM的事实相关,而是我试图在其中激活一个函数。

另一个错误是在调用函数期间,我将一个变量(浏览器)导出到 Get_Rx_Pwr 函数,该函数没有得到任何参数。

解决了这些问题之后,我发现了另一个与 Get_Rx_Pwr 功能中的第一行相关的问题。那里的初始化没有做好。