Selenium-Grid 允许你在多台机器的多个浏览器上并行的进行测试,也就是说,你可以同时运行多个测试。本质上来说就是,Selenium-Grid 支持分布式的测试执行。它可以让你的测试在一个分布式的执行环境中运行。

通常,以下两种情况你都会需要使用 Selenium-Grid。

  • ·        在多个浏览器中运行测试,在多个版本的浏览器中进行测试,或在不同操作系统的浏览器中进行测试。
  • ·        减少测试运行时间。

最近新增了两台Windows机器,需要配置Selenium Grid环境,但是在网上搜到的很多都是针对1.0版本,需要安装Apache Ant等。在 2.0 中,Selenium-Grid 和Selenium-RC 服务端进行了合并。现在,你仅需要下载一个jar 包就可以获得它们。具体配置步骤如下:

1.    1.下载 最新版本的selenium-server-standalone-*.jar,(我用的时候是2.35)官方下载地址 http://code.google.com/p/selenium/downloads/list

2.   2.创建hub(我用的是mac环境下的IntelliJ IDEA),在terminal窗口切换到jar包所在目录,输入命令行:java -jar selenium-server-standalone-2.21.0.jar -role hub

3.   3. 创建node(我的两台分别是WIN7 和WIN8),将前面下载的jar包分别复制到两台准备做node的机器中;在command窗口切换到jar包所在目录,输入命令行:java -jar selenium-server-standalone-2.21.0.jar -role node  -hub http://localhost:4444/grid/register(其中localhost可以换成你做hub的机器的IP地址),可以看到下图:

4.    4.这时在hub端浏览器中输入 http://localhost:4444/grid/console 就应该可以看到两个node的基本信息,如下图

点击view config还可以看到关于hub(即本机)的基本信息

5.   5.接下来就是运行测试case了。首先要确定你的node机器上安装了你想测试的浏览器,IE是Windows默然绑定安装的,jar包里还绑定了Firefox的路径信息,chrome的话问题最多。

6.    6.先贴上关键语句

·         Selenium selenium = new DefaultSelenium("localhost", 4444, "*firefox","http://www.google.com");

·         Selenium selenium = new DefaultSelenium("localhost", 4444, "*googlechrome","http://www.google.com");

·         Selenium selenium = new DefaultSelenium("localhost", 4444, "*iexplore","http://www.google.com");

注意指定浏览器时的标示符(加粗斜体部分)!!!

7.    7.然后就是chrome的问题了,刚开始提示错误内容如下:

java.lang.RuntimeException: Could not start Selenium session: Failed to start new browser session: java.lang.RuntimeException: Google Chrome could not be found in the path!

Please add the directory containing ''chrome.exe' or 'google-chrome'' to your PATH environment

然后我就在两台主机上分别设置了环境变量path,在两台node上分别找到chrome.exe文件的位置,然后在path后添加。(注意WIN7和WIN8下位置不同),尤其是,WIN8下的chrome.exe在用户文件夹下的隐藏文件夹AppData中(C:\Users\xiama\AppData\Local\Google\Chrome\Application),刚添加上错误依旧存在,在Application文件夹下直接双击chrome.exe没有反应(而在WIN7下双击是可以启动chrome的),怀疑是权限问题;然后我又在cmd窗口下分别运行了两个chrome.exe文件,chrome都启动成功!!再然后,case就可以神奇的运行了,sigh~~

8.    8. 下面给出一个典型的测试case:

public class GridTest extends TestCase {

 

    Selenium selenium = new DefaultSelenium("localhost", 4444, "*googlechrome", "http://www.google.cn");

 

    public GridTest()

    {

        super();    

    }

    @Before

    public void setUp(){

 

        selenium.start();

    }

    @After

    public void tearDown(){

 

          selenium.stop();

    }

 

    public void testLogin() {

 

        selenium.open("myWebsite");

        selenium.waitForPageToLoad("30000");

        selenium.type("css=form.login-form input[type=email]","tomserious");

 

 

    }

}

需要注意的几点:

·         浏览器环境是chrome

·         myWebsite应该换成待测试的网页IP

·         type的参数问题  selenium 1selenium.type("css=input#searchText", "salt");对应selenium 2WebElement element = driver.findElement(By.id("searchText")) and useelement.sendKeys("salt");

9.9.另一种典型的的测试case

Public class testDriverTest{

private WebDriver driver = null;

 

    private static boolean isSupportedPlatform() {

        Platform current = Platform.getCurrent();

        return Platform.MAC.is(current) || Platform.WINDOWS.is(current);

    }

 

    @Before

    public void createDriver() {

  assumeTrue(isSupportedPlatform());

  //driver = new SafariDriver();

    DesiredCapabilities capability = DesiredCapabilities.chrome();

 //DesiredCapabilities capability = DesiredCapabilities.firefox();

// DesiredCapabilities capability = DesiredCapabilities.safari();

//        capability.setBrowserName("safari");

//        capability.setPlatform(Platform.WINDOWS);

//        capability.setVersion("5.1");

        try {

            URL url=new URL("http://localhost:4444/wd/hub");

            driver=new RemoteWebDriver(url, capability);

        }

        catch (MalformedURLException e) {

            e.printStackTrace();

            System.out.println("wrong!!!");

        }

 

    }

 

    @After

    public void quitDriver() {

        driver.quit();

    }

 

    @Test

    public void shouldBeAbleToPerformAGoogleSearch() {

       driver.get("http://www.google.com");

        driver.findElement(By.name("q")).sendKeys("webdriver");

        driver.findElement(By.name("btnG")).click();

        new WebDriverWait(driver, 3)

                .until(ExpectedConditions.titleIs("webdriver - Google Search"));

 

    }

}

Tips:需要说明的是“DesiredCapabilities capability = DesiredCapabilities.chrome();”这句,刚开始会提示错误信息如下:

The path to the chromedriver executable must be set by the webdriver.chrome.driver system property

解决方案见https://code.google.com/p/selenium/wiki/ChromeDriver

其实就是这里默认会去打开chromedriver.exe(不是chrome.exe!!!)文件,如果系统环境变量path中没有就会失败。去http://chromedriver.storage.googleapis.com/index.html下载最新版本的chromedriver.zip,解压缩,把chromedriver.exe的路径加入到环境变量path中即可。

1.    关于safari的问题,参见https://code.google.com/p/selenium/wiki/SafariDriver

2.    Selenium 跨平台跨浏览器与否的兼容性http://www.seleniumhq.org/about/platforms.jsp

3.      iOS测试

首先安装Xcode(版本至少为4.6),下载地址https://developer.apple.com/xcode/index.php

其次,下载从http://code.google.com/p/selenium/source/checkout下载源码,在Xcode中运行selenium/iphone/iWebDriver.xcodeproj

第三步,设置build为Simulator / iPhone OS 5.0,run!

最后,在selenium下的代码关键driver=new RemoteWebDriver(new URL("http://localhost:3001/wd/hub"),DesiredCapabilities.iphone());

selenium 工作原理 http://www.cnblogs.com/hyddd/archive/2009/05/30/1492213.html

10.待续

参考资料:

1.      WIKI http://code.google.com/p/selenium/wiki/Grid2

2.      中文文档https://github.com/fool2fish/selenium-doc/blob/master/official-site/selenium-grid.md

3.      Selenium 2教程https://thenewcircle.com/bookshelf/selenium_tutorial/selenium2.html

4.      http://code.google.com/p/selenium/wiki/IPhoneDriver

转:Selenium Grid+JAVA +Windows 配置(Selenium 2.0)的更多相关文章

  1. selenium grid java 资料

    Grid TestNG: 使用Selenium Grid改进Web应用程序的测试: http://www.ithov.com/server/117464.shtml

  2. linux + docker + selenium grid 实现分布式执行selenium脚本

    Selenium Grid 有两个概念 hub :主节点,你可以看作 "北京总公司的测试经理". node:分支节点,你可以看作 "北京总公司的测试小兵A" 和 ...

  3. 【Mac + Python + Selenium】之PyCharm配置Selenium自动化

    一.安装PyCharm 1.下载地址: Pycharm编辑器官网下载地址 2.安装完成之后打开,一路下去点击,直到填写注册码,参考: <[转载][Pycharm编辑器破解步骤]之idea和Pyc ...

  4. selenium grid使用(windows+centos7.4)

    windows作为hub,centos7.4作为node. firefox用的centos7自带的52版本,懒得更新. vm虚拟机必须设置成bridge模式上网,否则报错.具体参见博文:Vmware改 ...

  5. 39 | 从小作坊到工厂:什么是Selenium Grid?如何搭建Selenium Grid?

  6. 搭建selenium grid简单配置

    1.使用selenium提供的服务端独立jar包 :服务端.客户端都是运行于java7环境. 2.启动hub: hub配置文件如下: Java -jar selenium-server-standal ...

  7. Selenium Grid的原理、配置与使用(转)

    Selenium GridSelenium Grid在前面介绍Selenium的时候说过它有三大组件,Selenium Grid就是其中之一而作用就是分布式执行测试.讲分布式之前还是要说说UI自动化的 ...

  8. Selenium Grid分布式测试环境搭建

    Selenium Grid简介 Selenium Grid实际上是基于Selenium RC的,而所谓的分布式结构就是由一个hub节点和若干个node代理节点组成.Hub用来管理各个代理节点的注册信息 ...

  9. Selenium Grid 的使用

    简介 Selenium Grid 是 selenium 的三大组件之一,允许用户同时在不同的机器和系统上测试不同的浏览器,可以分布式的来执行我们的自动化测试,也可以测试不同浏览器的兼容性. Selen ...

随机推荐

  1. 6.编写一个Java应用程序,该应用程序包括2个类:Print类和主类E。Print 类里有一个方法output()功能是输出100 ~ 999之间的所有水仙花数(各位数字的 立方和等于这个三位数本身,如: 371 = 33 + 73 + 13。)在主类E的main方法中来 测试类Print。

    Print类: package com.bao; public class Print { int g,s,b; void outPut() { for(int i=100;i<1000;i++ ...

  2. libPods.a 无法找到的解决方法

    http://stackoverflow.com/questions/9863836/library-not-found-for-lpods To be clear for newbies out t ...

  3. 写一个CGI程序并运行

    准备Linux和Apache我在/var/www/cgi-bin/下建一个文件get.c #include <stdio.h> #include <stdlib.h> int ...

  4. js解析php返回的json数据无法获取length的问题分析

    1.问题出现的过程,js解析php json_encode 的数据,无法获取长度信息,提示undefined   debug:       首先打印查看了php encode后的数据,返现最外层是一个 ...

  5. 其他应用和技巧-eval()函数大行其道

    ---------------------------------- <script type="text/javascript">                   ...

  6. OpenCV成长之路:图像直方图

    http://ronny.blog.51cto.com/8801997/1394115 2014-04-11 13:47:27 标签:opencv 直方图 统计表 原创作品,允许转载,转载时请务必以超 ...

  7. hdu_4529_郑厂长系列故事——N骑士问题(状压DP)

    题目连接:http://acm.hdu.edu.cn/showproblem.php?pid=4529 题意:中文,不解释 题解:状压DP,dp[i][j][k][s]表示第i行当前用了j个骑士,i- ...

  8. A框架 第二部 实例化接收到的get类,调用父类抽象方法,自动执行方法call_user_func_array()

    01父类抽象类 abstract.php <?phpabstract class controller_abstract{ protected $app; function __construc ...

  9. Django: 之用户注册、缓存和静态网页

    Django 用户注册系统 Django 的源码中已经有登录,退出,重设密码等相关的视图函数,在下面这个app中 django.contrib.auth 可以点击对应的版本查看相关源代码:1.9  1 ...

  10. oc拨打电话

    判断当前设备是否支持电话功能 +(int)isCanCall{ if(UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad || [[[UIDevi ...