selenium 測試框架中使用grid

to be crazy發表於2015-06-09

之前的測試框架:http://www.cnblogs.com/tobecrazy/p/4553444.html

配合Jenkins可持續整合:http://www.cnblogs.com/tobecrazy/p/4529399.html

在測試框架中使用Log4J 2 :http://www.cnblogs.com/tobecrazy/p/4557592.html

首先介紹一下grid ,selenium grid 是一種執行測試用例時使用的包含不同平臺(windows、Linux、Android)的框架,並且

這些平臺是由一箇中心點控制,這個中心點稱之為HUB,而那些不同的平臺稱之為NODE

其結構如下:

為什麼使用selenium grid:

如果你的程式需要在不用的瀏覽器,不同的作業系統上測試,而且比較多的case需要多執行緒遠端執行,那麼一個比較好的solution就是使用grid.selenium-grid是用於設計幫助我們進行分散式測試的工具,其整個結構是由一個hub節點和若干個代理節點組成。hub用來管理各個代理節點的註冊和狀態資訊,並且接受遠端客戶端程式碼的請求呼叫,然後把請求的命令再轉發給代理節點來執行。

怎麼使用:

首先啟用HUB:

在A機器下載:selenium standalone 4.6:http://pan.baidu.com/s/1qWE7SD2

然後建立HUB.bat

內容為:

1 java -jar selenium-server-standalone-2.46.0.jar -role hub 

其預設監聽埠4444,預設IP localhost  如果要修改,只需要加-port 引數和-Hubhost 

java -jar selenium-server-standalone-2.46.0.jar -role hub  -port 1235 -Hubhost 192.168.2.45

接下來在B機新增node ,建立Node.bat,這裡使用的是預設的Hubhost Ip 和埠

1 java -jar selenium-server-standalone-2.46.0.jar -role node -hub http://localhost:4444/grid/register

為了使用chrome和IE driver,我們需要這樣設定

1 java -Dwebdriver.ie.driver="C:\Users\workspace\Demo\webDriver\IEDriverServer.exe" -Dwebdriver.chrome.driver="C:\Users\workspace\Demo\webDriver\chromedriver.exe" -jar selenium-server-standalone-2.46.0.jar -role node -hub http://localhost:4444/grid/register

分別啟動這兩個bat

若使用remote Driver,需要設定這樣的引數

DesiredCapabilities capability = DesiredCapabilities.firefox();
        WebDriver driver = new RemoteWebDriver(new URL("http://localhost:4444/wd/hub"), capability);
        capability.setBrowserName("firefox" ); 
        capability.setVersion("3.6");

所以我們索性建立一個bean

 1 /**
 2  * 
 3  */
 4 package com.dbyl.libarary.utils;
 5 
 6 /**
 7  * for remote browser bean
 8  * @author Young
 9  *
10  */
11 public class RemoteBrowserBean {
12     private String browserName;
13     private String version;
14     private String[] platform;
15     private String hubURL;
16     public String getBrowserName() {
17         return browserName;
18     }
19     
20     public RemoteBrowserBean()
21     {
22         this.browserName="firefox";
23         this.version="38";
24         this.platform=new String[]{"VISTA", "windows 7"};
25         this.hubURL="http://localhost:4444/wd/hub";
26         
27     }
28     
29     public RemoteBrowserBean(String browser)
30     {
31         this.browserName=browser;
32         this.version="42";
33         this.platform=new String[]{"VISTA", "windows 7"};
34         this.hubURL="http://localhost:4444/wd/hub";
35         
36     }
37     
38     public void setBrowserName(String browserName) {
39         this.browserName = browserName;
40     }
41     public String getVersion() {
42         return version;
43     }
44     public void setVersion(String version) {
45         this.version = version;
46     }
47  
48     
49     public String[] getPlatform() {
50         return platform;
51     }
52 
53     public void setPlatform(String[] platform) {
54         this.platform = platform;
55     }
56 
57     public String getHubURL() {
58         return hubURL;
59     }
60     public void setHubURL(String hubURL) {
61         this.hubURL = hubURL;
62     }
63     
64 
65 }

然後在DriverFactory裡建立getRemoteDriver

 1 /**
 2      * This method will create RemoteWebdriver
 3      * @author Young
 4      * @param remoteBrowserBean
 5      * @return WebDriver
 6      */
 7     public static WebDriver getRemoteDriver(RemoteBrowserBean remoteBrowserBean) {
 8         DesiredCapabilities capability = null;
 9         if (remoteBrowserBean.getBrowserName().contains("firefox")) {
10             capability = DesiredCapabilities.firefox();
11         } else if (remoteBrowserBean.getBrowserName().contains("chrome")) {
12             capability = DesiredCapabilities.chrome();
13         }
14 
15         WebDriver driver = null;
16         try {
17             driver = new RemoteWebDriver(
18                     new URL(remoteBrowserBean.getHubURL()), capability);
19         } catch (MalformedURLException e) {
20             e.printStackTrace();
21         }
22         capability.setBrowserName(remoteBrowserBean.getBrowserName());
23         capability.setVersion(remoteBrowserBean.getVersion());
24         capability.setCapability(remoteBrowserBean.getPlatform()[0],
25                 remoteBrowserBean.getPlatform()[1]);
26         driver.manage().window().maximize();
27         return driver;
28     }

接下來就可以在case裡邊使用

1 @BeforeClass(alwaysRun=true)
2     public void beforeTest()
3     {
4         driver = DriverFactory.getRemoteDriver(new RemoteBrowserBean("chrome"));
5     }

 下載地址:https://github.com/tobecrazy/Demo

相關文章