Selenium_多線程執行測試用例

多線程執行測試用例

這裏以百度搜索爲例,經過不一樣的瀏覽器來啓動不一樣的線程。java

 1 #!/usr/bin/env python
 2 # _*_ coding:utf-8 _*_
 3 __author__ = 'YinJia'
 4 
 5 from threading import Thread
 6 from selenium import webdriver
 7 from time import ctime,sleep
 8 
 9 #測試用例
10 def test_baidu(browser,search):
11     print('start: {0}'.format(ctime()))
12     print('browser: {0}'.format(browser))
13     if browser == "ie":
14         driver =  webdriver.Ie()
15     elif browser == "chrome":
16         driver = webdriver.Chrome()
17     elif browser == "ff":
18         driver = webdriver.Firefox()
19     else:
20         print("browser參數有誤,只能爲 ie、ff、chrome")
21 
22     driver.get("http://www.baidu.com")
23     driver.find_element_by_id("kw").send_keys(search)
24     driver.find_element_by_id("su").click()
25     sleep(2)
26     driver.quit()
27 
28 if __name__=="__main__":
29     lists = {'chrome':'C++','ie':'JAVA','ff':'python'}
30     threads = []
31     files = range(len(lists))
32 
33     #建立線程
34     for browser,search in lists.items():
35         t = Thread(target=test_baidu,args=(browser,search))
36         threads.append(t)
37     #啓動線程
38     for t in files:
39         threads[t].start()
40     for t in files:
41         threads[t].join()
42     print('end: {0}'.format(ctime()))
43 
44 運行結果:
45 start: Mon Aug 20 00:09:50 2018
46 browser: chrome
47 start: Mon Aug 20 00:09:50 2018
48 browser: ie
49 start: Mon Aug 20 00:09:50 2018
50 browser: ff
51 end: Mon Aug 20 00:10:09 2018

 可見同時運行三個不一樣的瀏覽器環境執行測試用例。node

多線程分佈式執行測試用例

Selenium Grid自己不提供併發執行用例,只是提供多系統、多瀏覽器執行環境,鑑於併發執行須要用到多線程技術結合Grid來實現分佈式併發執行測試用例。python

在一臺主機上啓動一個hub主節點和一個node分支節點,另外一臺主機啓動一個node分支節點以下所示:web

  • 啓動本機hub主機:java -jar selenium-server-standalone-2.53.0.jar -role hub [默認4444端口]
  • 啓動本機node分支節點:java -jar selenium-server-standalone-2.53.0.jar -role node -port 5555        
  • 在遠程主機啓動node分支節點:java -jar selenium-server-standalone-2.53.0.jar -role node -port 6666 -hub http://192.168.31.210:4444/grid/register
 1 #!/usr/bin/env python
 2 # _*_ coding:utf-8 _*_
 3 __author__ = 'YinJia'
 4 
 5 
 6 from threading import Thread
 7 from selenium import webdriver
 8 from time import ctime,sleep
 9 
10 #測試用例
11 def test_baidu(host,browser):
12     print('start:{0}'.format(ctime()))
13     print(host,browser)
14     dc = {'browserName':browser}
15     driver = webdriver.Remote(command_executor=host,
16                               desired_capabilities=dc)
17     driver.get("http://www.baidu.com")
18     driver.find_element_by_id("kw").send_keys(browser)
19     driver.find_element_by_id("su").click()
20     sleep(2)
21     driver.quit()
22 
23 if __name__=="__main__":
24     #啓動參數(指定運行主機與瀏覽器)
25     lists = {'http://127.0.0.1:4444/wd/hub':'chrome',
26              'http://192.168.31.230:6666/wd/hub': 'internet explorer',
27              'http://1127.0.0.1:5555/wd/hub': 'firefox'}
28     threads = []
29     files = range(len(lists))
30 
31     #建立線程
32     for browser,search in lists.items():
33         t = Thread(target=test_baidu,args=(browser,search))
34         threads.append(t)
35     #啓動線程
36     for t in files:
37         threads[t].start()
38     for t in files:
39         threads[t].join()
40     print('end: {0}'.format(ctime()))
相關文章
相關標籤/搜索