Selenium Grid分布式測試入門詳解


本文對Selenium Grid進行了完整的介紹,從環境准備到使用Selenium Grid進行一次完整的多節點分布式測試。
運行環境為Windows 10,Selenium版本為 3.5.0,Chrome版本為62,Firefox版本為56,IE版本為11。

1. Selenium Grid簡介

Selenium Grid允許你在多台機器的多個瀏覽器上並行的進行測試,即分布式測試。
通常,以下兩種情況會需要使用Selenium Grid:
1) 通常多個瀏覽器的兼容性測試,即在不同瀏覽器或不同操作系統的瀏覽器中進行測試
2) 測試用例較多時,可以通過分布式測試減少測試執行時間
 

2. Selenium Grid結構

Selenium Grid由一個中心hub及多個節點node組成(類似於其他分布式系統的matser-slave),其中hub用來管理各個node的注冊和狀態信息,並且接受遠程客戶端代碼的請求調用,然后把請求再轉發給node來執行。

 

 

3. 環境准備

由於Selenium Grid的hub及node啟動時需要java環境,所以首先需要安裝JDK。
 

3.1 JDK環境

2. 選擇Java SE 8u151/ 8u152,點擊JDK下載
3. 安裝下載的JDK
4. 配置系統環境變量
 

3.2 selenium-server-standalone下載

2. 下載與本機selenium 3.5.0匹配的版本:selenium-server-standalone-3.5.0.jar
 
2. 拷貝selenium-server-standalone-3.5.0.jar至本地工作目錄下,如D:\grid

3.3 webdriver下載

3.3.1 IE

2) 下載與selenium版本、系統均匹配的IEDriver版本:IEDriverServer_Win32_3.5.0.zip

3.3.2 Chrome

2) 下載與系統及瀏覽器版本匹配的driver版本:chromedriver_win32.zip

3.3.3 Firefox

2) 下載與系統及瀏覽器版本匹配的driver版本:geckodriver-v0.19.1-win64.zip
 

 

driver下載完成解壓后,分別拷貝IEDriverServer.exe,geckodriver.exe,chromedriver.exe至Python安裝路徑(如C:\Python27\)下即可

上述selenium-server-standalone 及webdriver,我已上傳至百度網盤,下載鏈接: https://pan.baidu.com/s/1i4MBpXF 密碼: ygdy

4. Selenium Grid啟動

4.1 啟動hub

hub啟動命令如下:
java -jar selenium-server-standalone-3.5.0.jar -role hub
其中 -role指定角色為hub,通過下圖可以看到:hub已使用默認4444端口啟動成功,且node可以通過 http://localhost:4444/grid/register/進行注冊
 

4.2 啟動node

node啟動命令如下:
java -jar selenium-server-standalone-3.5.0.jar -role node -port 5555 -hub http://localhost:4444/grid/register
其中 -role指定角色為node, -port指定端口為 5555, -hub指定連接hub地址,通過下圖可以看到node已成功連接hub

同理我們另外啟動兩個 node(使用端口號分別為5556/5557):

java -jar selenium-server-standalone-3.5.0.jar -role node -port 5556 -hub http://localhost:4444/grid/register
java -jar selenium-server-standalone-3.5.0.jar -role node -port 5557 -hub http://localhost:4444/grid/register
 
此時打開頁面 http://localhost:4444/grid/console,可以看到我們啟動的三個node:

4.3 更好的啟動方法 — bat腳本

1. 新建一個文件selenium_grid.bat,寫入我們剛才啟動hub及node的命令:
cd /d D:\grid
start java -jar selenium-server-standalone-3.5.0.jar -role hub
start java -jar selenium-server-standalone-3.5.0.jar -role node -port 5555 -hub http://localhost:4444/grid/register
start java -jar selenium-server-standalone-3.5.0.jar -role node -port 5556 -hub http://localhost:4444/grid/register
start java -jar selenium-server-standalone-3.5.0.jar -role node -port 5557 -hub http://localhost:4444/grid/register
1) 第一行是進入並修改當前目錄為存放 selenium-server-standalone-3.5.0.jar的目錄:D:\grid
2) 后面三行stat java...是分別打開新的cmd窗口用以啟動hub及node
 
2. 雙擊selenium_grid.bat即可啟動hub及node
 

4.4 更多選項

關於Selenium Grid更多命令選項,可運行--help查看:
d:\grid>java -jar selenium-server-standalone-3.5.0.jar --help
Usage: <main class> [options]
  Options:
    --version, -version
       Displays the version and exits.
       Default: false
    -browserTimeout
       <Integer> in seconds : number of seconds a browser session is allowed to
       hang while a WebDriver command is running (example: driver.get(url)). If the
       timeout is reached while a WebDriver command is still processing, the session
       will quit. Minimum value is 60. An unspecified, zero, or negative value means
       wait indefinitely.
       Default: 0
    -debug
       <Boolean> : enables LogLevel.FINE.
       Default: false
    -enablePassThrough
       <Boolean>: Whether or not to use the experimental passthrough mode.
       Defaults to true.
       Default: true
    -jettyThreads, -jettyMaxThreads
       <Integer> : max number of threads for Jetty. An unspecified, zero, or
       negative value means the Jetty default value (200) will be used.
    -log
       <String> filename : the filename to use for logging. If omitted, will log
       to STDOUT
    -port
       <Integer> : the port number the server will use.
       Default: 4444
    -role
       <String> options are [hub], [node], or [standalone].
       Default: standalone
    -timeout, -sessionTimeout
       <Integer> in seconds : Specifies the timeout before the server
       automatically kills a session that hasn't had any activity in the last X seconds. The
       test slot will then be released for another test to use. This is typically
       used to take care of client crashes. For grid hub/node roles, cleanUpCycle
       must also be set.
       Default: 1800

5. Selenium Grid 分布式測試腳本

下面,我們編寫一個Selenium Grid自動化測試腳本,分別在3個node上運行Chrome,Firefox及IE瀏覽器,執行WEB頁面自動化測試。
示例腳本如下:
# coding:utf-8


from selenium.webdriver import Remote
import time

# 定義node_hub與瀏覽器對應關系
nodes = {
    'http://127.0.0.1:5555/wd/hub': 'chrome',
    'http://127.0.0.1:5556/wd/hub': 'internet explorer',
    'http://127.0.0.1:5557/wd/hub': 'firefox'
}

# 通過不同的瀏覽器執行測試腳本
for host, browser in nodes.items():
    print(host, browser)
    # 調用remote方法
    driver = Remote(command_executor=host,
                    desired_capabilities={'platform': 'ANY', 'browserName': browser, 'version': '', 'javascriptEnabled': True})

    # 打開百度首頁並搜索詞語,最后判斷搜索跳轉頁面標題是否含有搜索詞
    wd = 'lovesoo'
    driver.get('https://www.baidu.com')
    driver.find_element_by_id("kw").send_keys(wd)
    driver.find_element_by_id("su").click()
    time.sleep(1)
    assert wd in driver.title, '{0} not in {1}'.format(wd, driver.title.encode('utf-8'))
    driver.quit()
運行結果如下:
('http://127.0.0.1:5555/wd/hub', 'chrome')
('http://127.0.0.1:5557/wd/hub', 'firefox')
('http://127.0.0.1:5556/wd/hub', 'internet explorer')
 

6. 常見問題

腳本運行過程中若IE瀏覽器報錯找不到元素,而Chrome及Firefox瀏覽器正常,原因是IE瀏覽器的保護模式沒有關閉:
1) 打開IE瀏覽器,在瀏覽器的菜單欄上點擊“工具”選項,然后點擊“Internet選項”
2) 在“Internet選項”中,切換到“安全”選項,取消“啟用保護模式”的勾選
3) 並且上面四個區域的保護模式都需要關閉

 


免責聲明!

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



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