Tuesday, 23 August 2011

Example - Selenium2 working with Android environment

Aim:
To execute a Selenium2 script against a web browser on the Andrioid emulator

Difficulty Level: Hard

What you will learn?
  • Create a working Android emulator setup.
  • Integration of the environment with Selenium2 APIs
Tips:
  1. Some knowledge of creating/running selenium test scripts would be advantage in working on this Exercise.
  2. For more details on working with Android: Useful Link


Pre-requisites:
1. Android setup on the Eclipse IDE, use link How to setup Android environment over Ecplise IDE?
:To make sure of if Android setup is done,use link How to make sure Android setup into Eclipse?
2. Selenium2 API jar, for downloading these APIs follow the below steps:
  • Go with the link 'http://code.google.com/p/selenium/downloads/list'
  • Download selenium-java-x.x.x.zip, here x.x.x denotes any version.
  • Keep the downloaded APIs anywhere in your PC's hard disk, we would use them in the exercise's steps.
3. Install Android-server-x.x.x.apk file (where x.x.x denotes the version of the latest installer), to make the installation correctly use the exercise link Android server installation

Commands used in example:
  • driver.get(String url)
  • Driver.findElement by id, name.
  • submit()
  • sendkeys()
  • getTitle()

Working Examples Steps:
  • If you have followed Pre-requisites (basically first and third ones) then go with the next step to make the completion of exercise.
  • Run the installed Webdriver server. (once you followed pre-requisite #3 then you would see icon to install the Webdriver server)
  • Open the command-prompt and select the platform-tool directory.
  • Execute the command ‘adb forward tcp:8080 tcp:8080’ for setup the connection.
  • Create Android project on the Eclipse IDE.
  • Add all Selenium2 API jar into the library of already created project, after following Pre-requisite #2 you have already store APIs on your system. Follow the link to get how to add APIs in project's library
  • Run the created Project.
  • See the script run on the Android Emulator.

Script:

Save the code as a AndroidScript.java
package com.example.test;

import junit.framework.TestCase;

import org.openqa.selenium.By;

import org.openqa.selenium.WebElement;

import org.openqa.selenium.android.AndroidDriver;

public class AndroidScript extends TestCase{

public void testGoogle() throws Exception {

System.out.println("Hello Android");

AndroidDriver driver = new AndroidDriver();

driver.get("http://www.google.com");

WebElement element = driver.findElement(By.name("q"));

element.sendKeys("Selenium!");

element.submit();

System.out.println("Page title is: " + driver.getTitle());

driver.quit();

}

}


Helpful Video

2 comments:

  1. Modifications:
    Ensure to download the latest versions.
    The video reflects the version used when recorded.

    ReplyDelete
  2. https://dl-ssl.google.com/android/eclipse/

    ReplyDelete