簡介
Selenium Grid是一種自動化的測試輔助工具,Grid通過利用現有的計算機基礎設施,能加快Web-app的功能測試。利用Grid,可以很方便地同時在多臺機器上和異構環境中並行運行多個測試事例。
Selenium Grid基於Web-app測試工具Selenium,它可以讓您同時並行運行多個Selenium Remote Control。比較好的一點事,它使所有這些Selenium Remote Control顯示爲一個,這樣您在測試中就可以不必操作具體的計算機。
Selenium Grid因爲是基於Selenium RC的,所以它同時支持RC的其它所有語言,如:Ruby, Java, Python, C#, PHP, ...
還有最後一點,Selenium Grid簡單易用。
原理圖
使用
下面,我們就介紹下如何使用Selenium Grid。
準備:
1. 下載安裝ant
http://apache.mirrormax.net/ant/binaries/apache-ant-1.7.0-bin.zip
解壓包到你選擇的任意目錄,然後將 你的解壓路徑/apache-ant-1.7.0/bin 添加的Path變量中。
最後驗證安裝:
$ ant -version
Apache Ant version 1.7.0 compiled on December 13 2006
2. 下載安裝JDK 1.6
http://www.java.com/en/download/index.jsp
安裝後添加java的bin目錄到path變量,然後驗證:
$java -version
java version "1.6.0"
Java(TM) SE Runtime Environment (build 1.6.0-b105)
Java HotSpot(TM) Server VM (build 1.6.0-b105, mixed mode)
3. 下載Selenium Grid
http://selenium-grid.seleniumhq.org/download.html
其中,zip是Windows平臺的,tar.bz2用於其他平臺。下載後解壓縮,然後驗證:
$cd <你的grid目錄>
$ant sanity-check
運行:
1. 運行Selenium Grid
$ ant launch-hub
通過瀏覽器訪問Selenium Grid控制檯驗證Selenium Grid啓動成功:
http://localhost:4444/console
2. 運行Selenium Remote Control
與Grid在同一臺機器上,只要指定不同的端口號,就可以運行多個RC在同一臺機器上:
$ant -Dport=5556 launch-remote-control
$ant -Dport=5557 launch-remote-control
$ant -Dport=5558 launch-remote-control
運行後,通過訪問Grid控制檯,驗證RC註冊成功:
localhost
5555
*firefox
localhost
5556
*firefox
localhost
5557
*firefox
localhost
5558
*firefox
與Gird不在同一臺機器上,假設環境如下圖:
我們三臺機器,一臺機器hub.thoughtworks.com運行grid,其它兩臺rc1和rc2分別運行兩個RC.則我們需要在啓動RC時,使用下列命令指定相應的參數:
$ant -Dport=<port> -Dhost=<hostname> -DhubURL=<hub url> -Denvironment="Firefox on Windows" launch-remote-control
其中,-Dport指定RC的端口號,-Dhost指定RC所在機器的Host名。-DhubURL指定Grid機器的URL,-Denvironment標示RC的瀏覽器和操作平臺,具體可參考Grid控制檯說明。
URL可以使Host Name,也可以是TCP/IP。
按照上圖所示參數運行完畢後,通過Grid控制檯驗證成功結果如下:
Host
Port
Environment
rc1.seleniumhq.org
5555
Firefox on Windows
rc1.seleniumhq.org
5556
Firefox on Windows
rc2.seleniumhq.org
5555
Firefox on Mac
rc2.seleniumhq.org
5556
Firefox on Mac
3. 調用
下面是調用代碼,運行的話需要寫main函數或者與TestNG結合
// seleniumHost Grid地址, seleniumPort Grid端口號, browser 瀏覽器, website 被測對象。
import static com.thoughtworks.selenium.grid.tools.ThreadSafeSeleniumSessionStorage.session;
import static com.thoughtworks.selenium.grid.tools.ThreadSafeSeleniumSessionStorage.startSeleniumSession;
import static org.testng.AssertJUnit.assertEquals;
import static org.testng.AssertJUnit.assertTrue;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Parameters;
/**
* Base class for all tests in Selenium Grid Java examples.
*/
public class FlickrTestBase {
public static final String TIMEOUT = "30000";
protected void startSession() throws Exception {
startSeleniumSession("localhost", 4444, "*iexplore", "http://www.google.cn");
session().setTimeout(TIMEOUT);
}
protected void closeSession() throws Exception {
closeSeleniumSession();
}
protected void runFlickrScenario(String searchString) {
session().open("/");
assertTrue(session().getLocation(), session().getLocation().startsWith("http://www.google.cn"));
session().type("q", searchString);
session().click("btnG");
session().waitForPageToLoad(TIMEOUT);
assertTrue(session().isTextPresent(searchString.split(" ")[0]));
}
public static void main(String[] args) throws Exception {
FlickrTestBase gt = new FlickrTestBase();
gt.startSession();
gt.runFlickrScenario("selenium");
}
}
// seleniumHost Grid地址, seleniumPort Grid端口號, browser 瀏覽器, website 被測對象。
import static com.thoughtworks.selenium.grid.tools.ThreadSafeSeleniumSessionStorage.session;
import static com.thoughtworks.selenium.grid.tools.ThreadSafeSeleniumSessionStorage.startSeleniumSession;
import static org.testng.AssertJUnit.assertEquals;
import static org.testng.AssertJUnit.assertTrue;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Parameters;
/**
* Base class for all tests in Selenium Grid Java examples.
*/
public class FlickrTestBase {
public static final String TIMEOUT = "30000";
protected void startSession() throws Exception {
startSeleniumSession("localhost", 4444, "*iexplore", "http://www.google.cn");
session().setTimeout(TIMEOUT);
}
protected void closeSession() throws Exception {
closeSeleniumSession();
}
protected void runFlickrScenario(String searchString) {
session().open("/");
assertTrue(session().getLocation(), session().getLocation().startsWith("http://www.google.cn"));
session().type("q", searchString);
session().click("btnG");
session().waitForPageToLoad(TIMEOUT);
assertTrue(session().isTextPresent(searchString.split(" ")[0]));
}
public static void main(String[] args) throws Exception {
FlickrTestBase gt = new FlickrTestBase();
gt.startSession();
gt.runFlickrScenario("selenium");
}
}
Selenium Grid FAQ:
http://selenium-grid.seleniumhq.org/faq.html
本文來自CSDN博客,轉載請標明出處:http://blog.csdn.net/iamqa/archive/2009/08/06/4419548.aspx