Python自動化テストシリーズ[v 1.0.0][マルチスレッドアプリケーション]

14611 ワード

Pythonマルチスレッドの自動化テストへの応用


マルチスレッドをテストに巧みに適用することは、確かに多くのメリットをもたらし、機械リソースを十分に利用して効率的なテストを実行するのに良い方法です.
# -*- coding: utf-8 -*-
import threading
from time import ctime
import time
from selenium import webdriver


def test_search(browser, word):
    print("Start search at: %s
"
% ctime()) print("Browser is: %s
"
% browser) if browser == 'ie': browser_driver = webdriver.Ie() elif browser == 'chrome': browser_driver = webdriver.Chrome() elif browser != 'ie' and browser != 'chrome': print("browser 'ie chrome' Fiefox") browser_driver = webdriver.Firefox() browser_driver.get("http://www.baidu.com") browser_driver.find_element_by_id("kw").send_keys(word) browser_driver.find_element_by_id("su").click() time.sleep(3) browser_driver.quit() if __name__ == '__main__': dicts = {'chrome':'__davieyang__','ie':'davieyang','davieyang':'davieyang__'} threads = [] for browser, word in dicts.items(): t = threading.Thread(target = test_search, args=(browser, word)) threads.append(t) for t in range(len(dicts)): threads[t].start() for t in range(len(dicts)): threads[t].join()
# -*- coding: utf-8 -*-
from selenium import webdriver
from time import sleep
from time import ctime
import multiprocessing


def start_browser(browser, time):
    if browser == "chrome":
        print("starting chrome browser now! %s" % ctime())  #   
        chrome_driver = webdriver.Chrome()
        chrome_driver.get("http://www.baidu.com")
        sleep(time)
        chrome_driver.quit()
    elif browser == "firefox":
        print("starting firefox browser now! %s" % ctime())  #  
        fire_driver = webdriver.Firefox()
        fire_driver.get("http://www.baidu.com")
        sleep(time)
        fire_driver.quit()
    else: 
        print("starting ie browser now! %s" %ctime())  #  
        ie_driver = webdriver.Ie()
        ie_driver.get("http://www.baidu.com")
        sleep(time)
        ie_driver.quit()


#           
browser_dict = {"chrome": 3, "firefox": 4}
#   List 
start_browser_processing = []
#   Key-Value, List 
for browser, time in browser_dict.items():
    processing_browser = multiprocessing.Process(target = start_browser, args = (browser, time))
    start_browser_processing.append(processing_browser)

if __name__ == '__main__':
    for processing_browser in range(len(browser_dict)):
        start_browser_processing[processing_browser].start()
    for processing_browser in range(len(browser_dict)):
        start_browser_processing[processing_browser].join()
    print(u"  %s" % ctime())