2017-06-26 95 views
0

我構建了一個由python腳本(FireFox_FirstTests.py)實現的特定的自動化WEB GUI測試。爲了重複執行此測試,我編寫了另一個腳本(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進口加上「的最後一次更改後我「我不但沒有得到所需的結果,但我也進入了多個python.exe進程啓動和內存的情況淹沒。 記憶氾濫的原因是什麼? 我怎樣才能得到所需的結果(索引打印到文件的每個週期)?

回答

0

通過查看你的代碼我不認爲你必須從另一個代碼中導入變量。有兩種方法來解決這個問題。

1)只需保存您的本地變量或使用ONSR_cycle中的Pickle到文件中,然後閱讀Pickle或文件。

2)但是,簡單的解決方案是將ONSR_cycle的代碼寫入FireFox_FirstTests.py,以便可以將其作爲局部變量進行訪問。

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() 
+0

我有2個問題與您的解決方案:「我」內main()被標記爲undefined出於某種原因。和Cold_Restart()中的「瀏覽器」也被標記爲未定義。關於瀏覽器,我認爲它不知何故與execute_code()將「瀏覽器」變成本地變量(糾正我,如果我錯了)的事實 –

+0

我剛剛向您解釋瞭解決您的問題的機制/工作流程。我的機器上沒有安裝依賴項。所以不能調試,但通過這種方式,你可以解決你的問題 –

+0

我認爲我解決了「瀏覽器」問題。還有一個變量需要處理。我不需要初始化「我」(i = 0)嗎? –

相關問題