1.1     自动化实例

不同浏览器启动不同线程

创建lists字典,对浏览器与搜索内容进行参数化。通过多线程来运行test_baidu()的测试用例。

from time import sleep, ctime
from selenium import webdriver
from threading import Thread

#测试用例
def test_baidu(driver, search):
    print('start: %s' %ctime())
    print('driver: %s' %driver)
    driver.get('http://www.baidu.com')
    driver.find_element_by_id('kw').send_keys(search)
    driver.find_element_by_id('su').click()
    sleep(2)
    driver.quit()

if __name__ =='__main__':
    lists = {webdriver.Ie():'ietest',webdriver.Chrome():234567,webdriver.Firefox():'fftest'}
    threads = []

    for driver,search in lists.items():
        t = Thread(target=test_baidu,args=(driver,search))
        threads.append(t)
    for t in range(len(lists)):
        threads[t].start()
    for t in range(len(lists)):
        threads[t].join()

    print('End: %s' % ctime())

运行结果

start: Tue May 22 15:35:31 2018
driver: <selenium.webdriver.ie.webdriver.WebDriver (session="3e51bb8d-8bcf-4caf-b2b7-4baedb1f996d")>
start: Tue May 22 15:35:31 2018
driver: <selenium.webdriver.chrome.webdriver.WebDriver (session="de92b79536d3cc0359c84c333910e604")>
start: Tue May 22 15:35:31 2018
driver: <selenium.webdriver.firefox.webdriver.WebDriver (session="2b852aef-8dcd-49d5-a1a4-fb93bca54c51")>
End: Tue May 22 15:36:09 2018

Process finished with exit code 0

 
posted on 2018-05-22 16:13  jxba  阅读(211)  评论(0编辑  收藏  举报