转自 http://blog.csdn.net/jiguanghoverli/article/details/8668919

Introduction

Android WebDriver allows to run automated end-to-end tests that ensure your site works correctly when viewed from the Android browser. Android WebDriver supports all core WebDriver APIs, and in addition to that it supports mobile spacific and HTML5 APIs. Android WebDriver models many user interactions such as finger taps, flicks, finger scrolls and long presses. It can rotate the display and interact with HTML5 features such as local storage, session storage and application cache.

We try to stay as close as possible to what the user interaction with the browser is. To do so, Android WebDriver runs the tests against a WebView (rendering component used by the Android browser) configured like the Android browser. To interact with the page Android WebDriver uses native touch and key events. To query the DOM, it uses the JavaScript Atoms libraries.

Supported Platforms

  • Thecurrent apkwill only work with Gingerbread (2.3.x), Honeycomb (3.x), Ice Cream Sandwich (4.0.x) and later.
    • Note that there is anemulator bug on Gingerbreadthat might cause WebDriver to crash.
  • The last version to support Froyo (2.2) is2.16.

Useful Related Links

  • Android WebDriver on Android Blog
  • Android WebDriver on Google Open Source Blog

Get Started

Install the Android SDK

Download theAndroid SDK, and unpack it to ~/android_sdk/. NOTE: The location of the Android SDK must exist in../android_sdk, relative to the directory containing the Selenium repository.

Setup the Environment

Android WebDriver test can run on emulators or real devices for phone and tablets.

Setup the Emulator

To create an emulator, you can use thegraphical interfaceprovided by the Android SDK, or thecommand line. Below are instructions for using the command line.

First, let's create an Android Virtual Device (AVD):

<span class="pln">$cd </span><span class="pun" style="color:#66660;">~</span><span class="str" style="color:#0880;">/android_sdk/</span><span class="pln">tools</span><span class="pun" style="color:#66660;">/</span><span class="pln">
$</span><span class="pun" style="color:#66660;">./</span><span class="pln">android create avd </span><span class="pun" style="color:#66660;">-</span><span class="pln">n my_android </span><span class="pun" style="color:#66660;">-</span><span class="pln">t </span><span class="lit" style="color:#06666;">12</span><span class="pln"> </span><span class="pun" style="color:#66660;">-</span><span class="pln">c </span><span class="lit" style="color:#06666;">100M</span>

-n: specifies the name of the AVD.

-t: specifies the platform target. For an exhaustive list of targets, run:

<span class="pun" style="color:#66660;">./</span><span class="pln">android list targets</span>

Make sure the target level you selected corresponds to a supported SDK platform.

-c: specifies the SD card storage space.

When prompted "Do you wish to create a custom hardware profile[no]" enter "no".

Note: An Android emulator bug prevents WebDriver from running on emulators on platforms 2.3 (Gingerbread). Please refer toAndroidDriver#Supported_Platformsfor more information

Now, start the emulator. This can take a while, but take a look at the FAQ below for tips on how to improve performance of the emulator.

<span class="pln">$</span><span class="pun" style="color:#66660;">./</span><span class="pln">emulator </span><span class="pun" style="color:#66660;">-</span><span class="pln">avd my_android </span><span class="pun" style="color:#66660;">&</span>

Setup the Device

Simply connect your Android device through USB to your machine.

Now that we're done with the test environment setup, let's take a look at how to write tests. There are two options available to you to run Android WebDriver tests:

  • Using theremote WebDriver server
  • Using theAndroid test framework
Here is a comparaison of both approach to help you choose which one suits you best.

Android WebDriver using the remote server Android WebDriver using the Android test framework
Tests can be written in any supported language binding (Java, Python, Ruby, etc.) Tests can only br written in Java
Runs slower because every command makes extra RPCs (HTTP requests/responses) Runs fasters because the tests are on the device
Ideal if you use the same WebDriver tests on other browsers Ideal if you don't use the same tests on other browsers and if you already use the Android test framework

Using the Remote Server

This approach has a client and a server component. The client consists of your typical Junit tests that you can run from your favorite IDE or through the command line. The server is an Android application that contains an HTTP server. When you run the tests, each WebDriver command will make a RESTful HTTP request to the server using JSON according to awell defined protocol. The remote server will delegate the request to Android WebDriver, and will then return a response.

Install the WebDriver APK

Every device or emulator has a serial ID. Run this command to get the serial ID of the device or emulator you want to use:

<span class="pln">$</span><span class="pun" style="color:#66660;">~</span><span class="str" style="color:#0880;">/android_sdk/</span><span class="pln">platform</span><span class="pun" style="color:#66660;">-</span><span class="pln">tools</span><span class="pun" style="color:#66660;">/</span><span class="pln">adb devices</span>

Download the Android server from ourdownloads page. To install the application do:

<span class="pln">$</span><span class="pun" style="color:#66660;">./</span><span class="pln">adb </span><span class="pun" style="color:#66660;">-</span><span class="pln">s </span><span class="str" style="color:#0880;"><serialId></span><span class="pln"> </span><span class="pun" style="color:#66660;">-</span><span class="pln">e install </span><span class="pun" style="color:#66660;">-</span><span class="pln">r android</span><span class="pun" style="color:#66660;">-</span><span class="pln">server</span><span class="pun" style="color:#66660;">.</span><span class="pln">apk</span>

Make sure you are allowing installation of application not coming from Android Market. Go to Settings -> Applications, and check "Unknown Sources".

Start the Android WebDriver application through the UI of the device or by running this command:

<span class="pln">$</span><span class="pun" style="color:#66660;">./</span><span class="pln">adb </span><span class="pun" style="color:#66660;">-</span><span class="pln">s </span><span class="str" style="color:#0880;"><serialId></span><span class="pln"> shell am start </span><span class="pun" style="color:#66660;">-</span><span class="pln">a android</span><span class="pun" style="color:#66660;">.</span><span class="pln">intent</span><span class="pun" style="color:#66660;">.</span><span class="pln">action</span><span class="pun" style="color:#66660;">.</span><span class="pln">MAIN </span><span class="pun" style="color:#66660;">-</span><span class="pln">n org</span><span class="pun" style="color:#66660;">.</span><span class="pln">openqa</span><span class="pun" style="color:#66660;">.</span><span class="pln">selenium</span><span class="pun" style="color:#66660;">.</span><span class="pln">android</span><span class="pun" style="color:#66660;">.</span><span class="pln">app</span><span class="pun" style="color:#66660;">/.</span><span class="typ" style="color:#66066;">MainActivity</span>

You can start the application in debug mode, which has more verbose logs by doing:

<span class="pln">$</span><span class="pun" style="color:#66660;">./</span><span class="pln">adb </span><span class="pun" style="color:#66660;">-</span><span class="pln">s </span><span class="str" style="color:#0880;"><serialId></span><span class="pln"> shell am start </span><span class="pun" style="color:#66660;">-</span><span class="pln">a android</span><span class="pun" style="color:#66660;">.</span><span class="pln">intent</span><span class="pun" style="color:#66660;">.</span><span class="pln">action</span><span class="pun" style="color:#66660;">.</span><span class="pln">MAIN </span><span class="pun" style="color:#66660;">-</span><span class="pln">n org</span><span class="pun" style="color:#66660;">.</span><span class="pln">openqa</span><span class="pun" style="color:#66660;">.</span><span class="pln">selenium</span><span class="pun" style="color:#66660;">.</span><span class="pln">android</span><span class="pun" style="color:#66660;">.</span><span class="pln">app</span><span class="pun" style="color:#66660;">/.</span><span class="typ" style="color:#66066;">MainActivity</span><span class="pln"> </span><span class="pun" style="color:#66660;">-</span><span class="pln">e debug </span><span class="kwd" style="color:#0088;">true</span>

Now we need to setup the port forwarding in order to forward traffic from the host machine to the emulator. In a terminal type:

<span class="pln">$</span><span class="pun" style="color:#66660;">./</span><span class="pln">adb </span><span class="pun" style="color:#66660;">-</span><span class="pln">s </span><span class="str" style="color:#0880;"><serialId></span><span class="pln"> forward tcp</span><span class="pun" style="color:#66660;">:</span><span class="lit" style="color:#06666;">8080</span><span class="pln"> tcp</span><span class="pun" style="color:#66660;">:</span><span class="lit" style="color:#06666;">8080</span>

This will make the android server available athttp://localhost:8080/wd/hubfrom the host machine. You're now ready to run the tests. Let's take a look at some code.

Run the Tests

<span class="kwd" style="color:#0088;">import</span><span class="pln"> junit</span><span class="pun" style="color:#66660;">.</span><span class="pln">framework</span><span class="pun" style="color:#66660;">.</span><span class="typ" style="color:#66066;">TestCase</span><span class="pun" style="color:#66660;">;</span><span class="pln">

</span><span class="kwd" style="color:#0088;">import</span><span class="pln"> org</span><span class="pun" style="color:#66660;">.</span><span class="pln">openqa</span><span class="pun" style="color:#66660;">.</span><span class="pln">selenium</span><span class="pun" style="color:#66660;">.</span><span class="typ" style="color:#66066;">By</span><span class="pun" style="color:#66660;">;</span><span class="pln">
</span><span class="kwd" style="color:#0088;">import</span><span class="pln"> org</span><span class="pun" style="color:#66660;">.</span><span class="pln">openqa</span><span class="pun" style="color:#66660;">.</span><span class="pln">selenium</span><span class="pun" style="color:#66660;">.</span><span class="typ" style="color:#66066;">WebElement</span><span class="pun" style="color:#66660;">;</span><span class="pln">
</span><span class="kwd" style="color:#0088;">import</span><span class="pln"> org</span><span class="pun" style="color:#66660;">.</span><span class="pln">openqa</span><span class="pun" style="color:#66660;">.</span><span class="pln">selenium</span><span class="pun" style="color:#66660;">.</span><span class="pln">android</span><span class="pun" style="color:#66660;">.</span><span class="typ" style="color:#66066;">AndroidDriver</span><span class="pun" style="color:#66660;">;</span><span class="pln">

</span><span class="kwd" style="color:#0088;">public</span><span class="pln"> </span><span class="kwd" style="color:#0088;">class</span><span class="pln"> </span><span class="typ" style="color:#66066;">OneTest</span><span class="pln"> </span><span class="kwd" style="color:#0088;">extends</span><span class="pln"> </span><span class="typ" style="color:#66066;">TestCase</span><span class="pln"> </span><span class="pun" style="color:#66660;">{</span><span class="pln">

</span><span class="kwd" style="color:#0088;">public</span><span class="pln"> </span><span class="kwd" style="color:#0088;">void</span><span class="pln"> testGoogle</span><span class="pun" style="color:#66660;">()</span><span class="pln"> </span><span class="kwd" style="color:#0088;">throws</span><span class="pln"> </span><span class="typ" style="color:#66066;">Exception</span><span class="pln"> </span><span class="pun" style="color:#66660;">{</span><span class="pln">
</span><span class="typ" style="color:#66066;">WebDriver</span><span class="pln"> driver </span><span class="pun" style="color:#66660;">=</span><span class="pln"> </span><span class="kwd" style="color:#0088;">new</span><span class="pln"> </span><span class="typ" style="color:#66066;">AndroidDriver</span><span class="pun" style="color:#66660;">();</span><span class="pln">

</span><span class="com" style="color:#8800;">// And now use this to visit Google</span><span class="pln">
driver</span><span class="pun" style="color:#66660;">.</span><span class="kwd" style="color:#0088;">get</span><span class="pun" style="color:#66660;">(</span><span class="str" style="color:#0880;">"http://www.google.com"</span><span class="pun" style="color:#66660;">);</span><span class="pln">

</span><span class="com" style="color:#8800;">// Find the text input element by its name</span><span class="pln">
</span><span class="typ" style="color:#66066;">WebElement</span><span class="pln"> element </span><span class="pun" style="color:#66660;">=</span><span class="pln"> driver</span><span class="pun" style="color:#66660;">.</span><span class="pln">findElement</span><span class="pun" style="color:#66660;">(</span><span class="typ" style="color:#66066;">By</span><span class="pun" style="color:#66660;">.</span><span class="pln">name</span><span class="pun" style="color:#66660;">(</span><span class="str" style="color:#0880;">"q"</span><span class="pun" style="color:#66660;">));</span><span class="pln">

</span><span class="com" style="color:#8800;">// Enter something to search for</span><span class="pln">
element</span><span class="pun" style="color:#66660;">.</span><span class="pln">sendKeys</span><span class="pun" style="color:#66660;">(</span><span class="str" style="color:#0880;">"Cheese!"</span><span class="pun" style="color:#66660;">);</span><span class="pln">

</span><span class="com" style="color:#8800;">// Now submit the form. WebDriver will find the form for us from the element</span><span class="pln">
element</span><span class="pun" style="color:#66660;">.</span><span class="pln">submit</span><span class="pun" style="color:#66660;">();</span><span class="pln">

</span><span class="com" style="color:#8800;">// Check the title of the page</span><span class="pln">
</span><span class="typ" style="color:#66066;">System</span><span class="pun" style="color:#66660;">.</span><span class="kwd" style="color:#0088;">out</span><span class="pun" style="color:#66660;">.</span><span class="pln">println</span><span class="pun" style="color:#66660;">(</span><span class="str" style="color:#0880;">"Page title is: "</span><span class="pln"> </span><span class="pun" style="color:#66660;">+</span><span class="pln"> driver</span><span class="pun" style="color:#66660;">.</span><span class="pln">getTitle</span><span class="pun" style="color:#66660;">());</span><span class="pln">
driver</span><span class="pun" style="color:#66660;">.</span><span class="pln">quit</span><span class="pun" style="color:#66660;">();</span><span class="pln">
</span><span class="pun" style="color:#66660;">}</span><span class="pln">
</span><span class="pun" style="color:#66660;">}</span>

To compile and run this example you will need the selenium-java-X.zip (client side piece of selenium). Download the selenium-java-X.zip from ourdownload page, unzip and include all jars in your IDE project. For Eclipse, right click on project -> Build Path -> Configure Build Path -> Libraries -> Add External Jars.

Using the Android Test Framework

This approach runs the tests directly on the device using theAndroid test framework.

You first need to download Android WebDriver, which is an SDK extra. Open the Android SDK manager and select: Available packages → Extras → Google → WebDriver. Once the SDK manager has downloaded WebDriver, the jars will be under the directory android_sdk/extras/google/webdriver/ . In the same directory you will also find sample code containing and Android project named "SimpleApp" and an Android test project called "TestAnAndroidWebApp".

The SimpleApp project, contains an activity with no layout:

<span class="kwd" style="color:#0088;">public</span><span class="pln"> </span><span class="kwd" style="color:#0088;">class</span><span class="pln"> </span><span class="typ" style="color:#66066;">SimpleAppActivity</span><span class="pln"> </span><span class="kwd" style="color:#0088;">extends</span><span class="pln"> </span><span class="typ" style="color:#66066;">Activity</span><span class="pln"> </span><span class="pun" style="color:#66660;">{</span><span class="pln">
</span><span class="lit" style="color:#06666;">@Override</span><span class="pln">
</span><span class="kwd" style="color:#0088;">public</span><span class="pln"> </span><span class="kwd" style="color:#0088;">void</span><span class="pln"> onCreate</span><span class="pun" style="color:#66660;">(</span><span class="typ" style="color:#66066;">Bundle</span><span class="pln"> savedInstanceState</span><span class="pun" style="color:#66660;">)</span><span class="pln"> </span><span class="pun" style="color:#66660;">{</span><span class="pln">
</span><span class="kwd" style="color:#0088;">super</span><span class="pun" style="color:#66660;">.</span><span class="pln">onCreate</span><span class="pun" style="color:#66660;">(</span><span class="pln">savedInstanceState</span><span class="pun" style="color:#66660;">);</span><span class="pln">
</span><span class="pun" style="color:#66660;">}</span><span class="pln">
</span><span class="pun" style="color:#66660;">}</span>

The reason the activity does not have any layout is because Android WebDriver will create the WebView and automatically set the layout in that activity.

The TestAnAndroidWebApp is an Android test project which contains a simple test that open www.google.com, makes a search and validates that the result page's title contains the word "Google". Let's take a look at the code.

We instantiate the driver by passing the activity in which the WebView will run.

<span class="typ" style="color:#66066;">WebDriver</span><span class="pln"> driver </span><span class="pun" style="color:#66660;">=</span><span class="pln"> </span><span class="kwd" style="color:#0088;">new</span><span class="pln"> </span><span class="typ" style="color:#66066;">AndroidWebDriver</span><span class="pun" style="color:#66660;">(</span><span class="pln">getActivity</span><span class="pun" style="color:#66660;">());</span>

Then we use the driver in a test to drive the web application.

<span class="pln">  </span><span class="kwd" style="color:#0088;">public</span><span class="pln"> </span><span class="kwd" style="color:#0088;">void</span><span class="pln"> testGoogleWorks</span><span class="pun" style="color:#66660;">()</span><span class="pln"> </span><span class="pun" style="color:#66660;">{</span><span class="pln">
</span><span class="com" style="color:#8800;">// Loads www.google.com</span><span class="pln">
driver</span><span class="pun" style="color:#66660;">.</span><span class="kwd" style="color:#0088;">get</span><span class="pun" style="color:#66660;">(</span><span class="str" style="color:#0880;">"http://www.google.com"</span><span class="pun" style="color:#66660;">);</span><span class="pln">
</span><span class="com" style="color:#8800;">// Lookup the search box on the page by it's HTML name property</span><span class="pln">
</span><span class="typ" style="color:#66066;">WebElement</span><span class="pln"> searchBox </span><span class="pun" style="color:#66660;">=</span><span class="pln"> driver</span><span class="pun" style="color:#66660;">.</span><span class="pln">findElement</span><span class="pun" style="color:#66660;">(</span><span class="typ" style="color:#66066;">By</span><span class="pun" style="color:#66660;">.</span><span class="pln">name</span><span class="pun" style="color:#66660;">(</span><span class="str" style="color:#0880;">"q"</span><span class="pun" style="color:#66660;">));</span><span class="pln">
</span><span class="com" style="color:#8800;">// Enter keys in the search box</span><span class="pln">
searchBox</span><span class="pun" style="color:#66660;">.</span><span class="pln">sendKeys</span><span class="pun" style="color:#66660;">(</span><span class="str" style="color:#0880;">"Android Rocks!"</span><span class="pun" style="color:#66660;">);</span><span class="pln">
</span><span class="com" style="color:#8800;">// Hit enter</span><span class="pln">
searchBox</span><span class="pun" style="color:#66660;">.</span><span class="pln">submit</span><span class="pun" style="color:#66660;">();</span><span class="pln">
</span><span class="com" style="color:#8800;">// Ensure the title contains "Google"</span><span class="pln">
assertTrue</span><span class="pun" style="color:#66660;">(</span><span class="pln">driver</span><span class="pun" style="color:#66660;">.</span><span class="pln">getTitle</span><span class="pun" style="color:#66660;">().</span><span class="pln">contains</span><span class="pun" style="color:#66660;">(</span><span class="str" style="color:#0880;">"Google"</span><span class="pun" style="color:#66660;">));</span><span class="pln">
</span><span class="com" style="color:#8800;">// Ensure that there is at least one link with the keyword "Android"</span><span class="pln">
assertTrue</span><span class="pun" style="color:#66660;">(</span><span class="pln">driver</span><span class="pun" style="color:#66660;">.</span><span class="pln">findElements</span><span class="pun" style="color:#66660;">(</span><span class="typ" style="color:#66066;">By</span><span class="pun" style="color:#66660;">.</span><span class="pln">partialLinkText</span><span class="pun" style="color:#66660;">(</span><span class="str" style="color:#0880;">"Android"</span><span class="pun" style="color:#66660;">)).</span><span class="pln">size</span><span class="pun" style="color:#66660;">()</span><span class="pln"> </span><span class="pun" style="color:#66660;">></span><span class="pln"> </span><span class="lit" style="color:#06666;">1</span><span class="pun" style="color:#66660;">);</span><span class="pln">
</span><span class="pun" style="color:#66660;">}</span>

Here a are some instructions to help you setup you project.

Configuring from an Eclipse Project

If you are using Eclipse, first make sure that theEclipse ADT pluginis installed .

  1. Open Eclipse
  2. Right click on the Android Test project → Build Path → Configure Build Path... → Libraries → Add External Jars
  3. Select the android_webdriver_library.jar

If you would like to step in the code for debugging or see the javadoc from Eclipse, it’s recommended you link the sources as well.

  1. Open the Referenced Libraries
  2. Right click on android_webdriver_library.jar → Properties → Java Source Attachment
  3. Select android_webdriver_library-srcs.jar

Let's import the examples and run the sample code:

  1. Open Eclipse
  2. File → Import... → General → Existing Projects into Workspace
  3. Select the directory your_android_sdk/extras/google/webdriver which contains SimpleApp and TestAnAndroidWebApp projects.
  4. Right click on TestAnAndroidWebApp → Run As → Android JUnit Test

You will see the tests executing on your device or emulator.

Configuring command line managed projects

If you are managing you project from thecommand line, copy android_webdriver_library.jar under the /libs directory of your Android test project. First we need to build and install the SimpleApp application.

<span class="pln">$ cd </span><span class="typ" style="color:#66066;">SimpleApp</span><span class="pln">
$ adb update project
$ ant debug
$ adb install bin</span><span class="pun" style="color:#66660;">/</span><span class="typ" style="color:#66066;">SimpleAppActivity</span><span class="pun" style="color:#66660;">-</span><span class="pln">debug</span><span class="pun" style="color:#66660;">.</span><span class="pln">apk</span>

Then let's build and install the test application:

<span class="pln">$ cd </span><span class="typ" style="color:#66066;">TestAnAndroidWebApp</span><span class="pln">
$ adb update test project
$ ant debug
$ adb install bin</span><span class="pun" style="color:#66660;">/</span><span class="typ" style="color:#66066;">TestAnAndroidWebApp</span><span class="pun" style="color:#66660;">-</span><span class="pln">debug</span><span class="pun" style="color:#66660;">.</span><span class="pln">apk</span>

Now we can run the tests! Execute the following command to see your tests running:

<span class="pln">$ adb shell am instrument </span><span class="pun" style="color:#66660;">-</span><span class="pln">w simple</span><span class="pun" style="color:#66660;">.</span><span class="pln">app</span><span class="pun" style="color:#66660;">.</span><span class="pln">test</span><span class="pun" style="color:#66660;">/</span><span class="pln">android</span><span class="pun" style="color:#66660;">.</span><span class="pln">test</span><span class="pun" style="color:#66660;">.</span><span class="typ" style="color:#66066;">InstrumentationTestRunner</span>

Android WebDriver requires the following permissions in the application's manifest file to work properly:

  • android.permission.INTERNET
  • android.permission.ACCESS_NETWORK_STATE
  • android.permission.WRITE_SETTINGS
  • android.permission.WAKE_LOCK
  • android.permission.CLEAR_APP_CACHE
  • android.permission.ACCESS_COARSE_LOCATION
  • android.permission.ACCESS_MOCK_LOCATION
  • android.permission.ACCESS_FINE_LOCATION
  • android.permission.ACCESS_LOCATION_EXTRA_COMMANDS
  • android.permission.CHANGE_CONFIGURATION
  • android.permission.SET_DEBUG_APP
  • android.permission.WRITE_EXTERNAL_STORAGE
  • android.permission.WRITE_APN_SETTINGS

Get Involved

Want to fix a bug? Add a feature? Or simply play with the code? Here is a quick tutorial to help you get started with Android WebDriver.

  • Check out the code:
    <span class="pln">$ svn checkout https</span><span class="pun" style="color:#66660;">:</span><span class="com" style="color:#8800;">//selenium.googlecode.com/svn/trunk/ webdriver</span><span class="pln">
    $ cd webdriver</span>

Edit theproperties.ymlfile to reflect the location of your Android SDK, and set the platform target you want to use. If you want a list of all available target, execute the following command:

<span class="pun" style="color:#66660;">./</span><span class="pln">android list targets</span>

On Linux, android SDK is distributed for 32-bits machine. If you have a 64-bit machine you will need to install ia32-libshttp://developer.android.com/sdk/installing.html#troubleshooting

Build the Android WebDriver Code

Android WebDriver consists of 3 parts. The client and the server used to run Android WebDriver tests remotely, and a library. The library does most of the real work and is used by Android WebDriver to control the WebView. The server piece simply wraps the library in an Android application together with the HTTP server.

  • To build the library, the client and the server
    <span class="pln">$</span><span class="pun" style="color:#66660;">./</span><span class="pln">go android</span>
  • To build the client
    <span class="pln">$</span><span class="pun" style="color:#66660;">./</span><span class="pln">go android_client</span>
  • To build the server
    <span class="pln">$</span><span class="pun" style="color:#66660;">./</span><span class="pln">go android_server</span>
  • To build the library
    <span class="pln">$</span><span class="pun" style="color:#66660;">./</span><span class="pln">go </span><span class="com" style="color:#8800;">//android/library/src/java/org/openqa/selenium/android:android_library</span>
  • To run the tests
    <span class="pln">$</span><span class="pun" style="color:#66660;">./</span><span class="pln">go test_android</span>

This command will start the emulator install the Android WebDriver application and start the tests.

If the emulator/device screen is locked, simply unlock it to see the tests running.

Note that "./go test_android" will fail if there is already an emulator running.

Update the Atoms Used by Android WebDriver

First let's build all the fragment Android WebDriver uses:

<span class="pun" style="color:#66660;">./</span><span class="pln">go </span><span class="com" style="color:#8800;">//javascript/android-driver:atoms</span>

Then copy the new AndroidAtoms.java:

<span class="pln">cp build</span><span class="pun" style="color:#66660;">/</span><span class="pln">javascript</span><span class="pun" style="color:#66660;">/</span><span class="pln">android</span><span class="pun" style="color:#66660;">-</span><span class="pln">driver</span><span class="pun" style="color:#66660;">/</span><span class="typ" style="color:#66066;">AndroidAtoms</span><span class="pun" style="color:#66660;">.</span><span class="pln">java java</span><span class="pun" style="color:#66660;">/</span><span class="pln">client</span><span class="pun" style="color:#66660;">/</span><span class="pln">src</span><span class="pun" style="color:#66660;">/</span><span class="pln">org</span><span class="pun" style="color:#66660;">/</span><span class="pln">openqa</span><span class="pun" style="color:#66660;">/</span><span class="pln">selenium</span><span class="pun" style="color:#66660;">/</span><span class="pln">android</span><span class="pun" style="color:#66660;">/</span><span class="pln">library</span><span class="pun" style="color:#66660;">/</span><span class="typ" style="color:#66066;">AndroidAtoms</span><span class="pun" style="color:#66660;">.</span><span class="pln">java</span>

Compile and run the tests to make sure nothing broke! Then make a release of the new APK by following the instructionshere.

Debug on Android

  • Run the tests

It's advised to start your own emulator instance (instructions above), leave the emulator running and run the tests. This will prevent you from paying the cost of starting a new emulator for every test run. To run the tests on an already running emulator:

<span class="pln">$</span><span class="pun" style="color:#66660;">./</span><span class="pln">go </span><span class="com" style="color:#8800;">//java/client/test/org/openqa/selenium/android:java-test:run haltonerror=false haltonfailure=false</span>
  • To run one test class only:
    <span class="pun" style="color:#66660;">./</span><span class="pln">go </span><span class="com" style="color:#8800;">//java/client/test/org/openqa/selenium/android:java-test:run onlyrun=LocalStorageTest haltonerror=false haltonfailure=false</span>
  • To run one test method only:
    <span class="pun" style="color:#66660;">./</span><span class="pln">go </span><span class="com" style="color:#8800;">//java/client/test/org/openqa/selenium/android:java-test:run method=testShouldTreatReadonlyAsAValue</span>
  • To access the Android logs (server side logs):
    <span class="pln">$</span><span class="pun" style="color:#66660;">./</span><span class="pln">adb logcat</span>

The Android logcat logs are the most useful piece of information for debugging and fixing an issue. You can also filter the logs by adding "ActivityMAnager:W" for example to the logcat command.

  • To access the JS console:

- Open the android browser - Type "about:debug" in the address bar, this is a toggle setting that will enable the JS console - Execute Js in the browser by typing javascript:<your JS>, you will see the JS console opening

  • View the client side logs:
    <span class="pln">$ls build</span><span class="pun" style="color:#66660;">/</span><span class="pln">test_logs</span><span class="pun" style="color:#66660;">/</span>

Make a Release

To release an apk, build the new apk:

<span class="pln">$ </span><span class="pun" style="color:#66660;">./</span><span class="pln">go </span><span class="com" style="color:#8800;">//android:android-server</span>

Check-in the new prebuilt apk:

<span class="pln">$ svn ci android</span><span class="pun" style="color:#66660;">/</span><span class="pln">prebuilt</span><span class="pun" style="color:#66660;">/</span><span class="pln">android</span><span class="pun" style="color:#66660;">-</span><span class="pln">server</span><span class="pun" style="color:#66660;">.</span><span class="pln">apk</span>

Upload the new APK on the downloads page with the corresponding release notes.

FAQ

My tests are slow, what can I do?

  • Avoid looking up elements by XPath, it is by far more expensive than using IDs or name.
  • Keep the same emulator instance for all tests. Starting an Android emulator is an expensive operation which can take up to 2 minute depending on your machine specifications. Do this once for your entire test suite. If you need a fresh instance of the driver call WebDriver.quit() which flushes the WebView cache, cookies, user data, etc.
  • Prefer window handles to window names (e.g. String current = driver.getWindowHandle(); driver.switchTo().window(current);). Switching to a window by it's name property is more expensive because it requires injecting Javascript in every open window to fetch the window's name. While handles are saved in map which provides really fast lookup times.

The emulator is too slow to start

The Android emulator can take up to 2 or 3 minutes to start. Consider using theAndroid snapshotfeature, which allows you to start an emulator in less than 2 seconds!

You can speed up the tests by disabling animations, audio, skins, and even by running in headless mode. To do so start the emulator with the options --no-boot-anim, --no-audio, --noskin, and --no-window.

If you start the emulator with -no-window, you can launch the WebDriver app with this command:

<span class="pln">$adb shell am start </span><span class="pun" style="color:#66660;">-</span><span class="pln">a android</span><span class="pun" style="color:#66660;">.</span><span class="pln">intent</span><span class="pun" style="color:#66660;">.</span><span class="pln">action</span><span class="pun" style="color:#66660;">.</span><span class="pln">MAIN </span><span class="pun" style="color:#66660;">-</span><span class="pln">n org</span><span class="pun" style="color:#66660;">.</span><span class="pln">openqa</span><span class="pun" style="color:#66660;">.</span><span class="pln">selenium</span><span class="pun" style="color:#66660;">.</span><span class="pln">android</span><span class="pun" style="color:#66660;">.</span><span class="pln">app</span><span class="pun" style="color:#66660;">/.</span><span class="typ" style="color:#66066;">MainActivity</span>

Or in debug mode:

<span class="pln">$adb shell am start </span><span class="pun" style="color:#66660;">-</span><span class="pln">a android</span><span class="pun" style="color:#66660;">.</span><span class="pln">intent</span><span class="pun" style="color:#66660;">.</span><span class="pln">action</span><span class="pun" style="color:#66660;">.</span><span class="pln">MAIN </span><span class="pun" style="color:#66660;">-</span><span class="pln">n org</span><span class="pun" style="color:#66660;">.</span><span class="pln">openqa</span><span class="pun" style="color:#66660;">.</span><span class="pln">selenium</span><span class="pun" style="color:#66660;">.</span><span class="pln">android</span><span class="pun" style="color:#66660;">.</span><span class="pln">app</span><span class="pun" style="color:#66660;">/.</span><span class="typ" style="color:#66066;">MainActivity</span><span class="pln"> </span><span class="pun" style="color:#66660;">-</span><span class="pln">e debug </span><span class="kwd" style="color:#0088;">true</span>

Geo location does not work

  • Remember to set the following settings on your device: Settings -> Applications -> Development -> Check "USB debugging", "Stay Awake" and "Allow mock locations"

ADB does not find the emulator or device

  • Sometimes there are issues connecting to the device / emulator in which case you can restart adb by doing:
    <span class="pln">$ adb kill</span><span class="pun" style="color:#66660;">-</span><span class="pln">server
    $ adb start</span><span class="pun" style="color:#66660;">-</span><span class="pln">server</span>

How to start the HTTP server Jetty

Jetty is started by the Android WebDriver application when launched.

I get "Page not Available"

Sometimes the Android emulator does not detect the connection of the host machine it runs on. The solution in this case is to restart the emulator.

Android WebDriver fails to load HTTPS pages

You can enable accepting all HTTPS certs by setting a capability as follow:

<span class="typ" style="color:#66066;">DesiredCapabilities</span><span class="pln"> caps </span><span class="pun" style="color:#66660;">=</span><span class="pln"> </span><span class="typ" style="color:#66066;">DesiredCapabilities</span><span class="pun" style="color:#66660;">.</span><span class="pln">android</span><span class="pun" style="color:#66660;">();</span><span class="pln">
caps</span><span class="pun" style="color:#66660;">.</span><span class="pln">setCapability</span><span class="pun" style="color:#66660;">(</span><span class="typ" style="color:#66066;">CapabilityType</span><span class="pun" style="color:#66660;">.</span><span class="pln">ACCEPT_SSL_CERTS</span><span class="pun" style="color:#66660;">,</span><span class="pln"> </span><span class="kwd" style="color:#0088;">true</span><span class="pun" style="color:#66660;">);</span><span class="pln">

</span><span class="typ" style="color:#66066;">AndroidDriver</span><span class="pln"> driver </span><span class="pun" style="color:#66660;">=</span><span class="pln"> </span><span class="kwd" style="color:#0088;">new</span><span class="pln"> </span><span class="typ" style="color:#66066;">AndroidDriver</span><span class="pun" style="color:#66660;">(</span><span class="pln">caps</span><span class="pun" style="color:#66660;">);</span>

Can't talk to the emulator or device

Sometimes executing adb commands will fail because the adb server fails to talk to the device or emulator. To fix this kill the adb server and restart it:

<span class="pln">$adb kill</span><span class="pun" style="color:#66660;">-</span><span class="pln">server

$adb start</span><span class="pun" style="color:#66660;">-</span><span class="pln">server</span>

Inconsistent certificates when installing the apk

You may see the following error when trying to install an apk:

<span class="typ" style="color:#66066;">Failure</span><span class="pln"> </span><span class="pun" style="color:#66660;">[</span><span class="pln">INSTALL_PARSE_FAILED_INCONSISTENT_CERTIFICATES</span><span class="pun" style="color:#66660;">]</span>

This means that the certificates used to sign the apk are different from the certificate used with the already installed apk. To fix this, unistall the current apk:

<span class="pln">$adb uninstall org</span><span class="pun" style="color:#66660;">.</span><span class="pln">openqa</span><span class="pun" style="color:#66660;">.</span><span class="pln">selenium</span><span class="pun" style="color:#66660;">.</span><span class="pln">android</span><span class="pun" style="color:#66660;">.</span><span class="pln">app</span><span class="pun" style="color:#66660;">/.</span><span class="typ" style="color:#66066;">MainActivity</span>

You can also uninstall an app manually, by opening the Settings -> Applications -> select WebDriver -> Uninstall.

Can't access the Android WebDriver server from another machine

If you want to access the Android WebDriver server from another machine (or an interface which isn't localhost), do the following after doing the port forwarding:

<span class="com" style="color:#8800;"># Instal socat, one time setup</span><span class="pln">
$sudo apt</span><span class="pun" style="color:#66660;">-</span><span class="kwd" style="color:#0088;">get</span><span class="pln"> install socat

$socat TCP</span><span class="pun" style="color:#66660;">-</span><span class="pln">LISTEN</span><span class="pun" style="color:#66660;">:</span><span class="lit" style="color:#06666;">8081</span><span class="pun" style="color:#66660;">,</span><span class="pln">fork TCP</span><span class="pun" style="color:#66660;">:</span><span class="pln">localhost</span><span class="pun" style="color:#66660;">:</span><span class="lit" style="color:#06666;">8080</span>

Now the Android WebDriver server can be accessed fromhttp://hostname:8081/wd/hubfrom any machine or network interface.

X compatibility

Note: The android emulator is not compatible with xvfb, and will not run in it. If you need a headless X environment, run your emulator with the -no-window option, or use tightvncserver.

转载自:http://code.google.com/p/selenium/wiki/AndroidDriver

////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

我的安装步骤:

1. 在ubuntu 系统下首先安装selenium,运行命令: sudo pip install -U selenium,输入密码

2. 在模拟器上安装android-server.apk,必须在模拟器android2.2上面安装。

3.adb-s<serialId> shell am start -a android.intent.action.MAIN-n org.openqa.selenium.android.app/.MainActivity

4. adb-s<serialId> forward tcp:8080 tcp:8080

5. 然后按照eclipse中的配置运行就可以了

6. Selenium安装:

https://pypi.python.org/pypi/selenium#downloads

7. 代码地址:

https://github.com/truebit/AndroidWebDriver4Python


更多相关文章

  1. 【血泪】SDL终于移植成功,并且在模拟器上跑起来了 花了
  2. 不幸的是,在声明按钮时,模拟器中出现了错误
  3. Android开发系列: 电脑端Android模拟器安装使用教程

随机推荐

  1. Mac和Windows上Android(安卓)Studio常用
  2. Android如何播放GIF动画
  3. [置顶] Android(安卓)WebKit消息处理
  4. android退出应用方式归纳总结
  5. Android_UI_实现TabHost的两种方法
  6. Could not find property 'outputFile' o
  7. Android的AIDL服务
  8. android NDK编译多个so文件
  9. android中调用共享库.so的几种方法
  10. android StrictMode应用