一、Selenium Server 環境配置
? 1、selenium grid的組成與作用:由一個集線器hub和多個客戶機node組成,如果你的程序需要在不用的瀏覽器,不同的操作系統上測試,而且比較多的case需要多線程遠程執行,那么一個比較好的測試方案就是使用 selenium grid,hub用來管理各個代理節點的注冊和狀態信息,并且接受遠程客戶端代碼的請求調用,然后把請求的命令再轉發給代理節點來執行。
(二)selenium grid的優勢
1.在ui自動化的測試過程中,測試各個版本瀏覽器的兼容性問題
2.在ui自動化測試,當測試用例比較多的時候,使用selenium grid分布式處理能大大的提高測試的效率
(三)配置環境
1.安裝JDK,配置好java環境變量
2.下載selenium-server-standalone 地址:http://selenium-release.storage.googleapis.com/index.html
3.Chrome驅動下載地址:https://sites.google.com/a/chromium.org/chromedriver/downloads下載與系統及瀏覽器版本匹配的driver版本:chromedriver_win32.zip
4. Firefox 驅動下載地址:https://github.com/mozilla/geckodriver/releases?下載與系統及瀏覽器版本匹配的driver版本:geckodriver-v0.19.1-win64.zip
(四)啟動 Selenium Grid
?1. 將下載的selenium-server-standalone-3.141.0.jar放在D盤的目錄創建一個文件夾D:\Grid
?2.創建一個批處理文件,用來啟動Selenium Grid服務
(五)Selenium Grid 分布式測試腳本
我們編寫一個Selenium Grid自動化測試腳本,分別在2個node上運行Chrome,Firefox,執行百度WEB頁面自動化測試。
# coding:utf-8
from selenium.webdriverimport Remote
import time
# 定義node_hub與瀏覽器對應關系
nodes = {
'http://127.0.0.1:5555/wd/hub':'chrome',
? ? 'http://127.0.0.1:5556/wd/hub':'firefox'
}
# 通過不同的瀏覽器執行測試腳本
for host, browserin nodes.items():
print(host, browser)
# 調用remote方法
? ? driver = Remote(command_executor=host,
? ? ? ? ? ? ? ? ? ? desired_capabilities={'platform':'ANY', 'browserName': browser, 'version':'', 'javascriptEnabled':True})
# 打開百度首頁并搜索詞語,最后判斷搜索跳轉頁面標題是否含有搜索詞
? ? wd ='我要自學網'
? ? driver.get('https://www.baidu.com')
driver.find_element_by_id("kw").send_keys(wd)
driver.find_element_by_id("su").click()
time.sleep(2)
driver.quit()
如圖展示