Grid介紹
????????Selenium Grid 是 Selenium 提供的?個?具,?于?持在多臺計算機上并?運?測試。
????????它允許將測試分發到不同的機器和瀏覽器組合上,同時收集結果。
? 1.并?執?測試?例:在不同的機器上并?執?測試?例,從?加速整個測試過程。每個
? ? ? ? ? 節點可以并?地運?測試,?般?于?型測試套件和?規模的項?
? ? ? ? 2.多瀏覽器和多平臺測試:在不同的機器上同時運?測試,可以同時在不同的瀏覽器和操作系統上執?測試。確保應?程序的跨瀏覽器和跨平臺兼容性?常重要\
????????3. 資源最?化利?: 可以利??絡上多臺機器的資源,?不是僅僅依賴于本地機器的資源。可以更有效地使?硬件資源,尤其是在?規模測試或者需要?量瀏覽器并?執?的情況下
? ? ? ? 4.分布式測試環境: 可以設置為在不同的物理位置、不同的?絡環境或不同的云服務上運?測試。這允許你創建 ?個分布式的測試環境,以模擬真實的不同使?情境
? ? ? ? 5.提?可靠性: 在分布式環境下執?測試可以提?測試的可靠性。如果?個節點失敗,其他節點仍然可以繼續執?測 試,從?減?了整個測試過程受到?個節點失敗的影響
Grid快速??
? ? ? ? ? ? 1.首先我們要配置jdk的運行環境
? ? ? ? ? ? ?2.安裝google瀏覽器,并且配置chromedriver驅動
? ? ? ? ? ? ? 3.下載selenium-server-4.5.0.jar插件
????????????????https://github.c om/SeleniumHQ/selenium/releases/tag/selenium-4.5.0
環境都配置好后,在下載的selenium-server-4.5.0.jar所在的?錄層級打開cmd,并且輸?命令啟動grid:
????????????????
?件名(selenium-server-<version>.jar)盡可能的?tab?動補全,
防??動輸?有誤
示例:
java -jar selenium-server-4.5.0.jar standalone
注:啟動服務之后,切記不要關閉cmd窗?,否則服務就會被終?,
?法訪問
啟動之后,?google瀏覽器打開: http://127.0.0.1:4444,出現如下??及成功
????????????????
?將執??例的機器添加到監控端,在代碼中進?如下配置
????????????????
import allure
import pytest
from selenium import webdriver@pytest.fixture()
def browser():global driver# 01 用例的前置步驟,初始化瀏覽器對象chrome_options = webdriver.ChromeOptions()driver = webdriver.Remote(command_executor="http://127.0.0.1:4444",options=chrome_options)# 02 用例執行,返回driveryield driver# 03 用例的后置步驟,關閉瀏覽器driver.quit()@pytest.hookimpl(hookwrapper=True)
def pytest_runtest_makereport():# 獲取測試用例的執行結果,yield,返回給out對象,然后再去轉化為result對象out = yield"""從result對象out獲取調用結果的測試報告,返回一個report對象report對象的屬性:包括when(setup,call,teardown等三個值)、nodeid(測試用例的名稱)、outcome(用例的執行結果, passed, failed)"""report = out.get_result() # 返回一個report對象# 僅僅獲取用例call階段的執行結果,不包含setup、teardownif report.when == "call":# 獲取用例call執行結果為失敗的情況xfail = hasattr(report, "wasxfail") # hasattr方法會:返回對象是否具有給定名稱的屬性# 如果測試用例被跳過且標記為預期失敗,或者測試用例執行失敗且不是預期失敗if (report.skipped and xfail) or (report.failed and not xfail):# 添加allure報告截圖with allure.step("添加失敗的截圖 ---> "):allure.attach(driver.get_screenshot_as_png(), "失敗的截圖",allure.attachment_type.PNG)elif report.passed:# 如果測試用例執行通過,添加allure報告截圖with allure.step("添加成功的截圖 ---> "):allure.attach(driver.get_screenshot_as_png(), "成功的截圖",allure.attachment_type.PNG)
Grid模式詳解
? ? ? ? 1.Standalone(獨?模式)
????????????????Standalone將所有Grid組件?縫地組合為?個。以獨?模式運?Grid可以在單個進程中? ? ? ? ? ? ? ? ?使?單個命令獲得功能完整的Grid。單機只能在?臺機器上運?
????????????????Standalone也是運轉Selenium Grid最簡單的模式。默認情況下,服務器將監聽? ? ? ? ? ? ? ? ? ? ? ? ? ? ? http://localho st:4444上的RemoteWebDriver請求。默認情況下,服務器將從System? ? ? ? ? ? ? ? ? ? ? PATH(環境變量)中檢測可?的驅動程序
? ? ? ? 2.Hub and Node (中?和節點模式)
????????????????中?和節點是使?最多的模式,因為它允許:
????????????????將不同的機器組合在?個Grid中
????????????????例如,使?不同操作系統和/或瀏覽器版本的機器
????????????????使?統???點來在不同的環境中運?WebDriver測試在不破壞Grid的情況下,增加或? ? ? ? ? ? ? ? ? ?減少組件
? ? ? ? ? ?3.Hub(主控制節點)
????????????????Hub 是 Selenium Grid 的中?控制節點,負責管理和分發測試請求。
? ? ? ? ? ? ? Hub 接收來?測試腳本的請求,然后將這些請求分發給連接到 Grid 的各個 Node 上執? ? ? ? ? ? ? ? ? ??。
????????????????功能:
? ? ? ? 4.Node(執?測試的節點)
????????????????Node 是 Selenium Grid 中的?作節點,負責實際執?測試?例。
? ? ? ? ? ? ? ? ??個 Grid 中可以有多個 Node,每個 Node 都可以運?在不同的機器上,甚?可以具有? ? ? ? ? ? ? ? ?不同的操作系統和瀏覽器組合。
????????????????同?機器上的多個節點
? ? ? ? ? ? ? ? ?設置不同的端?
????????????????
java -jar selenium-server-4.5.0.jar node --port 6666
???????????????
運?的main.py?件采?多線程的運??式
????????
import pytest
if __name__ == '__main__':
pytest.main(['-vs', '-n', '3'])
什么時候應該使?Grid
????????多瀏覽器和多平臺測試
? ? ?1.? ?當你需要在不同的瀏覽器類型、版本和操作系統上同時運?測試時,Selenium
? ? ? ? Grid 是?個理想的選擇。它允許在多個節點上并?運?測試,每個節點可以代表不同的瀏覽? ? ? ? ? 器和操作系統組合
? ? ? ? 2.減少測試執?時間
? ? ? ? ? ? 對于?型測試套件,Selenium Grid 可以顯著減少測試執?時間。通過并?執?測試,你可以在相同的時間內完成更多的測試任務,提?測試效率
? ? ? ? 3.提?測試覆蓋率
????????通過在不同瀏覽器和平臺上并?運?測試,你可以更全?地測試應?程序,確保其在各種環境下的穩定性和?致性
? ? ? ? 4.快速反饋
????????縮短測試套件執?的時間,可以更快地獲得測試結果。這對于在持續集成和持續交付環境中迅速反饋開發?員和團隊?常重要
????????5. 資源最?化
????????利?多臺計算機(節點)上的瀏覽器實例,充分利?可?資源,提?測試并?性,避免資源浪費
????????6. ?動化平臺兼容性測試
????????????????當你需要測試應?程序在不同瀏覽器和平臺上的兼容性時,Selenium Grid 是?個強?的?具。它使你能夠同時在多個環境中驗證應?程序的性能和功能
?
服務Grid的組件(了解)????????
? ? 主要包括路由器(Router)、分發器(Distributor)、會話映射(Session Map)、新會? ? ? ? ? ? ? ?話隊列(New Session Queue)、節點(Node)和事件總線(Event Bus)
????????
封裝日志
? ? ? ? ? ? ? ?我們新建一個pytest.ini的文件
????????????????
[pytest]
log_cli = true
log_cli_level = INFO
log_cli_fromat = %(asctimes)s [%(levelname)s] %(message)s
log_cli_date_fromat = %Y-%m-%d %H:%M:%S
log_file = pytest.log
各個名詞的解釋
????????
[pytest]
; 這個里面的變量名都是固定的,不能隨意修改
; 啟用在命令行界面(CLI)上輸出日志。
log_cli = true; 設置輸出日志級別
log_cli_level = INFO; 指定命令行頁面(CLI)上日志輸出的格式
; 日志記錄的時間(年月日時分秒,代碼格式: %Y-%m-%d %H:%M:%S)
; 日志級別
; 日志消息
log_cli_fromat = %(asctimes)s [%(levelname)s] %(message)s; 設置時間格式
log_cli_date_fromat = %Y-%m-%d %H:%M:%S; 設置日志保存文件
log_file = pytest.log
應用到conftest中
????????
import loggingimport allure
import pytest
from selenium import webdriver@pytest.fixture()
def browser():global driver# 01 用例的前置步驟,初始化瀏覽器對象driver = webdriver.Chrome()# 02 用例執行,返回driveryield driver# 03 用例的后置步驟,關閉瀏覽器driver.quit()@pytest.hookimpl(hookwrapper=True, tryfirst=True)
def pytest_runtest_makereport(item, call):# 獲取測試用例的執行結果,yield,返回給out對象,然后再去轉化為result對象out = yield"""從result對象out獲取調用結果的測試報告,返回一個report對象report對象的屬性:包括when(setup,call,teardown等三個值)、nodeid(測試用例的名稱)、outcome(用例的執行結果, passed, failed)"""report = out.get_result() # 返回一個report對象# 僅僅獲取用例call階段的執行結果,不包含setup、teardownif report.when == "call":# 修改之前的版本:# logging.info("--------->日志的頭部<---------")# logging.info(f"用例ID:{report.nodeid}")# logging.info(f"測試結果:{report.outcome}")# logging.info(f"故障標識:{report.longrepr}")# logging.info(f"異常信息:{call.excinfo}")# logging.info(f"用例耗時:{report.duration}")# logging.info("--------->日志的尾部<---------")# 修改之后的版本:allure.attach("--------->日志的頭部<---------")allure.attach(f"用例ID:{report.nodeid}", name="用例ID")allure.attach(f"測試結果:{report.outcome}", name="測試結果")allure.attach(f"故障標識:{report.longrepr}", name="故障標識")allure.attach(f"異常信息:{call.excinfo}", name="異常信息")allure.attach(f"用例耗時:{report.duration}", name="用例耗時")allure.attach("--------->日志的尾部<---------")# 獲取用例call執行結果為失敗的情況xfail = hasattr(report, "wasxfail") # hasattr方法會:返回對象是否具有給定名稱的屬性# 如果測試用例被跳過且標記為預期失敗,或者測試用例執行失敗且不是預期失敗if (report.skipped and xfail) or (report.failed and not xfail):# 添加allure報告截圖with allure.step("添加失敗的截圖 ---> "):allure.attach(driver.get_screenshot_as_png(), "失敗的截圖",allure.attachment_type.PNG)elif report.passed:# 如果測試用例執行通過,添加allure報告截圖with allure.step("添加成功的截圖 ---> "):allure.attach(driver.get_screenshot_as_png(), "成功的截圖",allure.attachment_type.PNG)
?