selenium grid簡單使用

簡介

selenium grid能夠讓測試腳本在不一樣平臺、不一樣瀏覽器執行,是測試用例分發的框架。html

selenium grid 分爲兩種角色:hub 和 node.java

  • hub 是管理中心,負責將測試腳本分發給node
  • node能夠理解爲一個測試機(linex/windows/mac),負責執行hub分發的測試用例

hub使用

下載

http://selenium-release.storage.googleapis.com/index.html 下載selenium-server-standalone-2.53.0.jarnode

啓動

命令行輸入:

java -jar selenium-server-standalone-2.53.0.jar -role hub 這個命令的含義是讓當前電腦當作hub,並啓動服務git

控制檯輸出 "Selenium Grid hub is up and running" 說明已經啓動成功。github

瀏覽器中打開grid控制檯 http://localhost:4444/grid/consolechrome

Hub常見參數

  • throwOnCapabilityNotPresent : true 默認爲 true,表示當前hub只有在有node存在時,纔會接受測試請求。爲false 則反之;
  • capabilityMatcher : org.openqa.grid.internal.utils.DefaultCapabilityMatcher這是一個實現了CapabilityMatcher接口的 類,默認指向org.openqa.grid.internal.utils.DefaultCapabilityMatcher該類用於實現grid在分佈測試任務到對應node 時所使用的匹配規則,若是想要更精確的測試分配規則,那麼就註冊一個本身定義的匹配類;
  • prioritizer : null 這是一個實現了Prioritizer接口的類。設置grid執行test任務的優先邏輯;默認爲null,那個腳本先到 那個先執行;
  • port : 4444 這個是hub 默認的端口號;
  • newSessionWaitTimeout : -1 默認-1,即沒有超時;指定一個新的測試session等待執行的間隔時間。即一個代理節 點上先後2個測試中間的延時時間,單位爲毫秒;
  • browserTimeout : 0 瀏覽器無響應的超時時間,默認爲0表示沒有超時時間

修改Hub配置

  • 1.直接經過命令修改: java -jar selenium-server-standalone-2.53.0.jar -role hub -port 4445json

  • 2.經過Json文件修改: java -jar selenium-server-standalone-2.53.0.jar -role hub -hubConfig hub.jsonwindows

https://github.com/SeleniumHQ/selenium/wiki/Grid2#configuring-the-nodes-by-json

Configuring the hub by JSON

java -jar selenium-server-standalone.jar -role hub -hubConfig hubconfig.json
A sample hubconfig.json file can be seen at https://github.com/SeleniumHQ/selenium/blob/master/java/server/src/org/openqa/grid/common/defaults/DefaultHub.json

node使用

node要求

  • node節點必需要有java環境
  • node 節點跟hub 節點機器間能夠互相 ping 通。
  • node 節點負責執行Selenium 腳本,因此必須有Selenium 環境(腳本語言對應的環境如 java, 各個瀏覽器及其對應的driver)

啓動node並註冊到node

java -jar selenium-server-standalone-2.53.0.jar -role node -hub http://192.168.1.110:4444/grid/registerapi

hub的地址是註冊hub成功後控制檯返回的地址瀏覽器

修改node配置

  • 經過命令修改: java -jar selenium-server-standalone-2.53.0.jar -role rc -port 6666

  • 經過JSON文件修改

Configuring the nodes by JSON

java -jar selenium-server-standalone.jar -role node -nodeConfig nodeconfig.json

A sample nodeconfig file for server version 3.x.x (>= beta4) can be seen at https://github.com/SeleniumHQ/selenium/blob/master/java/server/src/org/openqa/grid/common/defaults/DefaultNodeWebDriver.json

A sample nodeconfig file for server version 2.x.x can be seen at https://github.com/SeleniumHQ/selenium/blob/selenium-2.53.0/java/server/src/org/openqa/grid/common/defaults/DefaultNode.json

Note: the configuration { ... } object in version 2.x.x has been flattened in version 3.x.x (>= beta4)

grid執行測試用例

DesiredCapabilities capabilities = DesiredCapabilities.chrome();
        WebDriver driver = new RemoteWebDriver(new URL("http://192.168.29.79:4444/wd/hub"), capabilities);
        driver.get("http://www.baidu.com");
相關文章
相關標籤/搜索