.NET自动化测试工具:Selenium Grid

  在生产环境,QA会同时跑几十个上百个的test case。若是用单机串行的话,是一件很是耗时的事情,估计比手点快不了多少。使用并行方案的话,有两种方法,一个是本身写并行框架,一个是用现成的Selenium Grid。java

本身写并行框架,好处是一切尽在掌控,有问题能够快速定位,针对业务的定制化程度高,用户用起来方便。目前想到的问题是,若是在单机多帐户登陆同一系统的状况下,会不会串cookie?不过我发现,多是浏览器厂商把不一样的Driver实例给隔离了,这样就省不少事。实现思路大致是:各Client(测试脚本的解析器)向MQ队列发送TestCase消息,多个执行器监听同一队列消费消息,执行测试用例,再返回结果。node

因为本身偷懒,这里先用现成的Selenium Grid来作并行。SeleniumGrid是一个集群,有一个中心节点,称为hub;多个执行节点,称为node。其中,Hub的职责是管理各Node节点(如节点信息、是否Alive)、与Client创建链接、向各Node发送执行命令。 Client与Hub的分界点在Driver这儿。即Selenium脚本仍是在Client上跑,可是控制的Driver是在Grid里面。(这其实跟本身写并行框架的边界划分已经不一样了)。下面就入个门,介绍一下安装、运行、Helloworld(哦,应该是Hello baidu...)chrome

 

1. 下载:浏览器

  1.Selenium Standalone Server: http://www.seleniumhq.org/download/bash

  

 

  2.各类Driver。在www.seleniumhq.org/download也能找到入口。cookie

  

  ChromeDriver能够直接访问:https://sites.google.com/a/chromium.org/chromedriver/框架

2. 安装:测试

  1. 找两台机器,虚拟机也能够。我这里用的是A:10.189.1.206,B:10.189.1.201。A做为Hub,B做为Nodeui

  2. 安装Hub:把下载的selenium-server-standalone-3.5.2.jar 放在在A机器上,运行命令:google

java -jar selenium-server-standalone-3.5.2.jar -role hub

  

  3. 安装Node:一样,把下载的selenium-server-standalone-3.5.2.jar 放在在B机器上,运行命令:

  java -jar selenium-server-standalone-3.5.2.jar -role node -port 6666 -hub http://10.189.1.206:4444/grid/register

  这里必需要指定Hub的地址,端口号(port)能够设定,也能够无论。执行完后,Hub的界面以下,表示Hub已经接受了Node的注册:

  

  能够访问整个Grid的控制台:http://10.189.1.206:4444/grid/console/

  

  4. 在Hub和Node上安装ChromeDriver:

    把下载的chromedriver.exe复制到A机器上,好比放在C:\SeleniumPlugins目录下。在环境变量的Path中,添加该目录地址:C:\SeleniumPlugins。

    在B机器上,执行一样的操做。

    

 

3. C#代码: 

  1. 建立控制台项目,添加Selenium引用。

  

  2. 代码:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using OpenQA.Selenium;
using OpenQA.Selenium.Remote;
using OpenQA.Selenium.Chrome;

namespace RunTestOnRemoteGrid
{
    class Program
    {
        static void Main(string[] args)
        {

            IWebDriver driver=new ChromeDriver();
            try
            {
                driver = new RemoteWebDriver(new Uri("http://10.189.1.206:4444/wd/hub"), new ChromeOptions());
                driver.Navigate().GoToUrl("http://www.baidu.com");
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.ToString());   
            }
            finally
            {
                driver.Quit();
            }
        }
    }
}

  3. 运行:就能够看到Client端在执行,没啥动做,而B机器上,打开了Chrome浏览器,访问Baidu

相关文章
相关标签/搜索