zoukankan      html  css  js  c++  java
  • java-selenium Grid2环境搭建和基本使用

    Selenium Grid简介

    利用Selenium Grid可以使主节点(hub)的测试用例在不同主机即分支点(node)运行。可以使一份测试用例在不同环境下(操作系统、浏览器)执行自动化测试。Selenium Grid 使用Hub和Node模式,一台计算机作为Hub(管理中心)管理其他多个Node(节点)计算机,Hub负责将测试用例分发给多台Node计算机执行,并收集Node计算机执行结果的报告,汇总后提交一份总的测试报告。

    Hub:

    1.在分布式测试模式中,只能有一台作为Hub的计算机。

    2.Hub负责关机测试用例,并负责发送脚本给其他Node节点。

    3.所有的Node节点计算机会在Hub计算机中先进行注册,祖册成功后再和Hub计算机通信,Node节点计算机会告之Hub自己的相关信息,例如,Node计算机的操作系统和浏览器版本。

    4.Hub计算机可以为自己分配执行测试用例的任务。

    5.Hub计算机分发的测试用例任务会在各个Node计算机节点执行。

    Node:

    1.在分布测试中可以有一个或多个Node节点。

    2.Node节点会打开本地的浏览器完成测试任务并返回测试结果给Hub。

    3.Node节点的操作系统和浏览器版本无需和Hub保持一致。

    4.在Node节点上可以同时打开多个浏览器并行执行测试任务。

    selenium Grid环境搭建

    grid下载地址:http://selenium-release.storage.googleapis.com/index.html

    找一个版本和自己使用的selenium版本相同的下载,这里下载的时2.53.1

    启动Hub:

    下载完成后,打开cmd,进入jar问件所在目录,执行命令 java -jar selenium-server-standalone-2.53.1.jar -role hub

    打开浏览器输入本机的IP+端口号(下图所示)

    点击console

    点击view config

     

    修改Hub参数

    方式一:

    (例:修改端口号为 5566)

    在cmd中按Ctrl+C退出关闭当前的Hub,执行命令 java -jar selenium-server-standalone-2.53.1.jar -role hub -port 5566

    方式二:

    在存放grid的目录下新建一个hub.json文件,将Hub所有参数放进去

    {
    "browserTimeout": 0,
    "capabilityMatcher":"org.openqa.grid.internal.utils.DefaultCapabilityMatcher",
    "cleanUpCycle": 5000,
    "host": null,
    "jettyMaxThreads": -1,
    "maxSession":10,
    "newSessionWaitTimeout": -1,
    "nodePolling": 5000,
    "port": 5555,
    "prioritizer": null,
    "role": hub,
    "servlets": [],
    "throwOnCapabilityNotPresent": true,
    "timeout":200000
    }

    根据自己实际情况修改后在cmd中执行命令: java -jar selenium-server-standalone-2.53.1.jar -role hub -hubConfig hub.json

    打开浏览器输入IP+端口即可

    Node使用

    node节点计算机要求

    1. Node 节点必须要有 java 环境

    2. Node节点跟Hub节点机器间可以互相 ping 通。

    3. Node 节点负责执行Selenium 脚本,所以必须有Selenium 环境(脚本语言对应的环境如java, 各个浏览器及其对应的driver)

    启动Node节点注册到Hub

    在cmd中执行命令:java -jar selenium-server-standalone-2.53.1.jar -role node -hub http://192.168.1.8:4444/grid/register(192.168.1.8:4444是我打开的Hub地址,可以根据自己实际情况而定)

    Hub中看到的效果如下

    修改参数(和Hub的一样)

    方式一:

    以修改端口为例

    在cmd中执行命令:java -jar selenium-server-standalone-2.53.1.jar -role node -hub http://192.168.1.8:4444/grid/register -port 5556

     

    方式二:

    在grid文件下创建一个node.json文件,json内容如下(其中的hub值根据实际情况做修改):

    {
    "capabilities": [
    {
    "browserName": "chrome",
    "maxInstances": 10,
    "platform": "WINDOWS",
    "version": "51",
    "webdriver.chrome.driver":"chromedriver.exe"
    },
    {
    "browserName": "firefox",
    "maxInstances": 10,
    "platform": "WINDOWS",
    "version": "46"
    },
    {
    "browserName": "internet explorer",
    "maxInstances": 3,
    "platform": "WINDOWS",
    "version": "11",
    "webdriver.ie.driver":"IEDriverServer.exe"
    }

    ],
    "configuration": {
    "proxy": "org.openqa.grid.selenium.proxy.DefaultRemoteProxy",
    "maxSession": 5,
    "port": 6060,
    "register": true,
    "registerCycle": 5000,
    "hub": "http://192.168.1.8:4444"
    }
    }

    在cmd中执行命令:java -jar selenium-server-standalone-2.53.1.jar -role node -nodeConfig node.json

    这种方式修改的配置参数只能执行webdriver脚本。也能通过命令的方式指定执行webdriver脚本,执行命令:java -jar selenium-server-standalone-2.53.1.jar -role wd -port 6767 -hub http://192.168.1.8:4444/grid/register

    (此方法基本不使用)只能执行rc脚本的命令:java -jar selenium-server-standalone-2.53.1.jar -role rc-port 6768 -hub http://192.168.1.8:4444/grid/register

     

    指定Node节点计算机执行测试用例

    启动一个Hub:java -jar selenium-server-standalone-2.53.1.jar -role hub -port 5050

    启动一个Node并注册大Hub:java -jar selenium-server-standalone-2.53.1.jar -role node -hub http://192.168.1.8:5050/grid/register

    Java代码

    package com.selenium;
    
    import java.net.MalformedURLException;
    import java.net.URL;
    
    import org.openqa.selenium.By;
    import org.openqa.selenium.WebDriver;
    import org.openqa.selenium.remote.DesiredCapabilities;
    import org.openqa.selenium.remote.RemoteWebDriver;
    import org.testng.Assert;
    import org.testng.annotations.AfterMethod;
    import org.testng.annotations.BeforeTest;
    import org.testng.annotations.Test;
    
    public class Grid {
        WebDriver driver;
        @BeforeTest
        public void BeforeTest() throws MalformedURLException{
            //创建一个DesiredCapabilities类型
            
            DesiredCapabilities ChromeGrid=DesiredCapabilities.chrome();
            //实例化一个driver
            driver=new RemoteWebDriver(new URL("http://192.168.1.8:5050/wd/hub"),ChromeGrid);    
            driver.get("https://www.baidu.com/");
        }
        
        @Test
        public void gridTest() throws InterruptedException{
            
            driver.findElement(By.xpath("//*[@id='kw']")).sendKeys("selenium");
            driver.findElement(By.xpath("//*[@id='su']")).click();    
            Thread.sleep(3000);    
            //获取页面上的资源
            String sourcetext=driver.getPageSource();
            //校验搜索结果是否包含 selenium
            Assert.assertTrue(sourcetext.contains("selenium"));
            Thread.sleep(3000);    
        }
        @AfterMethod
        public void aftermethod(){
            driver.quit();
        }
    
    }

     通过TestNG数据驱动的方式指定Node节点计算机上指定浏览器执行测试用例

    Java代码:

    package com.selenium;
    
    import java.net.MalformedURLException;
    import java.net.URL;
    
    import org.openqa.selenium.By;
    import org.openqa.selenium.WebDriver;
    import org.openqa.selenium.WebElement;
    import org.openqa.selenium.remote.DesiredCapabilities;
    import org.openqa.selenium.remote.RemoteWebDriver;
    import org.testng.Assert;
    import org.testng.annotations.*;
    
    public class GridBingFa {
        WebDriver driver;    
        @DataProvider(name="data")
        public Object[][] Data(){
            return new Object[][]{
                {"chrome","http://192.168.1.8:6060/wd/hub"},
                {"ie","http://192.168.1.8:6070/wd/hub"},
            };
        }
        @Test(dataProvider="data")
        public void test(String browser,String Url) throws InterruptedException, MalformedURLException{
            DesiredCapabilities BrowserGrid = null;
            if(browser.contentEquals("chrome")){
                BrowserGrid =DesiredCapabilities.chrome();
            }else{
                BrowserGrid =DesiredCapabilities.internetExplorer();            
            }
            WebDriver driver=new RemoteWebDriver(new URL(Url),BrowserGrid);
            driver.get("https://www.baidu.com/");
            driver.findElement(By.xpath("//*[@id='kw']")).sendKeys("selenium");
            driver.findElement(By.xpath("//*[@id='su']")).click();    
            Thread.sleep(3000);    
            //获取页面上的资源
            String sourcetext=driver.getPageSource();
            //校验搜索结果是否包含 selenium
            Assert.assertTrue(sourcetext.contains("selenium"));
            Thread.sleep(3000);    
            driver.quit();
        }
        
    }

     

  • 相关阅读:
    一个优秀测试的自我修养
    二.自动化接口测试---用例设计思路、模版
    关于文件读写的一些笔记
    模块导入---如何在一个文件中导入其它模块,来调用它的变量、函数等,以节省代码量
    变量以及作用域----(局部变量、全部变量...)
    python连接mysql数据库
    python修改txt文件内容
    使用PyQt4写界面后台程序方法总结
    unresolved import 解决办法
    怎样使用pyinstaller打包
  • 原文地址:https://www.cnblogs.com/puhongjun/p/10335550.html
Copyright © 2011-2022 走看看