Running Tests on Mobile Devices with selenium and Appium

It's no secret that mobile testing is crucial today as customers have more dependencies in their mobile phones, and Appium is one of the most popular tools for mobile automation. Appium is an open-source test automation framework for native, hybrid, and mobile web apps. It drives iOS, Android, and Windows apps using the WebDriver protocol.


When working with Selenium and Appium tools, the primary tool is the 'Driver'; It manages to find UI elements in web applications and interactions with them. For example, we can find search fields, enter a specific search query and validate the search results.


So there are several things that we need to install before we can run tests on our mobile devices. I'm going to use a physical Android device, and I'm also going to show you how you can run the test on a virtual device to run your tests without having a physical device next to you.


The environment preparation for using Appium will usually contain three phases:


Phase 1: Navigate to "https://appium.io" and Download Appium for windows

Phase 2: Download and deploy Java SE Development Kit

Download the java kit for your operating system; I'm using Windows 64 bit. So this is my Java development kit. We need it because Android is written in java, and this is the thing our Android tools will need to work with our android devices.

Phase 3: Download and deploy "Android Studio"

The only thing we will need from this tool is the "Android SDK," which will be deployed as part of this SDK.



Now that the environment is ready, things will start becoming more complicated and include a few more settings that we will have to do before we can start creating and running tests on your android device.


Thes settings we will have to add:


On your window explorer -> Right Click on "My Computer" -> Properties -> Advanced System settings.

On the "System Properties" dialog -> Choose the "Advanced" TAB and then select "Environment Variables."

We will have to create two new variables called "ANDROID_HOME" and "JAVA_HOME." To do it, click "New..."


Specifications:

Variable Name: ANDROID_HOME

Variable Value: C:\Users\.....\AppData\Local\Android\Sdk


Variable Name: JAVA_HOME

Variable Value: C:\Program Files\Java\jdk-16.0.2

Now the last thing that we will need to do is to edit the system path variable with three new paths:

  1. C:\Users\home-pc3\AppData\Local\Android\Sdk\platform-tools

  2. C:\Users\home-pc3\AppData\Local\Android\Sdk\tools

  3. C:\Program Files\Java\jdk-16.0.2\bin



Working with Physical Android Device

The first example that I will show you is how to work with a physical android device. To be able to do it, we will need to set the following configurations on the physical device:

  1. Set "Developer" mode settings to 'ON.'

  2. Under "Developer Options," set "USB debugging" to 'ON.'

  3. Under "Developer Options," set "Stay awake" to 'ON' to keep the phone turned on when connected.

  4. Connect your phone to a computer with a USB connection.

Now its time to ensure that the phone is correctly connected to your computer:

  1. Open Command Prompt.

  2. Send input: adb devices.

  3. validate that your mobile device is attached

One last thing that we need to do is to install Appium in our project:

  1. Open your VS project.

  2. In the solution Explorer - > Manage NuGet Packages...

  3. Install "Appium WebDriver".

Now that we installed the Appium package, it's time to write code. The first thing that we will have to do is to create and initialize a new Android driver:


Code

using Microsoft.VisualStudio.TestTools.UnitTesting;
using System;
using SeleniumExtras;
using OpenQA.Selenium;
using OpenQA.Selenium.Remote;
using OpenQA.Selenium.Chrome;
using OpenQA.Selenium.Support;
using OpenQA.Selenium.Appium;
using OpenQA.Selenium.Appium.Android;
using OpenQA.Selenium.Appium.Enums;

namespace AppiumTutorial
{
[TestClass]
public class UnitTest1
{
//Creating a new AppiumDriver
public static AppiumDriver<IWebElement> driver { get; set; }

[ClassInitialize]
public static void ClassInitialize(TestContext context)
{
//Set the driver capabilities in order to be able running our tests 
DesiredCapabilities capabilities = new DesiredCapabilities();
capabilities.SetCapability(MobileCapabilityType.DeviceName, "device");
capabilities.SetCapability(MobileCapabilityType.BrowserName, "chrome");
            
//Initialize a new Android Driver with the Appium server settings 
driver = new AndroidDriver<IWebElement>(new Uri("http://127.0.0.1:4723/wd/hub"), capabilities);            
}
 [TestMethod]
public void TestMethod1()
{
//N\A
}

[ClassCleanup]
public void cleanup()
{ 
//N\A
}
}
}

Note: The localhost and port of the Appium server are located in the server configuration menu, take it and press "Start Server."



Troubleshooting

There are two known errors that you may encounter when executing tests:


Error 1: Permission denial

The reason for this error is that there are vendors who add security limitations, which leads to "Permission denial: writing to secure settings requires android. Permission.WRITE_SECURE_SETTINGS"


What will usually works is to enable the following options in your device:

Error 2: Neither ANDROID_HOME nor ANDROID_SDK_ROOT environment variable was exported

The solution to this problem is straightforward. Open the Appium Server In main UI, Edit Configurations, set requested variables, and restart.




158 views0 comments