Selenium Grid 安装

    xiaoxiao2022-07-15  144

    Selenium Grid   Selenium Grid允许同时并行地、在不同的环境上运行多个 测试任务。这里主要演示一下怎么使用Selenium Grid。    准备:   1、需要两台机子   2、两台机子分别安装好JDK环境   3、两台机子需要从 http://code.google.com/p/selenium/downloads/list下载selenium-server-standalone-*.jar包    开始:   Grid需要一台机子做为主节点,然后其它机子做为子节点连接到这个主节点上来。所以首先要启动主节点。    启动主节点:   选一台机子做为主节点。打开命令行,cd至selenium-server-standalone-*.jar包的目录下,然后用下面的命令启动主节点服务:    java -jar selenium-server-standalone-2.24.1.jar -role hub   默认启动默认端口为4444。如果要改这个端口,可以再上面的命令后面加上 -port XXXX。启动完后,你可以用浏览 器    打开 http://localhost:4444/grid/console 这个网址查看主节点的状态。   启动完主节点之后,要做的就是启动子节点。    启动子节点:   先另一台机子做为子节点。同样打开命令行,cd至selenium-server-standalone-*.jar包的目录下,然后用下面的命令启动次节点服务:   java -jar selenium-server-standalone-2.24.1.jar  -role  node  -hubhttp://192.168.4.124:4444/grid/register   其中192.168.4.124为主节点机子的ip地址,可以使用ipconfig命令在命令行查看得到。上面命令默认启动5555端口,可使用-port 更改。   启动完成连接到主节点后,可以在主节点机子上 ,http://localhost:4444/grid/console网址查看到这个子节点状态。使用同样的方法,可以链接其它的子节点。    运行一个简单的例子:   上面已经把grid弄成功了,现在我们用Grid来运行一个很简单的例子。代码如下: import java.net.MalformedURLException; import java.net.URL; import org.openqa.selenium.WebDriver; import org.openqa.selenium.remote.DesiredCapabilities; import org.openqa.selenium.remote.RemoteWebDriver; public class GridTest { /** * @throws MalformedURLException */ public static void main(String[] args) throws MalformedURLException { DesiredCapabilities  test = DesiredCapabilities.firefox(); WebDriver dr = new RemoteWebDriver(new URL("http://192.168.4.137:5555/wd/hub"),test); dr.get("http://www.baidu.com"); } }   在主节点机子上运行上面的代码,你可以在次节点机子上看到firefox浏览器被启动,然后打开了www.baidu.com这个网址。    值得注意的是:   WebDriver dr = new RemoteWebDriver(newURL("http://192.168.4.137:5555/wd/hub"),test);   这一句中的192.168.4.137为次节点的ip地址。 Selenium Grid   Selenium Grid允许同时并行地、在不同的环境上运行多个 测试任务。这里主要演示一下怎么使用Selenium Grid。    准备:   1、需要两台机子   2、两台机子分别安装好JDK环境   3、两台机子需要从 http://code.google.com/p/selenium/downloads/list下载selenium-server-standalone-*.jar包    开始:   Grid需要一台机子做为主节点,然后其它机子做为子节点连接到这个主节点上来。所以首先要启动主节点。    启动主节点:   选一台机子做为主节点。打开命令行,cd至selenium-server-standalone-*.jar包的目录下,然后用下面的命令启动主节点服务:    java -jar selenium-server-standalone-2.24.1.jar -role hub   默认启动默认端口为4444。如果要改这个端口,可以再上面的命令后面加上 -port XXXX。启动完后,你可以用浏览 器    打开 http://localhost:4444/grid/console 这个网址查看主节点的状态。   启动完主节点之后,要做的就是启动子节点。    启动子节点:   先另一台机子做为子节点。同样打开命令行,cd至selenium-server-standalone-*.jar包的目录下,然后用下面的命令启动次节点服务:   java -jar selenium-server-standalone-2.24.1.jar  -role  node  -hubhttp://192.168.4.124:4444/grid/register   其中192.168.4.124为主节点机子的ip地址,可以使用ipconfig命令在命令行查看得到。上面命令默认启动5555端口,可使用-port 更改。   启动完成连接到主节点后,可以在主节点机子上 ,http://localhost:4444/grid/console网址查看到这个子节点状态。使用同样的方法,可以链接其它的子节点。    运行一个简单的例子:   上面已经把grid弄成功了,现在我们用Grid来运行一个很简单的例子。代码如下: import java.net.MalformedURLException; import java.net.URL; import org.openqa.selenium.WebDriver; import org.openqa.selenium.remote.DesiredCapabilities; import org.openqa.selenium.remote.RemoteWebDriver; public class GridTest { /** * @throws MalformedURLException */ public static void main(String[] args) throws MalformedURLException { DesiredCapabilities  test = DesiredCapabilities.firefox(); WebDriver dr = new RemoteWebDriver(new URL("http://192.168.4.137:5555/wd/hub"),test); dr.get("http://www.baidu.com"); } }   在主节点机子上运行上面的代码,你可以在次节点机子上看到firefox浏览器被启动,然后打开了www.baidu.com这个网址。    值得注意的是:   WebDriver dr = new RemoteWebDriver(newURL("http://192.168.4.137:5555/wd/hub"),test);   这一句中的192.168.4.137为次节点的ip地址。 最新内容请见作者的GitHub页:http://qaseven.github.io/
    最新回复(0)