Selenium RemoteWebDriver使用—让你的代码与测试分离(远程测试)

一、写在前面

    在学习Selenium基础的时候,我们都是代码和测试在同一端,即为设置好本地指定浏览器的driver后,再启动本地对应的浏览器运行测试。实例代码如下:

public class Test1 {
    public static void main(String[] args) throws Exception{
        System.setProperty("webdriver.chrome.driver","D:\\IDEA\\WorkSpace\\JavaUI\\hcf-webui\\src\\test\\resources\\drivers\\chromedriver81.exe");
        WebDriver driver=new ChromeDriver();
        driver.manage().window().maximize();
        driver.get("http://www.baidu.com");
        System.out.println("打开百度");
        Thread.sleep(2000);
        driver.quit();
    }
}

二、RemoteWebDriver基本使用

    RemoteWebDriver的作用就是远程测试。远程测试可以理解为代码在Machine A上,实际测试和启动的浏览器在Machine B
远程测试的好处:

  • 更方便跨平台、浏览器的测试
  • 测试更加稳定;(在本地启动浏览器测试时,可能会由于相关操作导致测试不稳定)

2.1 配置环境

    这里是在服务器上利用Docker拉取selenium-hub、selenium-node-chrome镜像,并生成对应的容器,且生成node-Chrome容器时,需要将node-chrome容器挂在(–link)selenium-hub容器上,最后的访问网址http://ip地址:selenium-hub容器端口/grid/console 有对应效果即可。
在这里插入图片描述

2.2 配置环境命令

    下面为在服务器上拉取镜像和生成容器的命令,实际也可以通过Rancher去管理。

//拉取selenium-hub的镜像
[root@localhost yff]# docker pull madehao/selenium-hub
//生成selenium-hub的容器 最后访问端口为5555
[root@localhost yff]# docker run --name selenium-hub -p 5555:4444 -v /home/selenium-hub:/var/selenium-hub madehao/selenium-hub

//拉取谷歌node-chromme镜像
[root@localhost yff]# docker pull madehao/selenium-node-chrome
//生成node-chrome容器,且将容器挂在hub容器上
[root@localhost yff]# docker run --name node-chrome --link selenium-hub:hub --shm-size=512m -p 5901:5900 -v /home/node-chrome:/var/node-chrome madehao/selenium-node-chrome

2.3 代码示例

public class Test2 {
    public static void main(String[] args) throws Exception{
    	 //由于相关原因,此处ip地址不展示出来
       //RemoteWebDriver两个参数 第一个为URL 第二个为执行对象,这里为谷歌浏览器
        WebDriver driver=new RemoteWebDriver(new URL("http://ip地址:9012/wd/hub/"), DesiredCapabilities.chrome());
        driver.manage().window().maximize();
        driver.get("http://www.baidu.com");
        System.out.println("打开百度");
        Thread.sleep(2000);
        driver.quit();
    }
}

三、扩展使用

    在上面RemoteWebDriver后的第二个参数,只是使用DesiredCapabilities.chrome()给了启动浏览器类型,实际中参数不会只设置这么简单。下面给出两种扩展参数的使用:

3.1 浏览器版本和平台参数

public class Test3 {
    public static void main(String[] args) throws Exception{
    		//创建DesiredCapabilities对象,并给出浏览器类型、版本号、平台类型
        DesiredCapabilities desiredCapabilities = new DesiredCapabilities("chrome", "81.0.4044.92", Platform.LINUX);
        //创建远程driver对象
        WebDriver driver=new RemoteWebDriver(new URL("http://ip地址:9012/wd/hub/"), desiredCapabilities);
        driver.manage().window().maximize();
        driver.get("http://www.baidu.com");
        System.out.println("打开百度");
        Thread.sleep(2000);
        driver.quit();
    }
}

3.2 浏览器启动相关参数

public class Test4 {
    public static void main(String[] args) throws Exception{
    		//创建DesiredCapabilities对象,并给出浏览器类型、版本号、平台类型
        DesiredCapabilities desiredCapabilities = new DesiredCapabilities("chrome", "81.0.4044.92", Platform.LINUX);
			  //创建ChromeOptions对象,设置启动浏览器相关参数
        ChromeOptions chromeOptions = new ChromeOptions().merge(desiredCapabilities);
        chromeOptions.addArguments("--no-sandbox");
        HashMap<String, Object> hashMap = new HashMap<>();
        hashMap.put("download.default_directory", "指定下载路径字符串");
        chromeOptions.setExperimentalOption("prefs", hashMap);
   			//创建远程driver对象
        WebDriver driver=new RemoteWebDriver(new URL("http://ip地址:9012/wd/hub/"), chromeOptions);
        driver.manage().window().maximize();
        driver.get("http://www.baidu.com");
        System.out.println("打开百度");
        Thread.sleep(2000);
        driver.quit();
    }
}

本人博客目录链接

猜你喜欢

转载自blog.csdn.net/qq_37688023/article/details/105891378