Selenium私房菜系列8 -- 玩轉Selenium Server

發表于:2011-09-30來源:未知作者:領測軟件測試網采編點擊數: 標簽:selenium
本篇主要是想更進一步介紹Selenium Server的工作原理,這次我們從Selenium Server的交互模式開始。 在《第一個Selenium RC測試案例》中,我們以命令“java -jar selenium-server.jar”啟動了Selenium Server,其實在啟動Selenium Server時,我們還可以

  本篇主要是想更進一步介紹Selenium Server的工作原理,這次我們從Selenium Server的交互模式開始。

  在《第一個Selenium RC測試案例》中,我們以命令“java -jar selenium-server.jar”啟動了Selenium Server,其實在啟動Selenium Server時,我們還可以加上各種參數(具體的參數請參考《Selenium RC服務器命令行參數列表》), 而開啟Selenium Server交互模式的命令為“java -jar selenium-server.jar -interactive”。交互模式,是Selenium Server提供的一種快速的測試方法,你可以對Selenium Server輸入命令從而直接啟動測試。

  1.啟動Selenium Server交互模式

Selenium私房菜系列8 -- 玩轉Selenium Server - swl632 - 我的<STRONG><A  target=博客" border="1" height="432" src="/uploads/allimg/110930/092K12051-0.jpg" width="661" />

  2.在命令行中輸入:cmd=getNewBrowserSession&1=*iexplore&2=http://www.google.com??刂芐elenium Server啟動瀏覽器,以及創建Session。

Selenium私房菜系列8 -- 玩轉Selenium Server - swl632 - 我的博客

  (1).---> Requesting http://localhost:4444/selenium-server/driver?cmd=getNewBrowserSession&1=*iexplore&2=http://www.google.com

  看過《深入了解Selenium RC工作原理(1)》的應該了解:我們所編寫的測試案例,其實是通過發送Http請求實現對Selenium Server的控制,而測試案例所發送的請求就正是:---> Requesting http://localhost:4444/selenium-server/driver?cmd=getNewBrowserSession&1=*iexplore&2=http://www.google.com。我們可以再打開一個IE瀏覽器,在地址欄輸入:http://localhost:4444/selenium-server/driver?cmd=getNewBrowserSession&1=*iexplore&2=http://www.google.com,回車!看,Selenium Server又為此產生了一個Session了!呵呵:>

  (2).這里,Selenium Server為上面的請求隨機生成了一個Session ID:9505f5f8c52041c28f4cdc1f8e59f769(由于寫這篇文章的時候中途重啟了Selenium Server,所以這里和上圖的Session ID不同,并且下文會繼續使用Session ID:9505f5f8c52041c28f4cdc1f8e59f769)。

  (3).如果一切正常,Selenium Server最后會出現Get Result Ok的字樣,并出現如下兩個框框:

Selenium私房菜系列8 -- 玩轉Selenium Server - swl632 - 我的博客

  3.控制瀏覽器訪問www.google.com/webhp,輸入:cmd=open&1=http://www.google.com/webhp&sessionId=9505f5f8c52041c28f4cdc1f8e59f769

Selenium私房菜系列8 -- 玩轉Selenium Server - swl632 - 我的博客

  噢,瀏覽器成功訪問http://www.google.com/webhp了:>。

  總結一下:

  (1).在Selenium Server中輸入命令的格式為:cmd=Command&1=Target&2=Value&SessionID=…,這和Selenium IDE的案例語句很像。

  (2).在輸入命令后,Selenium Server會發條Http請求給自己,請求的URL格式也是固定的:http://localhost:4444/selenium-server/driver?cmd=Command&1=Target&2=Value&SessionID=…,我們完全可以用瀏覽器發送請求控制Selenium Server進行測試。

  (3).另外,sessionId是很重要的一個參數,當一個Selenium Server同時運行多個測試案例時,Selenium Server就是通過sessionId判斷到底該操作哪個瀏覽器窗口。而在下面的C#代碼中:

  ISelenium selenium = new DefaultSelenium("127.0.0.1", 4444, "*iexplore", "http://www.google.com");

  selenium.Start();

  selenium.Open("/webhp");

  selenium就相當于上文中的sessionId。

  (4).在Selenium Server啟動一個Session時,必須先指定一個 “源”(原因見《深入了解Selenium RC工作原理(2)》),在上面的代碼中http://www.google.com就是“源”了,然而這是可能出現問題,請看下面代碼:

  ISelenium selenium = new DefaultSelenium("127.0.0.1", 4444, "*iexplore", "http://www.google.com");

  selenium.Start();

  selenium.Open(http://www.baidu.com);

  我們在啟動Session時,定義了源為http://www.google.com,但在后來的操作中,我們打開的卻是http://www.baidu.com,由于二者非同源,所以接下來的操作就可能會出現各種問題,故此Selenium Server會給出以下警告:

Selenium私房菜系列8 -- 玩轉Selenium Server - swl632 - 我的博客

  Selenium Server提示說;如果測試案例是運行在*iehta或者*chrome上,或者改變Selenium Server的運行模式為proxy injection mode即可避免問題出現。

  恩,在這里,我不得不承認之前在《深入了解Selenium RC工作原理(1)》中,為了簡化問題,我故意少寫了一些東西!

  其實,Selenium Server其實有2種運行模式:

  (1).Heightened Privileges Browsers

  (2).Proxy Injection

  現在Selenium Server啟動的默認模式為:Heightened Privileges Browsers。如果要啟動Proxy Injection模式,可以加參數“-proxyInjectionMode”。而之前在《深入了解Selenium RC工作原理(1)》中介紹Selenium RC與Testcase關系,其實就是在描述Proxy Injection的工作模式,因為我個人認為Proxy Injection設計模式更為合理,所以只對Proxy Injection模式作介紹。在這里我補充說明一下,為什么Heightened Privileges Browsers模式不能避免上面的問題。先看看Selenium Server在Heightened Privileges Browsers模式下的工作流程圖:

原文轉自:http://www.anti-gravitydesign.com

国产97人人超碰caoprom_尤物国产在线一区手机播放_精品国产一区二区三_色天使久久综合给合久久97