Selenium Grid操作使用指北


官網下載地址:https://www.selenium.dev/downloads/

本文中用的之前老版本,歷史版本下載地址:https://selenium-release.storage.googleapis.com/index.html

參考官方文檔:https://www.selenium.dev/documentation/en/grid/setting_up_your_own_grid/

一、實現串行多瀏覽器執行腳本
1、啟動selenium-server-standalone
java -jar selenium-server-s
2、腳本代碼

 1 from selenium import webdriver
 2 from selenium.webdriver.common.desired_capabilities import DesiredCapabilities
 3 import time
 4 
 5 lists = ["chrome","firefox"]
 6 for i in lists:
 7     print(i)
 8     driver = webdriver.Remote(
 9         command_executor="http://127.0.0.1:4444/wd/hub",
10         desired_capabilities={'platform':'ANY',
11                               'browserName':i,
12                               'vwesion':'',
13                               'javascriptEnabled':True
14                               }
15     )
16     driver.get("http://www.baidu.com")
17     driver.find_element_by_id("kw").send_keys("hello")
18     driver.find_element_by_id("su").click()
19     time.sleep(3)
20     driver.quit()

二、selenium實現串行多節點(分布式)執行腳本

1、啟動多節點selenium-server-standalone
主節點啟動(代碼所在主機)
java -jar selenium-server-standalone-2.44.0.jar -role hub

分支節點1啟動(默認是5555端口,如果實際場景可視為比作北京機房,localhost改為主節點IP地址即可去注冊主節點)
java -jar selenium-server-standalone-2.44.0.jar -role node -port 5555 -hub http://localhost:4444

分支節點2啟動(默認端口占用了需要修改下端口,如果實際場景可視為是上海機房,localhost改為主節點IP地址即可去注冊主節點)
java -jar selenium-server-standalone-2.44.0.jar -role node -port 5556 -hub http://localhost:4444

通過瀏覽器查看啟動狀態:http://代碼所在主機/grid/console

如下顯示啟動成功

配置文件代碼:

1 def getconfig():
2     d = {"http://192.168.109.1:5555/wd/hub":"chrome",
3          "http://192.168.109.1:5556/wd/hub":"firefox"}
4     return d

實現代碼:

 1 from selenium import webdriver
 2 from selenium.webdriver.common.desired_capabilities import DesiredCapabilities
 3 import time
 4 import config
 5 
 6 for host,browser in config.getconfig().items():
 7     print(host)
 8     print(browser)
 9     driver = webdriver.Remote(
10         command_executor="http://127.0.0.1:4444/wd/hub",
11         desired_capabilities={'platform':'ANY',
12                               'browserName':browser,
13                               'vwesion':'',
14                               'javascriptEnabled':True
15                               }
16     )
17     driver.get("http://www.baidu.com")
18     driver.find_element_by_id("kw").send_keys("hello")
19     driver.find_element_by_id("su").click()
20     time.sleep(3)
21     driver.quit()    

三、appium實現串行多節點(分布式)執行腳本 

1、啟動主節點:java -jar selenium-server-standalone-2.44.0.jar -role hub

2、appium的多節點需要一個json配置文件。

如下為啟動安卓的配置文件內容及啟動成功展示:

 1 {
 2   "capabilities": [
 3     {
 4       "deviceName": "android",
 5       "version": "7.0",
 6       "maxInstances": 3,
 7       "platformName": "ANDROID"
 8     }
 9   ],
10   "configuration": {
11     "cleanUpCycle": 2000,
12     "timeout": 30000,
13     "proxy": "org.openqa.grid.selenium.proxy.DefaultRemoteProxy",
14     "url": "http://192.168.56.1:4723/wd/hub",
15     "host": "192.168.56.1",
16     "port": 4723,
17     "maxSession": 6,
18     "register": true,
19     "registerCycle": 5000,
20     "hubPort": 4444,
21     "hubHost": "192.168.56.1"
22   }
23 }

如下為啟動iOS的配置文件內容及啟動成功展示:

 1 {
 2   "capabilities": [
 3     {
 4       "deviceName": "ios",
 5       "version": "13.5",
 6       "maxInstances": 3,
 7       "platformName": "IOS"
 8     }
 9   ],
10   "configuration": {
11     "cleanUpCycle": 2000,
12     "timeout": 30000,
13     "proxy": "org.openqa.grid.selenium.proxy.DefaultRemoteProxy",
14     "url": "http://192.168.56.1:5723/wd/hub",
15     "host": "192.168.56.1",
16     "port": 5723,
17     "maxSession": 6,
18     "register": true,
19     "registerCycle": 5000,
20     "hubPort": 4444,
21     "hubHost": "192.168.56.1"
22   }
23 }

 通過瀏覽器查看注冊狀態:http://代碼所在主機/grid/console

如上表示注冊成功!

運行代碼自動尋找設備實現自動化:

 如上顯示運行用例成功!!!

 

總結:

Selenium Grid方案:

1、支持 Android iOS 模擬器

2、支持 Web 瀏覽器

3、支持所有兼容 WebDriver 協議的框架

 

使用 grid 模式做自動化:

1、與普通的自動化用例沒有差別

2、URL 修改為對應的 hub 的 URL 即可

1  # self.driver = webdriver.Remote("http://localhost:4723/wd/hub", caps)
2  self.driver = webdriver.Remote("http://192.168.56.1:4444/wd/hub", caps)

 

Selenium Grid並不能實現並行執行腳本,如果想並行執行需要和多線程進行結合。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM