Running tests on an Android simulator

Probleem

The android driver allows us to execute our tests against an Android browser. This can be a simulator or a real device. This recipe will walk us through the steps we have to do to run our tests in a simulator.

Voorwaarden

Before we can register our simulator we have to download the android SDK (Software Development Kit) from the following location: http://developer.android.com/sdk/

Oplossing

We can divide this section into three parts: setup the emulator, install the WebDriver APK and finally run the test.

Setup the emulator

Navigate to the tools directory and create an Android Virtual Device.


$cd ~/android_sdk/tools/
$./android create avd -n my_android -t 12 -c 100M
1
2
$cd ~/android_sdk/tools/
$./android create avd -n my_android -t 12 -c 100M

-n: specifies the name of the AVD -t: specifies the platform target -c: specifies the SD card storage space

We can list the targets with the following command to check if the creation succeeded:

./android list targets

Finally we can start the emulator with the following command:

$./emulator -avd my_android &

Install the WebDriver APK

  1. We need to retrieve the serial id with the following command: $~/android_sdk/platform-tools/adb devices
  2. Download the Android server from http://code.google.com/p/selenium/downloads/list and save it in the platform-tools directory. To install the application enter: $./adb -s -e install -r android-server.apk
  3. Start the Android WebDriver application,by running this command: $./adb -s shell am start -a android.intent.action.MAIN -n org.openqa.selenium.android.app/.MainActivity
  4. Now we need to setup the port forwarding in order to forward traffic from the host machine to the emulator. Enter the following in the terminal: $./adb -s forward tcp:8080 tcp:8080

Run the test

Now we have our environment setup we can run our tests. The test will look like this:


import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.android.AndroidDriver;
import org.testng.Assert;
import org.testng.annotations.AfterClass;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.Test;

public class MobileAndroidDevice {
    private static WebDriver driver;
    
    @BeforeClass
    public void setUp() throws Exception {
        driver = new AndroidDriver();
        driver.get("http://selenium.polteq.com/prestashop/");
    }
    
    @AfterClass
    public void tearDown() {
        driver.close();
        driver.quit();
    }
    
    @Test
    public void measurePerformance() throws InterruptedException {
        Thread.sleep(1500);
        driver.findElement(By.cssSelector("input#search_query_top")).sendKeys("ipod nano");
        driver.findElement(By.cssSelector("input[name='submit_search']")).click();
        String searchHeader = driver.findElement(By.cssSelector("H1")).getText().toLowerCase();
            
        Assert.assertTrue(searchHeader.contains("ipod nano"));
    }
}
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.android.AndroidDriver;
import org.testng.Assert;
import org.testng.annotations.AfterClass;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.Test;

public class MobileAndroidDevice {
    private static WebDriver driver;
    
    @BeforeClass
    public void setUp() throws Exception {
        driver = new AndroidDriver();
        driver.get("http://selenium.polteq.com/prestashop/");
    }
    
    @AfterClass
    public void tearDown() {
        driver.close();
        driver.quit();
    }
    
    @Test
    public void measurePerformance() throws InterruptedException {
        Thread.sleep(1500);
        driver.findElement(By.cssSelector("input#search_query_top")).sendKeys("ipod nano");
        driver.findElement(By.cssSelector("input[name='submit_search']")).click();
        String searchHeader = driver.findElement(By.cssSelector("H1")).getText().toLowerCase();
            
        Assert.assertTrue(searchHeader.contains("ipod nano"));
    }
}

Running tests on a real Android device

Probleem

We want to run our testscripts on a real Android device. The android driver allows us to execute our tests against an Android browser. This can be a simulator or a real device.

Voorwaarden

Before we can register our device we have to download the android SDK (Software Development Kit) from the following location: http://developer.android.com/sdk/

Oplossing

We can divide this section into three parts: setup the device, install the WebDriver APK and finally run the test.

Setup the device

Connect the android device with the computer using a USB cable.

Install the WebDriver APK

  1. We need to retrieve the serial id with the following command: /android_sdk/platform-tools/adb devices
  2. Download the Android server from http://code.google.com/p/selenium/downloads/list and save it in the platform-tools directory. To install the application enter: $./adb -s -e install -r android-server.apk
  3. Start the Android WebDriver application,by running this command: $./adb -s shell am start -a android.intent.action.MAIN -n org.openqa.selenium.android.app/.MainActivity
  4. Now we need to setup the port forwarding in order to forward traffic from the host machine to the emulator. Enter the following in the terminal: $./adb -s forward tcp:8080 tcp:8080

Run the test

Now we have our environment setup we can run our tests. The test will look like this:


import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.android.AndroidDriver;
import org.testng.Assert;
import org.testng.annotations.AfterClass;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.Test;

public class MobileAndroidDevice {
    private static WebDriver driver;
    
    @BeforeClass
    public void setUp() throws Exception {
        driver = new AndroidDriver();
        driver.get("http://selenium.polteq.com/prestashop/");
    }
    
    @AfterClass
    public void tearDown() {
        driver.close();
        driver.quit();
    }
    
    @Test
    public void measurePerformance() throws InterruptedException {
        Thread.sleep(1500);
        driver.findElement(By.cssSelector("input#search_query_top")).sendKeys("ipod nano");
        driver.findElement(By.cssSelector("input[name='submit_search']")).click();
        String searchHeader = driver.findElement(By.cssSelector("H1")).getText().toLowerCase();
            
        Assert.assertTrue(searchHeader.contains("ipod nano"));
    }
}
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.android.AndroidDriver;
import org.testng.Assert;
import org.testng.annotations.AfterClass;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.Test;

public class MobileAndroidDevice {
    private static WebDriver driver;
    
    @BeforeClass
    public void setUp() throws Exception {
        driver = new AndroidDriver();
        driver.get("http://selenium.polteq.com/prestashop/");
    }
    
    @AfterClass
    public void tearDown() {
        driver.close();
        driver.quit();
    }
    
    @Test
    public void measurePerformance() throws InterruptedException {
        Thread.sleep(1500);
        driver.findElement(By.cssSelector("input#search_query_top")).sendKeys("ipod nano");
        driver.findElement(By.cssSelector("input[name='submit_search']")).click();
        String searchHeader = driver.findElement(By.cssSelector("H1")).getText().toLowerCase();
            
        Assert.assertTrue(searchHeader.contains("ipod nano"));
    }
}

Running against a real iPhone device

Probleem

We want to execute our testscripts on a iphone device. The iphone driver allows us to execute the WebDriver tests on a real iphone device.

Voorwaarden

We need to install Xcode which is available for the Mac operating systems. We also need a provisoning profile for running the script on a real device.

  1. We need to create a provisioning profile, so go to this website http://developer.apple.com/programs/ios/ and create a profile.
  2. We need to download Xcode from the following location: http://developer.apple.com/iphone
  3. The iPhone WebDriver application is not available from the App Store, therefore we need to check-out the code and build it manually. We can do the check-out by entering the following terminal command: svn checkout http://selenium.googlecode.com/svn/trunk/ selenium-read-only

More information on SVN checkouts: http://www.linuxfromscratch.org/blfs/edguide/chapter03.html

Oplossing

  1. Open the project selenium-read-only/iphone/iWebDriver.xcodeproj in Xcode.
  2. Double-click on the project and set the build configuration to the latest version of the iPhone Simulator, by using the drop-down box in the top left corner.
  3. Configure and install the iPhone provisioning profile.
  4. Open Info.plist and edit the Bundle Identifier to com.NAME.$ {PRODUCT_NAME:identifier} where NAME is the name you registered your provisioning profile to be an authority on.
  5. Make sure your device is connected to your computer. Your device must also be routable from your computer. The easiest way to do this is to configure a wifi network and connect your device to it.
  6. Click on Run (play button). After compiling, the iphone simulator should appear and the iWebDriver app will be installed in it.
  7. Now we can use the IPhoneDriver in our tests.

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.iphone.IPhoneDriver;
import org.testng.Assert;
import org.testng.annotations.AfterClass;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.Test;

public class MobileIphoneSimulator {
    private static WebDriver driver;
    
    @BeforeClass
    public void setUp() throws Exception {
        driver = new IPhoneDriver();
        driver.get("http://selenium.polteq.com/prestashop/");
    }
    
    @AfterClass
    public void tearDown() {
        driver.close();
        driver.quit();
    }
    
    @Test
    public void measurePerformance() throws InterruptedException {
        Thread.sleep(1500);
        driver.findElement(By.cssSelector("input#search_query_top")).sendKeys("ipod nano");
        driver.findElement(By.cssSelector("input[name='submit_search']")).click();
        String searchHeader = driver.findElement(By.cssSelector("H1")).getText().toLowerCase();
            
        Assert.assertTrue(searchHeader.contains("ipod nano"));
    }
}
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.iphone.IPhoneDriver;
import org.testng.Assert;
import org.testng.annotations.AfterClass;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.Test;

public class MobileIphoneSimulator {
    private static WebDriver driver;
    
    @BeforeClass
    public void setUp() throws Exception {
        driver = new IPhoneDriver();
        driver.get("http://selenium.polteq.com/prestashop/");
    }
    
    @AfterClass
    public void tearDown() {
        driver.close();
        driver.quit();
    }
    
    @Test
    public void measurePerformance() throws InterruptedException {
        Thread.sleep(1500);
        driver.findElement(By.cssSelector("input#search_query_top")).sendKeys("ipod nano");
        driver.findElement(By.cssSelector("input[name='submit_search']")).click();
        String searchHeader = driver.findElement(By.cssSelector("H1")).getText().toLowerCase();
            
        Assert.assertTrue(searchHeader.contains("ipod nano"));
    }
}

Running tests on a Xcode iPhone simulator

Probleem

We want to run our test in a iPhone simulator. The iphone simulator comes with Xcode which is available for the Mac operating systems. This recipe will show us how we can run our WebDriver tests in a iphone simulator.

Voorwaarden

We need to download Xcode from the following location: http://developer.apple.com/iphone

The iPhone WebDriver application is not available from the App Store, therefore we need to check-out the code and build it manually. We can do the check-out by entering the following terminal command: svn checkout http://selenium.googlecode.com/svn/trunk/ selenium-read-only

More information on SVN checkouts: http://www.linuxfromscratch.org/blfs/edguide/chapter03.html

Oplossing

  1. Open the project selenium-read-only/iphone/iWebDriver.xcodeproj in Xcode.
  2. Double-click on the project and set the build configuration to the latest version of the iPhone Simulator, by using the drop-down box in the top left corner.
  3. Click on Run (play button). After compiling, the iphone simulator should appear and the iWebDriver app will be installed in it.
  4. Now we can use the IPhoneDriver in our tests.

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.iphone.IPhoneDriver;
import org.testng.Assert;
import org.testng.annotations.AfterClass;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.Test;

public class MobileIphoneSimulator {
    private static WebDriver driver;
    
    @BeforeClass
    public void setUp() throws Exception {
        driver = new IPhoneDriver();
        driver.get("http://selenium.polteq.com/prestashop/");
    }
    
    @AfterClass
    public void tearDown() {
        driver.close();
        driver.quit();
    }
    
    @Test
    public void measurePerformance() throws InterruptedException {
        Thread.sleep(1500);
        driver.findElement(By.cssSelector("input#search_query_top")).sendKeys("ipod nano");
        driver.findElement(By.cssSelector("input[name='submit_search']")).click();
        String searchHeader = driver.findElement(By.cssSelector("H1")).getText().toLowerCase();
            
        Assert.assertTrue(searchHeader.contains("ipod nano"));
    }
}
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.iphone.IPhoneDriver;
import org.testng.Assert;
import org.testng.annotations.AfterClass;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.Test;

public class MobileIphoneSimulator {
    private static WebDriver driver;
    
    @BeforeClass
    public void setUp() throws Exception {
        driver = new IPhoneDriver();
        driver.get("http://selenium.polteq.com/prestashop/");
    }
    
    @AfterClass
    public void tearDown() {
        driver.close();
        driver.quit();
    }
    
    @Test
    public void measurePerformance() throws InterruptedException {
        Thread.sleep(1500);
        driver.findElement(By.cssSelector("input#search_query_top")).sendKeys("ipod nano");
        driver.findElement(By.cssSelector("input[name='submit_search']")).click();
        String searchHeader = driver.findElement(By.cssSelector("H1")).getText().toLowerCase();
            
        Assert.assertTrue(searchHeader.contains("ipod nano"));
    }
}