seleniumGrid分布式远程执行测试脚本
执行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"