Code & QA

Running Tests on Android

Execution of tests on Android OS is not an objective of the framework but I decided to include this possibility as an extra feature. If you are not going to work with Android you can pass this chapter by.

Testing environment

I have Windows 7 Home Premium on my computer installed and I have tablet PC Samsung Galaxy Note 10.1 2014 Edition with Android 4.4.2. Now I’m going to run my tests on this device.

  1. Setting up an Android device. On Android device switch on Settings, navigate to Developers Options setting and enable USB debugging option.
  2. connect the device to your machine with a USB cable.
  3.  Setting up the Windows. For the execution of the tests on Android, there is a tool called Selendroid (http://selendroid.io/). Selendroid is a child project of the Selenium and Selendroid web driver is a specific implementation of IWebDriver interface for using on Android OS. Interaction with Android device is provided through a Selendroid server which is executable Java file. Therefore, for being able to run a Selendroid server you have to have Java being installed on the machine.

Install Java SDK (minimum 1.6 version) from http://www.oracle.com/

clip_image002

Set JAVA_HOME system variable (how to).

Install Android SDK Tools. After the installation is complete new folder Android is added to the disk. By default, the SDK has been installed on my machine by the following path: C:\Program Files (x86)\Android

clip_image004

Then ANDROID_HOME system variable must be created

clip_image006

Add “android-sdk\tools” and “android-sdk\platform-tools” folders to system variable Path.

Downloadselendroidstandalone.jarfile and put it to the framework working directory (Tests/bin/Debug).

Finally, download and install the latest driver for your device. As I have Samsung tablet PC I navigated to Samsung’s site and got it from there.

clip_image008

Step 4: Running Selendroid server. Invoke command prompt, navigate to the framework working directory and execute the command: java -jar selendroidstandalone-0.15.0-with-dependencies.jar. Selendroid-standalone will start http server on port 4444. After it type http://localhost:4444/wd/hub/status in a browser address bar. You must see the following:

clip_image010

It means that the server has successfully started.

Programming code

1. Add new class to the Test project. I put it in BaseTest.cs file.

/// <summary>

/// represents an instance of remote web driver for interaction with web application on Android OS

/// </summary>

public class TouchCapableRemoteWebDriver : RemoteWebDriver, IHasTouchScreen

{

public TouchCapableRemoteWebDriver(Uri remoteAddress, ICapabilities desiredCapabilities) : base(remoteAddress, desiredCapabilities)

{

TouchScreen = new RemoteTouchScreen(this);

}

public ITouchScreen TouchScreen { get; private set; }

}

2. Add StartProcess method to BasefTest class. This method will be used for starting Selendroid server.

/// <summary>

/// Starts new application process

/// </summary>

/// <param name=”applicationName”>path to the application to start</param>

/// <param name=”arguments”>application parameters</param>

private void StartProcess(string applicationName, params string[] arguments)

{

var p = new Process();

var info = new ProcessStartInfo(applicationName) {RedirectStandardInput = true, UseShellExecute = false};

p.StartInfo = info;

p.Start();

using (StreamWriter sw = p.StandardInput)

{

foreach (string arg in arguments)

if (sw.BaseStream.CanWrite)

sw.WriteLine(arg);

}

}

3. Invoke StartProcess() in Setup() method

[SetUp][TestInitialize]

public void Setup()

{

if (ConfigurationManager.AppSettings[“browserType”] == “Android”)

StartProcess(“cmd.exe”, “java -jar selendroid-standalone-0.15.0-with-dependencies.jar”);

driver = SetUpDriverInstance(ConfigurationManager.AppSettings[“browserType”]);

}

4. Add DesiredCapabilities() method to BaseTest class

/// <summary>

/// initializes Android default desired options

/// </summary>

/// <returns>DesiredCapabilities</returns>

private static DesiredCapabilities DesiredCapabilities()

{

DesiredCapabilities caps = OpenQA.Selenium.Remote.DesiredCapabilities.Android();

return caps;

}

5. Modify SetupDriverInstance() for usage of Selendroid driver

IWebDriver SetUpDriverInstance(string browserType)

{

switch (browserType)

{

case “Firefox”:

//new instance of browser profile

var profile = new FirefoxProfile();

//retrieving settings from config file

var firefoxSettings = ConfigurationManager.GetSection(“FirefoxSettings”) as NameValueCollection;

//if there are any settings

if (firefoxSettings != null)

//loop through all of them

for (var i = 0; i < firefoxSettings.Count; i++)

//and verify all of them

switch (firefoxSettings[i])

{

//if current settings value is “true”

case “true”:

profile.SetPreference(firefoxSettings.GetKey(i), true);

break;

//if “false”

case “false”:

profile.SetPreference(firefoxSettings.GetKey(i), false);

break;

//otherwise

default:

int temp;

//an attempt to parse current settings value to an integer. Method TryParse returns True if the attempt is successful (the string is integer) or return False (if the string is just a string and cannot be cast to a number)

if (Int32.TryParse(firefoxSettings.Get(i), out temp))

profile.SetPreference(firefoxSettings.GetKey(i), temp);

else

profile.SetPreference(firefoxSettings.GetKey(i), firefoxSettings[i]);

break;

}

return new FirefoxDriver(profile);

case “Internet Explorer”:

var IEoptions = new InternetExplorerOptions();

var ieSettings = ConfigurationManager.GetSection(“IESettings”) as NameValueCollection;

if (ieSettings != null)

{

IEoptions.IgnoreZoomLevel = ieSettings[“IgnoreZoomLevel”].ToString(CultureInfo.InvariantCulture) == “true”;

IEoptions.IntroduceInstabilityByIgnoringProtectedModeSettings = ieSettings[“IntroduceInstabilityByIgnoringProtectedModeSettings”] == “true”;

}

return new InternetExplorerDriver(IEoptions);

case “Chrome”:

var options = new ChromeOptions();

var chromeSettings = ConfigurationManager.GetSection(“ChromeSettings”) as NameValueCollection;

var optionsList = new List<string>();

if (chromeSettings != null)

for (var i = 0; i < chromeSettings.Count; i++)

if (chromeSettings[i] == “true”)

optionsList.Add(chromeSettings.GetKey(i));

options.AddArguments(optionsList);

return new ChromeDriver(options);

case “Android”:

var caps = DesiredCapabilities();

driver = new TouchCapableRemoteWebDriver(new Uri(“http://localhost:4444/wd/hub “), caps);

Thread.Sleep(2000);

return driver;

default:

Report.AddError(browserType + ” web driver instance cannot be initialized. Test will by terminated. Verify configuration parameters.”);

throw new Exception();

}

}

Build the solution, change browser type in App.config file to Android

<add key=”browserType” value=”Android”/>

Run tests with NUnit or MSTest tool.

Setting up browsers                                           Table Of Content                                                         Interfaces

Leave a Reply