執行UI自動化測試指令碼時,不僅可以在本地執行,也可以遠端指定某計算機執行測試指令碼,seleniumGrid為我們提供了這類服務,但還需要自己搭建環境。
一、本地計算機需要準備java環境和selenium-server-standalone-4.0.0-alpha-2.jar包,jar包主要開啟本機的hub節點(理解成控制機)。紅色部分是開啟本機(控制機)節點,藍色部分是開啟遠端node節點時需要連線控制機地址
控制機地址
λ java -jar selenium-server-standalone-4.0.0-alpha-2.jar -role hub
11:05:38.938 INFO [GridLauncherV3.parse] - Selenium server version: 4.0.0-a
lpha-2, revision: f148142cf8
11:05:39.233 INFO [GridLauncherV3.lambda$buildLaunchers$5] - Launching Sele
nium Grid hub on port 4444
SLF4J: Failed to load class "org.slf4j.impl.StaticLoggerBinder".
SLF4J: Defaulting to no-operation (NOP) logger implementation
SLF4J: See http://www.slf4j.org/codes.html#StaticLoggerBinder for further d
etails.
11:05:41.352 INFO [Hub.start] - Selenium Grid hub is up and running
11:05:41.356 INFO [Hub.start] - Nodes should register to http://192.168.207
.1:4444/grid/register/
11:05:41.358 INFO [Hub.start] - Clients should connect to http://192.168.20
7.1:4444/wd/hub
二、遠端機(我用的ubuntu系統)需要準備java環境和selenium-server-standalone-4.0.0-alpha-2.jar包,以及瀏覽器驅動(我用的firefox的驅動)。記住瀏覽器驅動需要新增到系統環境變數中:export PATH="$PATH:/usr/local/bin"。在遠端機啟動node節點:java -jar selenium-server-standalone-4.0.0-alpha-2.jar -role node -port 32100 -hub http://192.168.207.1:4444/grid/register/(開啟遠端節點並連線控制機),連線成功後,在控制機的dos視窗可以看到綠色部分,表示遠端機與控制機連線成功。
λ java -jar selenium-server-standalone-4.0.0-alpha-2.jar -role hub 11:05:38.938 INFO [GridLauncherV3.parse] - Selenium server version: 4.0.0-a lpha-2, revision: f148142cf8 11:05:39.233 INFO [GridLauncherV3.lambda$buildLaunchers$5] - Launching Sele nium Grid hub on port 4444 SLF4J: Failed to load class "org.slf4j.impl.StaticLoggerBinder". SLF4J: Defaulting to no-operation (NOP) logger implementation SLF4J: See http://www.slf4j.org/codes.html#StaticLoggerBinder for further d etails. 11:05:41.352 INFO [Hub.start] - Selenium Grid hub is up and running 11:05:41.356 INFO [Hub.start] - Nodes should register to http://192.168.207 .1:4444/grid/register/ 11:05:41.358 INFO [Hub.start] - Clients should connect to http://192.168.20 7.1:4444/wd/hub 11:19:02.829 INFO [DefaultGridRegistry.add] - Registered a node http://192. 168.52.137:32100
三、python指令碼。主要用到selenium.webdriver.remote.webdriver的WebDriver類完成遠端執行指令碼的任務。第一個引數是遠端計算機的ip與連線埠,第二個引數是執行指令碼的瀏覽器名稱。
driver=webdriver.WebDriver(command_executor='http://0.0.0.0:0/wd/hub',desired_capabilities={ "browserName": "firefox"})
#friefoxdriverexport PATH="$PATH:/usr/local/bin"