Selenium Webdriver Desired Capabilities
前言
我們每一個的selenium測試都應該在指定的環境下運行,這個環境可以是web瀏覽器、移動設備、移動模擬器等等。
那怎麼來指定我們的selenium測試腳本運行環境呢?
本次分享主要解決這個問題!!!
在python版本的webdrive中,DesiredCapabilities類為我們提供了解決方案,通過這個類,我們能夠配置webdriver在指定的環境執行我們的測試腳本。
我們先看一張實際測試過程中會應用到的結構圖,基於selenium grid進行分散式測試:
在上圖中,我們可以使用windows下ie、firefox或linux下ie、firefox進行測試,甚至可以設置使用andriod或ios設備下瀏覽器進行測試。
那怎麼來進行設置呢?
我們先看一下DesiredCapabilities的源碼
源碼分析
DesiredCapabilities類代碼在C:Python27Libsite-packagesseleniumwebdrivercommondesired_capabilities.py。
註:筆者的python安裝在C盤下。
class DesiredCapabilities(object): """ Set of default supported desired capabilities. Use this as a starting point for creating a desired capabilities object for requesting remote webdrivers for connecting to selenium server or selenium grid. Usage Example:: from selenium import webdriver selenium_grid_url = "http://198.0.0.1:4444/wd/hub" # Create a desired capabilities object as a starting point. capabilities = DesiredCapabilities.FIREFOX.copy() capabilities[platform] = "WINDOWS" capabilities[version] = "10" # Instantiate an instance of Remote WebDriver with the desired capabilities. driver = webdriver.Remote(desired_capabilities=capabilities, command_executor=selenium_grid_url) Note: Always use .copy() on the DesiredCapabilities object to avoid the side effects of altering the Global class instance. """ FIREFOX = { "browserName": "firefox", "version": "", "platform": "ANY", "javascriptEnabled": True, "marionette": True, } INTERNETEXPLORER = { "browserName": "internet explorer", "version": "", "platform": "WINDOWS", "javascriptEnabled": True, } EDGE = { "browserName": "MicrosoftEdge", "version": "", "platform": "WINDOWS" } CHROME = { "browserName": "chrome", "version": "", "platform": "ANY", "javascriptEnabled": True, } OPERA = { "browserName": "opera", "version": "", "platform": "ANY", "javascriptEnabled": True, } SAFARI = { "browserName": "safari", "version": "", "platform": "MAC", "javascriptEnabled": True, } HTMLUNIT = { "browserName": "htmlunit", "version": "", "platform": "ANY", } HTMLUNITWITHJS = { "browserName": "htmlunit", "version": "firefox", "platform": "ANY", "javascriptEnabled": True, } IPHONE = { "browserName": "iPhone", "version": "", "platform": "MAC", "javascriptEnabled": True, } IPAD = { "browserName": "iPad", "version": "", "platform": "MAC", "javascriptEnabled": True, } ANDROID = { "browserName": "android", "version": "", "platform": "ANDROID", "javascriptEnabled": True, } PHANTOMJS = { "browserName": "phantomjs", "version": "", "platform": "ANY", "javascriptEnabled": True, }
翻譯下:
Set of default supported desired capabilities.desired capabilities默認支持的設置。Use this as a starting point for creating a desired capabilities object forrequesting remote webdrivers for connecting to selenium server or selenium grid.使用該類為selenium server或selenium grid啟動一個desired capabilities配置
對配置項進行解釋
FIREFOX = { "browserName": "firefox", # 瀏覽器名稱 "version": "", # 操作系統版本 "platform": "ANY", # 平台,這裡可以是windows、linux、andriod等等 "javascriptEnabled": True, # 是否啟用js "marionette": True, # 這個值沒找對應的說明^_^ 不解釋了}
DesiredCapabilities示例
from selenium import webdriver# 本地啟動selenium gridselenium_grid_url = "http://127.0.0.1:4444/wd/hub"# 創建一個DesiredCapabilities實例capabilities = DesiredCapabilities.FIREFOX.copy()capabilities[platform] = "WINDOWS" # 指定操作系統capabilities[version] = "10" # 指定操作系統版本# 連接到遠程服務進行自動化測試driver = webdriver.Remote(desired_capabilities=capabilities, command_executor=selenium_grid_url)
總結
這裡的本質就是基於selenium grid 構建分散式自動化測試,而selenium grid根據測試腳本構建的DesiredCapabilities參數來決定將您的測試腳本分發到哪台機器或設備進行測試。
推薦閱讀:
※selenium中如何模擬頁面滾動?
※mac下怎麼搭建selenium python環境?
※4.3 通過selenium 模擬瀏覽器抓取