在selenium官網看到一篇講述的是關於selenium在android手機上啟動並執行文章,於是也就自己試了試。以android模擬器為例(在手機上運行會更順暢)
配置環境:android SDK4.04+
Android WebDriver
系統內容:winXP
步驟一:安裝模擬器 Android Virtual Device (AVD)
該模擬器的安裝不細說,網上一大堆,也可以不裝直接用自己的手機也可。
我自己採用的是配置了AVD,然後類比手機設定了一個1G的SD卡裝置,將程式安裝在類比的SDK上;
啟動AVD:emulator -avd SDK4.04 -sdcard D:\android-sdk-windows\tools\sdcard.img
步驟二:安裝webdrive
從官網下載webdrive的apk:android-server-2.21.0.apk(地址:http://code.google.com/p/selenium/downloads/list)
下載後類比手機安裝到模擬器的sdk上,安裝好後
頁面上的webDrive代表已經裝好了。
啟動webdrive,映射8080連接埠:adb forward tcp:8080 tcp:8080
啟動後,看到的手機頁面是黑色的螢幕,彈出一個webdrive is already的資訊後便進入無盡的等待中....
這個時候不要以為是自己沒有裝好,而是你手機端的webdrive沒有收到請求,沒有運行而已;
步驟三、編寫junit測試案例
首先依賴包:selenium-java-X.zip
建立junit測試單元,測試google的單元,如下代碼:
import static org.junit.Assert.*;import org.junit.Test;import junit.framework.TestCase;import org.openqa.selenium.By;import org.openqa.selenium.WebDriver;import org.openqa.selenium.WebElement;import org.openqa.selenium.android.AndroidDriver;public class OneTest extends TestCase {public void testGoogle() throws Exception { WebDriver driver = new AndroidDriver(); // And now use this to visit Google driver.get("http://www.google.com"); // Find the text input element by its name WebElement element = driver.findElement(By.name("q")); // Enter something to search for element.sendKeys("Cheese!"); // Now submit the form. WebDriver will find the form for us from the // element element.submit(); // Check the title of the page System.out.println("Page title is: " + driver.getTitle()); driver.quit();}@Testpublic void test() throws Exception { this.testGoogle(); fail("Not yet implemented");}}
測試單元也編寫完成了,webdrive也在運行就緒狀態,這個時候便是體現是否可行的時機了,
我們運行junit單元,仔細觀察手機:
繼續運行:
至此完成手機上webdrive的測實驗證。
目前webdrive手機上測試只支援的Andriod手機系統為:Gingerbread (2.3.x), Honeycomb (3.x), Ice Cream Sandwich (4.0.x) and later
另外IOS支援至少4.2以上的版本。
參考文檔:http://code.google.com/p/selenium/wiki/AndroidDriver
有興趣的可以繼續參閱後續的junit+webdrive的測試架構搭建!