[自動化測試] Junit+webdrive 實現android手機測試

來源:互聯網
上載者:User

在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的測試架構搭建!

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.