简体   繁体   中英

Certain exception occurs during Page Object Model implementation. What am I doing wrong?

I want to implement the principle of Page Object Model in my automation project in order to turn it into a real Framework.

The tested object is a device WEB GUI. This device GUI can be separated into frames. Each of the frame includes some elements. In order to represent the device GUI in POM (Page Object Model) I plan to construct a separate file (POM) to each of the frames in my GUI.

Only one of these frames is going to have a lot of methods, all the rest will mostly include elements (by the way I would like to know if it is right to implement the frames without methods according to the POM principle ?).

In the following example I started to characterize several of the frames and I have written a Test Case that will interact with them. My problem is that at a certain point in the Test Case script (Simple_Test.py) I possibly get into an exception and I don't know why. I have debugged Simple_Test.py and found out that whenever I reach to Pwr, OSNR = Main_Screen.Get_Rx_Pwr(browser) the next step would be execute_code(i, browser) and then the next step is the browser.quit() (under the except: ).

Can somebody please help me to solve this issue ?

The following are the relevant scripts:

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

By the way, the scripts: Device_Panel_POM, Login_POM and Main_Screen_POM are all under the same Package which is called WEB_Pages. Simple_Test is in a separate Package which is called Test_Cases.

I finally managed to understand the exception that occurred in my project. The issue was related to the fact that I didn't construct (I hope that I am using the right terminology) the Main_Screen_POM and instead I tried to activate a function within it.

Another mistake was that during the call to the function I exported a variable (browser) to the Get_Rx_Pwr function which doesn't get any arguments.

After fixing these issues I revealed another issue that related to the first line in the Get_Rx_Pwr function. The initialization there wasn't done right.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM