Selenium grid 分布式测试搭建(一)

应领导要求使用 selenium grid 搭建分布式测试平台,于是有了以下操作:

第一步:准备2台电脑,且2台电脑都安装好jdk,都准备好selenium-server-standalone-2.40.0.jar,IEDriver, ChromeDriver等工具,注意chrome版本与chromedriver需要匹配,详见我的另一篇博客:http://www.cnblogs.com/cherrysu/p/7815245.html

第二步:其中一台电脑作为hub,也就是老大,另一台作为node,也就是小弟,两台电脑一定要能ping通。

第三步:hub机上新建HUB.bat, 内容参考:  

java -jar C:\\Software\\selenium\\selenium-server-standalone-2.40.0.jar -role hub

  其中selenium-server-standalone-2.40.0.jar 的版本和路径需要改成自己的。

第四步:node机上新建node.bat,内容参考:  

java -Dwebdriver.chrome.driver="C:\\software\\chromedriver.exe" -Dwebdriver.ie.driver="C:\\software\\IEDriverServer-x64.exe" -jar C:\\libs\\selenium-server-standalone-2.40.0.jar -role node -nodeConfig node.json -hub http://hub机IP:4444/grid/register

  其中 chromedriver.exe,IEDriverServer-x64.exe,standalone都需要改为node机上对应的地址, -nodeConfig node.json可以修改node机上的grid参数,具体内容参考:

{

"capabilities": [
{
"browserName": "chrome",
"maxInstances": 5,
"platform": "WINDOWS",
"version":"51"
},
{
"browserName": "internet explorer",
"maxInstances": 2,
"platform": "WINDOWS",
"webdriver.ie.driver": "IEDriverServer.exe"
}
],
"configuration": {
"proxy": "org.openqa.grid.selenium.proxy.DefaultRemoteProxy",
"maxSession": 5,
"port": 5555,
"register": true,
"registerCycle": 5000,
"hub": "http://192.168.84.209:4444"
}
}

第五步:先运行hub.bat,在运行node.bat,在hub机上打开 http://localhost:4444/grid/console# 查看链接状态

第六步:编写java脚本

package test;

import java.net.MalformedURLException;
import java.net.URL;

import org.openqa.selenium.Platform;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.ie.InternetExplorerDriver;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.openqa.selenium.remote.RemoteWebDriver;

/**
 * 
 * @author Rong
 *
 */

public class RemoteWebDriverUtil {

    static WebDriver driver;


    // 远程调用ie浏览器
    public static WebDriver createRemoteIEDriver() {
        // 指定调用IE进行测试  
        DesiredCapabilities capability = DesiredCapabilities.internetExplorer();
        // 避免IE安全设置里,各个域的安全级别不一致导致的错误  
        capability.setCapability(InternetExplorerDriver.INTRODUCE_FLAKINESS_BY_IGNORING_SECURITY_DOMAINS, true);
        // 连接到selenium hub,远程启动浏览器  
        capability.setPlatform(Platform.XP);
        try {
            driver = new RemoteWebDriver(new URL("http://node机IP:5555/wd/hub"), capability);
        } catch (MalformedURLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return driver;
    }

    // 启用远程调用chrome
    public static WebDriver createRemoteChromeDriver() {
        DesiredCapabilities capability = DesiredCapabilities.chrome();
        capability.setBrowserName("chrome");
        capability.setVersion("62");
        capability.setPlatform(Platform.WINDOWS);
        try {
            driver = new RemoteWebDriver(new URL("http://node机IP:5555/wd/hub"), capability);
        } catch (MalformedURLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return driver;
    }

    // 启用远程调用firefox
    public static WebDriver createRemoteFirefoxDriver() {
        DesiredCapabilities capability = DesiredCapabilities.firefox();
        capability.setBrowserName("firefox");
        capability.setPlatform(Platform.XP);
        try {
            driver = new RemoteWebDriver(new URL("http://node机IP:5555/wd/hub"), capability);
        } catch (MalformedURLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return driver;
    }

}
package test;

import org.junit.Test;
import org.openqa.selenium.By;
import org.openqa.selenium.NoSuchElementException;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;


public class test  extends RemoteWebDriverUtil{
    
    @Test
    public void testBaiduSearch() throws Exception{
        
        WebDriver driver = createRemoteChromeDriver();

        driver.get("http://www.baidu.com");
        WebElement baiduInput = driver.findElement(By.id("kw"));
        baiduInput.sendKeys("Cherry_chrome");
        WebElement btnSearch = driver.findElement(By.id("su"));
        btnSearch.click();
            
        try {
            WebElement CherrySearchResult = driver.findElement(By.cssSelector(".op_dict3_font24.op_dict3_marginRight"));
            System.out.println("Find search result Successfully!");
        } catch (NoSuchElementException e) {
            System.out.println("Can't find search result!, Search failed!");
        }
    }

}

第七步:运行test.java,node机上的chromedriver.exe运行起来了,远程控制搭建搭建完成。

后续请参考:Selenium grid 分布式测试搭建(二)

 

第六步代码转自:http://www.cnblogs.com/longronglang/p/6220277.html

第三步配置文件转自:http://www.360doc.com/content/16/1121/14/36343398_608254453.shtml

 

posted @ 2017-11-20 16:21  CherrySu  阅读(7951)  评论(3编辑  收藏  举报