我构建了一个自动WEB GUI测试,它由python脚本(FireFox_FirstTests.py)实现。为了反复运行这个测试,我编写了另一个使用索引变量的脚本(OSNR_Cycle.py)" i"计算周期数。 我希望能够添加这个索引" i"每个循环到写入文件。结果应该是文本文件中的当前行将显示当前的" i"值与当前时间和日期一起进行两次测量。
OSNR_Cycle.py:
import os
i = 0
while True:
i = i +1
os.system("FireFox_FirstTests.py")
if i == 500:
break
FireFox_FirstTests.py:
from selenium import webdriver
from datetime import datetime
import time
from OSNR_Cycle import i
from ReadWrite2File_Experiments import file
def Cold_Restart():
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 Save_2_File(Rx_Pwr, OSNR_Lvl):
file = open("test_results.txt", "a")
file.write(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
profile = webdriver.FirefoxProfile()
profile.accept_untrusted_certs = True
browser = webdriver.Firefox(firefox_profile = profile)
browser.get('http://10.0.1.134')
browser.implicitly_wait(10) # 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')
login_button = browser.find_element_by_id('login_but')
login_button.click()
# 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
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
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
Save_2_File(RcvPwr, OSNR_Lvl)
Cold_Restart()
browser.quit()
我的问题是,在我做了最后一次更改后,从OSNR_Cycle import i "添加" 我不仅未能获得所需的结果,而且还遇到了多个python.exe进程启动并且内存被泛滥的情况。 记忆泛滥的原因是什么? 如何获得所需的结果(每个周期将索引打印到文件中)?
答案 0 :(得分:0)
通过查看代码,我认为你不必从另一个代码导入变量。有两种方法可以解决这个问题。
1)只需保存您的Local变量或使用ONSR_cycle中的Pickle到文件中,然后阅读Pickle或文件。
2)第二个但是简单的解决方案是在FireFox_FirstTests.py中编写ONSR_cycle的代码,以便您可以将其作为本地变量访问。
from selenium import webdriver
from datetime import datetime
import time
from ReadWrite2File_Experiments import file
def main():
i =0
while True:
i = i +1
#os.system("FireFox_FirstTests.py")
execute_code(i)
if i == 500:
break
def Cold_Restart():
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 Save_2_File(Rx_Pwr, OSNR_Lvl,i):
file = open("test_results.txt", "a")
file.write(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):
profile = webdriver.FirefoxProfile()
profile.accept_untrusted_certs = True
browser = webdriver.Firefox(firefox_profile = profile)
browser.get('http://10.0.1.134')
browser.implicitly_wait(10) # 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')
login_button = browser.find_element_by_id('login_but')
login_button.click()
# 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
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
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
Save_2_File(RcvPwr, OSNR_Lvl,i)
Cold_Restart()
browser.quit()
if __name__ == '__main__':
main()