Wednesday, January 14, 2015

Selenium in Android - Demo




We want to run our test scripts 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.
Before we can register our simulator we have to download the android SDK (Software Development Kit) from the following location:

Steps to Achieve.
  • Setup the device
  • Install the WebDriver APK 
  • 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:
1

adb devices
2) Download the Android server from Selenium Site and save it in the platform-tools directory. To install the application enter:
1
adb -e install -r android-server.apk
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 :
1
$./adb -s <serialId> forward tcp:8080 tcp:8080

Run the test

You will need to take a look in TestNG Framework

From this program.
  1. Open Android Browser
      driver = new AndroidDriver();

  1. Navigate to http://www.automationplace.blogspot.com
    driver.get("https://automationplace.blogspot.com");

  1. Close the browser.
       driver.quit();


Now we have our environment setup we can run our tests.Create new TestNG and paste the code below :
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
34
35
36
37
38
39
40
41
42
43
44
45
46
package com.selenium.test;

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.android.AndroidDriver;
import org.testng.annotations.Test;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.AfterTest;

import com.selenium.pageObject.LoginPage;
import com.selenium.userAction.PostStatus;
import com.selenium.userAction.SignIn;

public class autoTestLoadingFacebook {
  
  /**
   * Create WebDriver as static variable
   */
  private static WebDriver driver;
  
  
  /**
 * Setup some variable to run your script test
 */
@BeforeTest
  public void beforeTest() {
    driver =  new AndroidDriver();
  }
  
  /**
 * your test script
 */
@Test
  public void f() {
  driver.get("https://automationplace.blogspot.com");
  //create more test script here
  }
  
  
  /**
 * after run your script test , use this code to close your browser
 */
@AfterTest
  public void afterTest() {
    driver.quit();
  }
}
5) Run your test.
Hope you like this post.



1 comment: