slemium webdriver 支持多種情況模擬用戶操作瀏覽器。如:IE,chrome,Firefox等。同時,selenium還有一種HtmlUnit Driver驅動能夠在不啟動瀏覽器的情況下運行相關測試用例。
利用HtmlUnit Driver方式運行測試用例大大減少了等待瀏覽器展現的時間。
如果需要使用HtmlUnit Driver方式,需要下載一個selenium-server-standalone.jar程序,進行模擬代理服務器運行。
運行selenium-server-standalone.jar程序前提條件是機器上安裝了java運行環境。
由於selenium官網總是不能訪問,只能從其他地方下載相關程序。selenium-server-standalone.jar程序可以從github.com的網站上獲得。我找了一個項目從中獲取了該程序。地址如下:
在機器存放selenium-server-standalone.jar的路徑下運行命令:
java -jar selenium-server-standalone.jar -port 4446
端口號一般設定為4444,我自己設定為4446.
程序啟動后,在瀏覽器中輸入:
可以看到服務啟動后的session信息。用於實時監控運行情況。
在python代碼中調用HtmlUnit Driver命令如下:
from selenium import webdriver
driver = webdriver.Remote("
http://localhost:4446/wd/hub", desired_capabilities=webdriver.DesiredCapabilities.HTMLUNIT)
或者 (支持javascript方式)
driver = webdriver.Remote("
http://localhost:4446/wd/hub", desired_capabilities=webdriver.DesiredCapabilities.HTMLUNITWITHJS)
運行程序后,可以看到瀏覽器沒有啟動,但是在selenium-server-standalone.jar程序的運行cmd窗口和
http://localhost:4446/wd/hub/static/resource/hub.html頁面中均顯示了相關的執行信息。