Mobile Automation Testing Using Selenium

Selenium is a superstar! Everyone who is into development, app production, software testing knows about the tool very well.

Since the tool is free and open-source enhancements are happening at a rapid pace and the community backs up very strongly, if you are facing an issue there will be a solution online!

But still, that’s not the only reason. Why do you think selenium is the most opted one when it comes to test automation of web apps is it because it’s free? No, not exactly. There are many reasons to name.

But, do you think selenium can be used for testing mobile applications?

Before that, let’s have a look at the latest features of selenium 4 so that you will get an idea of why it’s the most preferred one.

  • The most astounding ability of the selenium web driver is that it can drive any browser natively and that too with insanely good effectiveness.
  • The design of the web driver is very simple. Especially the programming interface
  • You can use the programming language of your choice. All you have to do is choose the right library
  • Selenium supports browsers like Chrome/Chromium, Firefox, Internet Explorer, Edge, Opera, and Safari.
  • Selenium IDE has the ability to record multiple locators with respect to the element it’s interacting with.
  • Features like setting breakpoints and pausing on exceptions make selenium IDE perfect for debugging
  • The IDE selenium is very easy to use. All you have to do is install an extension to the browser
  • Selenium IDE is equipped with a heavy control flow structure owing to the presence of commands like if, while and times, etc
  • With the help of the run command, one test case can be used to execute another test
  • Selenium Grid is one of the astounding features of the tool. This enables the tool to run tests simultaneously on multiple machines
  • Selenium grid can also solve common delegation and distribution problems
  • Selenium grid will come in handy when you have to run an app on multiple browsers and OS versions

Components of Selenium grid

When it comes to mobile app testing automation, flexibility and accuracy are one of the reasons many consider Selenium. However, while it is true that many other superior tools are there for the task, there exists a reason why people consider selenium automation.

Since you have already deployed Selenium for web browser compatibility testing, it will be easy to set it up for mobile app testing too.

But you cannot use selenium directly for the task. You have to integrate Appium and Selendroid to perform mobile app automation testing using selenium

Why Appium?

Appium and Selenium have something in common. Both frameworks use JSONWireProtocol to interact with iOS and Android apps.

Let’s see how. Before we get into Appium integration let’s have a look at selenium web driver installation. 

Driver implementation for all browsers are provided by the vendors themselves except Internet explorer

However, let’s have a look at the standard web driver installation

To make sure that you are using the correct driver you need to download WebDriverManager

WebDriverManager is a library. Not just any library. It’s an open-source JAVA library that can be used to download, do set-up and perform maintenance of drivers that selenium needs in an automated manner

To download a proper diver you need to be sure about the specification of the browser, for instance, chromedriver is the one for chrome browser.

Yes, we know that this is an obvious find for any. However, to avoid complication, read the driver documentation or release notes to find out you are using the latest versions.

Once the appropriate driver has been downloaded to your computer you have to set the path in selenium tests so that the tool can access it whenever there is a requirement.

In JAVA, it is done in two ways. The first one is to set the path in the environment variable and the second way is to export the driver path through JAVA system properties.

System.setProperty("webdriver.chrome.driver", "/path/to/chromedriver");

System.setProperty("webdriver.gecko.driver", "/path/to/geckodriver");

System.setProperty("webdriver.edge.driver", "/path/to/msedgedriver");

System.setProperty("webdriver.opera.driver", "/path/to/operadriver");

System.setProperty("webdriver.ie.driver", "C:/path/to/IEDriverServer.exe");

One thing that needs to be kept in mind is that browsers often update themselves. Make sure that the driver is also updated properly. There are third-party libraries available to assist you in that.

WebDriverManager is mostly used as a JAVA dependency.

The next step in installing the IDE is to import the WebDriverManager

Let’s see how it’s done for various languages

JAVA

import io.github.bonigarcia.wdm.WebDriverManager;

Python

from webdriver_manager.chrome import ChromeDriverManager

Ruby

gem ‘webdrivers’, ‘~> 5.0’

Kotlin

import io.github.bonigarcia.wdm.WebDriverManager;

Let’s see how IDE is set up for the JAVA language 

Call setup ( ) to put the correct browser driver 

WebDriverManager.chromedriver().setup();

Then initialize the driver

ChromeDriver driver = new ChromeDriver();

How to do automation testing for mobile applications using selenium?

It’s always safe to run one test at a time that targets one platform. However, the latest production environment and CI in practice demands parallel testing in their own capability. That’s the space where Appium fits in.

But, to make sure that you are leveraging this setup, you need to make sure that certain specifications have been met and Appium is using an independent driver rather than one that’s in use.

All these complexities can be avoided if you use Selenium along with Appium. Selenium has a special-purpose load balancer called Selenium grid which is basically a proxy server that has the ability to speak web driver protocol, manage a connection to a pool of driver servers, and is the best tool for DIY Appium clusters. The relay feature of the Selenium tool allows proxy Appium requests to an Appium server

What makes Selenium Grid work well with Appium?

  • You can kick start a selenium server in hub mode on a particular host and port of your like
  • You can start the ‘n’ number of servers using node mode. You can even start them anywhere you like unless you have network access to the grid server
  • What distinguishes every node is the “node config” that can clearly describe the kind of automation it is capable of.
  • Whenever your test needs to start a secession all you have to do is make sure that the grid hits the target server. The grid server will then choose which node to use.

The Important aspect of a node config

Capabilities – They are the set of capabilities supported by the particular node. You can specify a particular number of capabilities using maxInstances

Proxy: Proxy logic is used to communicate with the node; you can also write a custom proxy if you wish to.

maxSession: The maximum number of test sessions the node can run is fixed through this module

How to run the test in Appium 

First, you have to register your Appium server with a local selenium grid by using a –nodeconfig parameter

> appium --nodeconfig /path/to/nodeconfig.json

# or, if running from source:

> node . --nodeconfig /path/to/nodeconfig.json

Remember that in the config file you have to define the browserName, version, and platform. Selenium grid can only redirect your test to the right device based on these parameters.

Also, you need to configure Host details and Selenium Grid details.

After the Appium server starts and registers with the grid you will get to see your device on the grid console page.

“http://\<grid-ip-adress>:\<grid-port>/grid/console”

JSON fine example for Grid Node Configuration 

{

 "capabilities":

 [

 {

 "browserName": "<e.g._iPhone5_or_iPad4>",

 "version":"<version_of_iOS_e.g._8>",

 "maxInstances": 1,

 "platform":"<platform_e.g._MAC_or_ANDROID>"

 }

 ],

 "configuration":

 {

 "cleanUpCycle":2000,

 "timeout":30000,

 "proxy": "org.openqa.grid.selenium.proxy.DefaultRemoteProxy",

 "url":"http://<host_name_appium_server_or_ip-address_appium_server>:<appium_port>/wd/hub",

 "host": "<host_name_appium_server_or_ip-address_appium_server>",

 "port": <appium_port>,

 "maxSession": 1,

 "register": true,

 "registerCycle": 5000,

 "hubPort": <grid_port>,

 "hubHost": "<Grid_host_name_or_grid_ip-address>"

 "hubProtocol": "<Protocol_of_Grid_defaults_to_http>"

 }

}

Testing Mobile Web Browsers

Once everything is up and running you should see a msg like this in the grid consodle

13:48:04.514 INFO [DefaultGridRegistry.add] – Registered a node http://0.0.0.0:4733

To run tests it’s better to use maxParallelForks

Eg:

test {

maxParallelForks = 3

forkEvery = 1

}

This will help Gradle to create a new thread for each test. However, for tests to run in parallel, you need separate test classes.

public class Edition054_Selenium_Grid {

protected RemoteWebDriver driver;

static String HUB_URL = "http://localhost:4444/wd/hub";

private static By loginBtn = By.tagName("button");

private static By username = By.id("username");

private static By password = By.id("password");

private static By message = By.id("flash");

private static String LOGIN_URL = "https://the-internet.herokuapp.com/login";

private static String ERR_MSG = "Your username is invalid!";

@After

public void tearDown() {

if (driver != null) {

driver.quit();

}

}

protected void actualTest(RemoteWebDriver driver) {

WebDriverWait wait = new WebDriverWait(driver, 10);

driver.navigate().to(LOGIN_URL);

wait.until(ExpectedConditions.presenceOfElementLocated(username))

.sendKeys("foo");

driver.findElement(password).sendKeys("bar");

driver.findElement(loginBtn).click();

String errText = wait.until(ExpectedConditions.presenceOfElementLocated(message)).getText();

Assert.assertThat(errText, Matchers.containsString(ERR_MSG));

}

}

Selendroid

  • Selendroid is a pristine test automation framework that drives the UI of Android native and hybrid apps.
  • It can be used on emulators and real devices the most astounding feature about Selendroid is that it can be integrated as a node into selenium grid
  • It’s compatible with JSON Wire Protocol
  • No modification is required for an application under test
  • Built-in Android driver webview app
  • Multiple APIs at the disposal
  • Can be extended at runtime with the appropriate extension
  • Emulators will start automatically
  • Gesture support
  • UI elements can be traced out by different locator types

How to use Selendroid for app test automation?

  • Selendroid runs on Mac, Linux, and Windows
  • Selendroid can be used to test Android apps. But in order to run tests, the framework requires permission <uses-permission android:name=”android.**permission.INTERNET”/>
  • Install JAVA SDK and ensure that it is JAVA_HOME configured
  • Install Android SDK and ensure that is ANDROID_HOME configured
  • There is an exception in running Selendroid. If the machine is a 64 bit Linux machine,
install

sudo dpkg --add-architecture i386

sudo apt-get update

sudo apt-get install libc6:i386 libncurses5:i386 libstdc++6:i386
  • To run the test make sure that Android apps (apk file) are on the same machine so that the Selendroid server can be started easily
  • The next step is to selendroid-standalone.jar. To run the selendroid-standalone jar execute the code,

java -jar selendroid-standalone-0.17.0-with-dependencies.jar -app selendroid-test-app-0.17.0.apk

To read more about how to run the tests, please visit http://selendroid.io/setup.html

Selendroid Architecture

Conclusion

Both Selendroid and Appium enable testers to leverage the features of Selenium. Howe ever, Many prefer Appium for its flexibility and agility. Appium also has huge community support. Hope you got an idea about mobile app automation testing using selenium.

But it has to be kept in mind that millions of dollars is at stake in a development project. You simply cannot experiment in that situation. Not just because of the risk, maintaining an automation suite is a costly and time-consuming task, If it’s done by people who have no or even little idea about it.

You can only leverage the power of automation in App testing if it’s done by skilled testers. A mistake would make the automation framework a burden rather than do any good. To avoid any mishaps we can help you in integrating automation to your existing dev process. Our vast experience and expertise enables us to do it efficiently. Let’s have a talk and see if we can work together.

Related Posts