我正在为我公司的GUI自动化基础设施工作。我是蟒蛇新手,也是写SW的全体业余爱好者(我的专长是HW)。
我开始尝试使用python和selenium,并且我开始期待并看到某个实现问题。
以下是一个实际上是一个简短测试计划的脚本示例:
from selenium import webdriver
from datetime import datetime
import time
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 == 500:
break
except:
browser.quit()
def Cold_Restart(browser):
browser.switch_to.default_content()
browser.switch_to.frame('box_menu')
browser.switch_to.frame('box_menu')
SysBtn = browser.find_element_by_id('System')
SysBtn.click()
browser.switch_to.default_content()
browser.switch_to.frame('main_menu')
Mainten_Btn = browser.find_element_by_id('Maintenance')
Mainten_Btn.click()
browser.switch_to.default_content()
browser.switch_to.frame('main_body')
Mntn_Rst_Tab = browser.find_element_by_id('tab_restart')
Mntn_Rst_Tab.click()
browser.switch_to.frame('maint_sys')
Cold_Rst_Btn = browser.find_element_by_id('cold_restart')
Cold_Rst_Btn.click()
#In order to confirm the Alert Message I first need to switch to the alert pop-up message and then accept it
alertDialog = browser.switch_to_alert()
alertDialog.accept()
time.sleep(205)
return
def Port_Admin_Down(browser):
browser.switch_to.default_content()
browser.switch_to.frame('box_menu')
browser.switch_to.frame('box_menu')
Port_19 = browser.find_element_by_id('Port-19')
Port_19.click()
browser.switch_to.default_content()
# Show the Port Configuration TAB
browser.switch_to.frame('main_body')
CFP2_Info = browser.find_element_by_id('tab_general')
CFP2_Info.click()
browser.switch_to.frame('config_port') # Move to the inner frame that holds the configuration fields and buttons
Admin_Down_Btn = browser.find_element_by_id('red_button')
Admin_Down_Btn.click()
#In order to confirm the Alert Message I first need to switch to the alert pop-up message and then accept it
alertDialog = browser.switch_to_alert()
alertDialog.accept()
time.sleep(5)
return
def Port_Admin_Up(browser):
browser.switch_to.default_content()
browser.switch_to.default_content()
browser.switch_to.frame('box_menu')
browser.switch_to.frame('box_menu')
Port_19 = browser.find_element_by_id('Port-19')
Port_19.click()
browser.switch_to.default_content()
# Show the Port Configuration TAB
browser.switch_to.frame('main_body')
CFP2_Info = browser.find_element_by_id('tab_general')
CFP2_Info.click()
browser.switch_to.frame('config_port') # Move to the inner frame that holds the configuration fields and buttons
Admin_Down_Btn = browser.find_element_by_id('green_button')
Admin_Down_Btn.click()
#In order to confirm the Alert Message I first need to switch to the alert pop-up message and then accept it
alertDialog = browser.switch_to_alert()
alertDialog.accept()
time.sleep(5)
return
def Gui_Login(browser, URL):
browser.get(URL)
# browser.implicitly_wait(20) # Implicit wait
browser.maximize_window()
# Find the User Name text box and fill the User name
user_name_box = browser.find_element_by_id('u_name_box')
user_name_box.click()
user_name_box.send_keys('admin')
# Find the Password text box and fill the Password
user_pass_box = browser.find_element_by_id('u_pass_box')
user_pass_box.click()
user_pass_box.send_keys('admin')
#webdriver.ActionChains(driver).send_keys(Keys.ESCAPE).perform()
login_button = browser.find_element_by_id('login_but')
login_button.click()
return
def Gui_Logout(browser):
browser.switch_to.default_content() # Get back to the default starting point
# In order to click the Logout button I needed to pass through two frames
browser.switch_to.frame('box_menu')
browser.switch_to.frame('box_menu')
logout_btn = browser.find_element_by_id('Logout')
logout_btn.click()
return
def Sample_Uplink(browser):
browser.switch_to.default_content()
# Go to the Configuration Pane (main_menu)
browser.switch_to.frame('main_menu')
Configuration_Btn = browser.find_element_by_id('Configuration')
Configuration_Btn.click()
browser.switch_to.default_content()
# Go to the Uplink 1 CFP2 information and take the Rx Pwr
browser.switch_to.frame('box_menu')
browser.switch_to.frame('box_menu')
Port_19 = browser.find_element_by_id('Port-19')
Port_19.click()
browser.switch_to.default_content()
# Show the Optic Module information TAB
browser.switch_to.frame('main_body')
CFP2_Info = browser.find_element_by_id('tab_XFP')
CFP2_Info.click()
# Collect the Rx Pwr from the CFP2 Info screen
browser.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 = browser.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 = browser.find_element_by_xpath('html/body/form/div[1]/div/table/tbody/tr[4]/td[2]')
OSNR_Lvl = OSNR.text
# time.sleep(5)
return (RcvPwr, OSNR_Lvl)
def Save_2_File(Rx_Pwr, OSNR_Lvl, i):
file = open("test_results.txt", "a")
file.write("%i. " %i)
file.write(datetime.now().strftime('%H:%M:%S %d-%m-%Y ')) # Print Time & Date to the text file
file.write(Rx_Pwr) # Print the Rx_Pwr to the text file
file.write('%10s' %(OSNR_Lvl)) # Format the placement of the OSNR value
file.write('\n') # Make sure that the next iteration will write the results in the next line
file.close() # Closing the file
return
def execute_code(i, browser):
#===========================================================================
# Cold Restart Recovery test
#===========================================================================
URL1 = 'http://10.0.1.131' # First device that will be Cold Restarted
Gui_Login(browser, URL1)
Cold_Restart(browser)
(RcvPwr, OSNR_Lvl) = Sample_Uplink(browser)
Save_2_File(RcvPwr, OSNR_Lvl, i)
URL2 = 'http://10.0.1.134'
Gui_Login(browser, URL2)
Cold_Restart(browser)
(RcvPwr, OSNR_Lvl) = Sample_Uplink(browser)
Save_2_File(RcvPwr, OSNR_Lvl, i)
#===========================================================================
# Local Uplink Down --> Up
#===========================================================================
browser.get(URL1)
Port_Admin_Down(browser)
Port_Admin_Up(browser)
time.sleep(5)
browser.get(URL2)
#Get Rx Pwr and OSNR and save in file
(RcvPwr, OSNR_Lvl) = Sample_Uplink(browser)
Save_2_File(RcvPwr, OSNR_Lvl, i)
#===========================================================================
# Remote Uplink Down --> Up
#===========================================================================
Port_Admin_Down(browser)
Port_Admin_Up(browser)
time.sleep(5)
browser.get(URL1)
#Get Rx Pwr and OSNR and save in file
(RcvPwr, OSNR_Lvl) = Sample_Uplink(browser)
Save_2_File(RcvPwr, OSNR_Lvl, i)
if __name__ == '__main__':
main()
我的问题是,大多数脚本实际上是find_elements和switch_frame命令。当我期待时,我希望看到测试计划脚本更清洁,而查找(映射)元素和切换帧的所有“脏工作”都在单独的模块中完成。
我将这些代码行分成了一个不同的模块,然后才意识到我有一定的问题。浏览器变量对于模块和测试脚本是通用的,不应该打开两次(我想是这样)。
如何在不失去测试计划的主要目标的情况下完成这项工作?