Selenium grid 分佈式測試搭建(一)

應領導要求使用 selenium grid 搭建分佈式測試平臺,因而有了如下操做:css

第一步:準備2臺電腦,且2臺電腦都安裝好jdk,都準備好selenium-server-standalone-2.40.0.jar,IEDriver, ChromeDriver等工具,注意chrome版本與chromedriver須要匹配,詳見個人另外一篇博客:http://www.cnblogs.com/cherrysu/p/7815245.htmlhtml

第二步:其中一臺電腦做爲hub,也就是老大,另外一臺做爲node,也就是小弟,兩臺電腦必定要能ping通。java

第三步:hub機上新建HUB.bat, 內容參考:  node

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

  其中selenium-server-standalone-2.40.0.jar 的版本和路徑須要改爲本身的。web

第四步:node機上新建node.bat,內容參考:  chrome

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參數,具體內容參考:json

{

"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

相關文章
相關標籤/搜索