selenium Grid

时间:2021-04-14 09:16:44
Selenium Grid 的机制是启动一个 hub,然后启动多个 Selenium RC 注册到 hub 上,
当测试请求到 hub 时,hub 会将测试分发给 Selenium RC, Selenium RC 会实际的启动一个浏览器完成测试。

grid原理图

selenium Grid

安装java jdk 并配置好环境的条件下

1.本机启动hub ( win 7 64bit FF 28.0)

java -jar selenium-server-standalone-2.41.0.jar -role hub -maxSession 40 -port 4444

selenium Grid

2.另外一台启动node (win  7 32bit ff 24.5.0),防火墙需要关闭

java -jar selenium-server-standalone-2.41.0.jar -role node \
-hub http://{grid_server}:4444/grid/register -port 5555 \
-firefoxProfileTemplate llinotes.profile -maxSession 20 \
-browser "browserName=firefox,version=28.0,platform=WINDOWS,maxInstances"

在远程机器显示 couldn't register this node,

selenium Grid

将node输入命令改成如下:

java -jar selenium-server-standalone-2.41.0.jar -role webdriver \

-hub http://192.168.1.9:4444/grid/register -port 5555 \

-firefoxProfileTemplate llinotes.profile -maxSession 20 \

-browser "browserName=firefox,version=28.0,platform=WINDOWS,maxInstances"

selenium Grid

register成功

selenium Grid

run hub机器上的脚本

 [Test]
public void Test()
{
DesiredCapabilities capabilities = new DesiredCapabilities(); capabilities = DesiredCapabilities.Firefox(); capabilities.SetCapability(CapabilityType.BrowserName, "firefox"); capabilities.SetCapability(CapabilityType.Platform, new Platform(PlatformType.Windows)); capabilities.SetCapability(CapabilityType.Version, "24.5.0"); driver = new RemoteWebDriver(new Uri("nodeUrl/wd/hub"), capabilities); baseURL = "https://www.google.com/"; verificationErrors = new StringBuilder(); driver.Navigate().GoToUrl(baseURL);
driver.FindElement(By.Id("lst-ib")).SendKeys("selenium");
driver.FindElement(By.Name("btnK")).Click();
}

可以打开Node FF浏览器并进行操作。Done

hub机器显示信息:

selenium Grid

node机器显示:

selenium Grid

接着又遇到一个问题:如果我要在不同的机器上运行hub上的测试,应该怎么做呢

我试着在另外一台node机器register node(记得关闭防火墙)

在hub机器添加如下代码,将nodeUrl改成其他机器上的ip,运行所有测试,按照测试顺序启动不同机器的浏览器

[Test]
public void Test()
{
DesiredCapabilities capabilities = new DesiredCapabilities(); capabilities = DesiredCapabilities.Firefox(); capabilities.SetCapability(CapabilityType.BrowserName, "firefox"); capabilities.SetCapability(CapabilityType.Platform, new Platform(PlatformType.Windows)); capabilities.SetCapability(CapabilityType.Version, "28.0"); driver = new RemoteWebDriver(new Uri("nodeUrl2/wd/hub"), capabilities); baseURL = "https://www.google.com/"; verificationErrors = new StringBuilder(); driver.Navigate().GoToUrl(baseURL);
driver.FindElement(By.Id("lst-ib")).SendKeys("selenium");
driver.FindElement(By.Name("btnK")).Click();
}

为了省事,我想把那些命令直接用cmd批处理来执行

新建.txt文件,输入如下内容:
cd selenium-server-standalone-2.41.0.jar路径
java -jar selenium-server-standalone-2.41.0.jar -role hub -maxSession 40 -port 4444

另存为hubstart.bat,双击就能直接运行这个命令启动hub