How to Automate Mobile Apps Using SeeTest Automation

A step-by-step guide to automating Mobile Apps using SeeTest Automation Tool:

While continuing our journey of Mobile Automation with SeeTest Automation, so far in our first SeeTest Automation tutorial, we learned how to setup SeeTest Automation for first use and connect iOS and Android devices with it.

Now we need to see how real mobile apps can be automated using SeeTest Automation. We will also see more about some important commands used in SeeTest Automation.

This tutorial will brief you on how to automate the actions performed by mobile apps using SeeTest Automation Record & Playback feature and scripting techniques along with the commands that help us to perform simple actions.

How To Automate Mobile Apps Using SeeTest Automation

Let’s automate some real time apps now!

Working with Applications:

Installing & Launching Applications

There are various methods to install a mobile application on a mobile device. In an Android device, you can install from Google PlayStore or copy the .apk file and run or install it via ADB.

For iOS devices, applications can be installed from the AppStore or by using iTunes from the PC if you have the .ipa file with you. In all these ways, the application will treat you as a user and not as a developer. Therefore, you will not have many privileges to work with the internal details of the applications.

SeeTest Automation provides an application manager that can install applications in Instrumented mode. Instrumenting a mobile app can remove the DRM restrictions set for the mobile app. After instrumenting the app, you can dig deep into the mobile app to view the UI map and element’s XPath more precisely.

Instrumentation is highly recommended and you should always instrument the app under test.

In order to add an application to the SeeTest package manager, click on ‘Import/Sign application’ button in the application window (you can hover over each button to see its tooltip information).

For Android devices, we can import applications in two ways:

  • Import application by selecting the .apk file.
  • Import application from the device by extracting .apk if the application is already installed on the device.

The second way is a better feature, hence, we can import the application to SeeTest even if we do not have the source .apk file with us. On selecting this option, it will navigate us to another window that contains a list of the installed applications.

We can choose the desired application from the list to import to SeeTest Automation. This feature is not supported for iOS applications. In iOS, .ipa files cannot be extracted from the installed application.

Figure 1 – Installed apps

installed apps

SeeTest Automation will install the application in instrumented mode if we install the application from SeeTest package manager in the Application window of the SeeTest. Clicking on Install Application button will install the application as instrumented on the device.

Figure 2 – Install Application

Install App

Figure 3 – Installing Application

Installing App

After successful installation, SeeTest will show a popup asking whether to launch the application on the device or not. You can choose ‘Yes’ if you want to launch it or choose ‘No’ if you wish to launch it later. You can always launch the application using the ‘Launch Application’ button in the Application Window.

Figure 4 – Launch Application

Launch App

If you launch the application, a popup will be displayed indicating launching mode (Instrumented/ Non-Instrumented) and the progress of launch. An application will be launched in the mobile device and the same will be reflected in the device reflection as well.

For Android devices, only launching the app via SeeTest Automation will enable instrumented mode and manual launching will not launch the app in instrumented mode. If we launch the app manually, SeeTest will not correctly identify the elements during Object Spy.

Figure 5 Launching Application

Launching App

Figure 6 Launch App – Device Reflection

Launch App Device Reflection

Object Spy

Object Spy is an important feature of SeeTest Automation that helps to identify the elements by their XPath representation. It is similar to the Chrome’s Inspect Element feature which is a part of Chrome Developer Tools or Firefox’s FireBug.

Mobile App UI map is having a corresponding xml configuration containing all the element definitions and their linking. So XPath can be used to traverse among these elements.

You can launch Object Spy by clicking on ‘Object Spy’ button in the device reflection. Once you are in the Object Spy mode, you will get access to each element in the UI. Selecting an element will show all its properties and their values on the screen from which we can extract the unique properties to identify that particular element from the other elements.

Figure 7 Object Spy button

Object Spy Button

Figure 8 Object Spy


Object Spy Elements

Figure 7 shows the Object Spy screen of the EriBank application. Username field in the UI is highlighted with green color in the device reflection and the same element is selected in the UI Map window shown on the left. It also displays the properties and values of the Username field.

Using these properties, we can easily form Xpath to identify the Username field as we find Xpath of the web elements using FireBug.

The colors used to highlight the elements in the device reflection indicate the below:

  • Red: Instrumented application & Native Element
  • Yellow: Non-Instrumented application & Native Element
  • Blue: Web Element
  • Green: Selected element

By using these color codes, we can distinguish the type of application and element.

The ‘Filter Query’ text box is used to filter the elements by providing Xpath. When we give a valid XPath in that area, all elements that are having the same XPath will be selected and the count will be updated in a small text box up to the ‘Filter Query’ text box.

Clicking on the Right and Left arrow will navigate to the next and previous elements.

Figure 9 XPath Filter

Xpath Filter

The above figure indicates, that the EriBank Application has two elements with the XPath mentioned in the ‘Filter Query’ text box. If we need to identify a particular element, we need to choose a unique property to form XPath. Therefore, probably we can use the ‘id’ property (if available) to form a unique XPath.

SeeTest Automation can form XPath automatically using the properties of an element. To get XPath, you need to right-click on the element in the Object Spy Properties window and Select ‘Copy Unique Xpath’. Then paste it into the Filter Query to verify if the formed XPath uniquely identifies the desired element.

You can also form XPath by right-clicking on the element property with which you need to form the XPath and select ‘Add to Filter’. Then the corresponding XPath representation will be automatically added to the Filter Query text box.

Figure 10 Unique Xpath

Unique Xpath

Figure 11 Add to Filter

Xpath Filter

Figure 12 Copy Unique Xpath – Verify Element

Unique Xpath - Verified Xpath

Record & Playback Feature

As mentioned earlier, SeeTest Automation supports Record & Playback feature apart from the scripting feature.

Some best practices for record & playback feature are as follows:

  • Keeps recording short. Recording the whole test in one go is not recommended.
  • Click the center of the objects that you need to identify.
  • Wait for the screen to refresh and make sure the step is added to the command window.

In order to start recording, connect the mobile device and open the device reflection. Then navigate to the ‘Test’ tab in the command window and press the red Record button.

Figure 13 Record

Record

Then a popup window will open up to choose the recording options. If you do not want to change any option, click on the Start button and the recording will start. If you need to perform dynamic recording, then check the Dynamic Recording check box.

You can also choose Recording Type from the same window.

#1) Dynamic Recording

Dynamic Recording is only possible with the applications that are available in the SeeTest package manager and displayed in the Application window of SeeTest Automation. By default, EriBank application will be added to the SeeTest and it will be available in the Application Window.

If you have this application installed on the device, then you will be able to perform Dynamic Recording.

In Dynamic Recording, elements (both NATIVE and WEB) are identified via their Xpaths and not via the Image or Text. This is useful so that we do not have to spy each object separately while writing the test scripts.

#2) Recording Type

A recording type has three sub-categories.

They are as follows:

  • Native Recording

This is for recording a test in Native applications. In Native Recording, you can either choose the Instrumented Application if you are testing an instrumented application or Non-Instrumented Application if you are testing a Non-Instrumented application.

  • Web Recording

This is used if the Web application is used to test. You can select Chrome or the Native browser according to the browser which you use to test the web application.

  • Image and Text Recording

If this is selected, the elements are identified using Images and Texts and not by XPath. This is used when Native/Web recognition does not work with the elements.

After the recording has started, perform some events on the mobile device and SeeTest will record the same and the corresponding commands will be displayed in the command window after you stop recording.

Figure 14 Non-Dynamic Recording

Dynamic Recording

In the above figure, all elements in the application are identified as Images and Texts (Objects) and not by XPath. This is because of the absence of Dynamic Recording. All these objects are saved in the Object Repository as well for future use.

In order to identify the elements by XPath during recording, enable Dynamic Recording and choose the application after selecting the radio button ‘New Application’. This will launch the application (re-launches if already launched).

Figure 15 Dynamic Recording Options

Dynamic Recording Options

Figure 16 Dynamic Recording – Recorded Steps

Recording Steps

In Figure 35, all the elements with which we interacted during recording are identified by using their Native/Web property instead of Image/Text. On clicking on the Play button, the recorded steps will start to execute one by one and you can see the actions that are being performed on the device accordingly.

Writing Test Scripts using Client Libraries

SeeTest Automation supports writing test scripts using the client libraries provided by them.

Currently, SeeTest supports and provides client libraries for Java, C#, Perl, Python, Ruby, Visual Basic, JavaScript. The client libraries are available in the ‘clients’ folder of SeeTest Automation installed directory (C:\Program Files (x86)\Experitest\SeeTest\clients).

Here, we are trying with Java.

First Test Script – Java

Follow the below steps to setup test script to test the EriBank application in Java using SeeTest Java API:

#1) Launch Eclipse.

#2) Create a Java Project and name it as EriBank Test (you can choose whatever name you want).

Select ‘Use project folder as a root for sources and class files’. This will avoid creating ‘src’ and ‘bin’ directories in the project folder.

Then click on Finish.

Figure 17 Create Java Project

Create Java Project

#3) After creating Java Project, right click on the Project and select New->Package.

#4) Give package name as ‘com.experitest.eribank’ (again, you can give any package name that you want). Then click on Finish.

Figure 18 Create Package

Create Package

#5) After adding a package, right click on a package and select New->Class.

#6) Give class name as EriBankTest (any name is fine) and uncheck ‘public static void main(String[] args). Then click on Finish.

Figure 19 Create Java Class

Create Java Class

#7) Add the client libraries to the build path. For that, right click on the Project then select Build Path -> Configure Build Path.

Figure 20 Configure build path

Configure Buildpath

#8) Select ‘Libraries’ tab.

#9) Click on ‘Add External Jars’ and navigate to the directory where java client libraries/Jars are stored. (By default, C:\Program Files (x86)\Experitest\SeeTest\clients\java).

Figure 21 Adding external jars

Add external jar

Figure 22 Added external jars

Add external jars

#10) Select all Jars and click on OK.

#11) Click Ok.

Now we will have a Java project with all the required libraries as shown in the below figure.

Figure 23 Final Java Project Setup

Java Final Project

#12) Open SeeTest Automation and if the previously recorded steps are not deleted, select Java (JUnit) from the code window. If test steps are deleted, then record the steps again.

Figure 24 Java JUnit code

Java JUnit code

#13) Click on the Export button displayed next to the drop-down to copy the code.

#14) Paste the code into our program and make necessary modifications on the package name and class name. (modifying the test name is also recommended i.e. testUntitled to testEriBank)

#15) The final code will look like this:

package com.experitest.eribank;
import com.experitest.client.*;
import org.junit.*;
/**
*
*/
public class EriBankTest {
private String host = "localhost";
private int port = 8889;
private String projectBaseDirectory = "C:\\Users\\MUNEER\\workspace\\project2";
protected Client client = null;
@Before
public void setUp(){
client = new Client(host, port, true);
client.setProjectBaseDirectory(projectBaseDirectory);
client.setReporter("xml", "reports", "Untitled");
}
@Test
public void testEriBank(){
client.setDevice("adb:YU5010A");
client.launch("com.experitest.ExperiBank/.LoginActivity", true, true);
client.elementSendText("NATIVE", "hint=Password", 0, "admin");
client.elementSendText("NATIVE", "hint=Password", 0, "admin");
client.click("NATIVE", "text=Login", 0, 1);
client.click("NATIVE", "text=Close", 0, 1);
}
@After
public void tearDown(){
// Generates a report of the test case.
// For more information - https://docs.experitest.com/display/public/SA/Report+Of+Executed+Test
client.generateReport(false);
// Releases the client so that other clients can approach the agent in the near future.
client.releaseClient();
}
}

#16) Run the program and you can see the test steps that are executing and performing actions on the device and device reflection.

#17) If a test is passed, JUnit will show green color for the test, otherwise red.

Code Explained

Now we will look into the code on a snippet by snippet basis.

importcom.experitest.client.*;
importorg.junit.*;

These import statements import SeeTest Client and JUnit. SeeTest Client is the main class that provides various methods to perform actions such as click, send text etc. on the mobile device.

private String host = "localhost";
privateintport = 8889;
private String projectBaseDirectory = "C:\\Users\\MUNEER\\workspace\\project2";
protected Client client = null;

These initialization statements are necessary to define the SeeTest Client object. If we are executing the same test in another device that is connected along with the current device, then we need to change the port to ‘8890’ and projectBaseDirectory is the location of the SeeTest project folder.

@Before
publicvoidsetUp(){
client = newClient(host, port, true);
client.setProjectBaseDirectory(projectBaseDirectory);
client.setReporter("xml", "reports", "Untitled");
}

The code in @Before executes before the test (or tests if we have multiple tests). In that, we are defining a Client object using the previously initialized variables (host and port) and a boolean variable useSessionId (set it to true). Then set the project directory and report type (xml), report name (Untitled) and reports directory accordingly.

@Test
publicvoidtestEriBank(){
client.setDevice("adb:YU5010A");
client.launch("com.experitest.ExperiBank/.LoginActivity", true, true);
client.elementSendText("NATIVE", "hint=Username", 0, "admin");
client.elementSendText("NATIVE", "hint=Password", 0, "admin");
client.click("NATIVE", "text=Login", 0, 1);
client.click("NATIVE", "text=Close", 0, 1);
}

The code in @Testexecutes during a test.

client.setDevice("adb:YU5010A");

Set the device using the device name identified by SeeTest Automation.

client.launch("com.experitest.ExperiBank/.LoginActivity", true, true);

Launch the application whose ActivityName (as shown in the SeeTest Package Manager in Application Window) is mentioned as the first argument. The second argument (Boolean) indicates an instrumented launch. Setting true will launch the application in instrumented mode. The third argument (Boolean) indicates whether to stop or not if the application is already running.

Setting true will close the app if it is already running and re-launch the app.

client.elementSendText("NATIVE", "hint=Username", 0, "admin");
client.elementSendText("NATIVE", "hint=Password", 0, "admin");

This is used to send a text to a text box.

The first argument (String) indicates the Zone of the element (Native if the element is Native, WEB if the element is a web element). The second argument (String) is the unique property of the element  (XPath of the element is also acceptable). The third argument (int) indicates the index of the element. Last argument (String) defines the text to be sent.

client.click("NATIVE", "text=Login", 0, 1);
client.click("NATIVE", "text=Close", 0, 1);

To click on an element whose Zone (first argument), unique property (second argument) and index (third argument) is defined by the arguments. The last argument (int) indicates the click count. If we need to perform a double click, then make the click count as 2.

@After
publicvoidtearDown(){
// Generates a report of the test case.
// For more information - https://docs.experitest.com/display/public/SA/Report+Of+Executed+Test
client.generateReport(false);
// Releases the client so that other clients can approach the agent in the near future. 
client.releaseClient();
}

The code is executed after a test (or after all the tests if we have multiple tests). In that, we generate a test report and release the client object, that we created during a test. The argument (Boolean) of the generateReport method indicates whether to release the client after generating a report or not.

First Test Script – Maven

Follow the below steps to set up a SeeTest Automation test with Maven:

#1) Create a simple Maven Project (skip archetype selection).

#2) Enter the group Id as com.experitest, Artifact Id as eribank.test (any name is fine) and click Finish.

Figure 25 Creating a simple maven project

Maven project build

Figure 26 Create a Maven Project

#3) Create a package ‘com.seetest under src/test/java' folder in the project.

#4) Create a Java Class ‘EriBankTest’ under com.seetest package.

Figure 27 Maven Project – Folder structure

Maven Project

#5) Open pom.xml.

#6) Add the following properties to use JavaSE 1.7.

<properties>
 <maven.compiler.source>1.7</maven.compiler.source>
 <maven.compiler.target>1.7</maven.compiler.target>
</properties>

If you get any errors or observe if the JRE System Library is missing from the Project, navigate to Window->Preferences->Java->Installed JREs->Execution Environments and click on Java SE-1.7.

Select jre7 from the right window and click OK. Then right-click on the maven project and choose Maven->Update Project.

Figure 28 Execution environment setting

Execution environments

Figure 29 Update Maven Project

Update maven project

#7) Add the following repository:

<repositories>
 <repository>
 <releases>
 <enabled>true</enabled>
 <updatePolicy>always</updatePolicy>
 <checksumPolicy>fail</checksumPolicy>
 </releases>
 <id>Experitest.repo1</id>
 <name>YourName</name>
 <url>http://repo.experitest.com:8010/Maven2/</url>
 <layout>default</layout>
 </repository>
</repositories>

#8) Add the following dependency:

<dependencies>
 <dependency>
 <groupId>com.experitest</groupId>
 <artifactId>SeeTestClient</artifactId>
 <version>10.6</version>
 </dependency>
</dependencies>

#9) Final pom.xml will look like this:

<projectxmlns="http://maven.apache.org/POM/4.0.0"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
 <modelVersion>4.0.0</modelVersion>
 <groupId>com.experitest</groupId>
 <artifactId>eribank-test</artifactId>
 <version>0.0.1-SNAPSHOT</version>
<properties>
 <maven.compiler.source>1.7</maven.compiler.source>
 <maven.compiler.target>1.7</maven.compiler.target>
 </properties>
 <repositories>
 <repository>
 <releases>
 <enabled>true</enabled>
 <updatePolicy>always</updatePolicy>
 <checksumPolicy>fail</checksumPolicy>
 </releases>
 <id>Experitest.repo1</id>
 <name>YourName</name>
 <url>http://repo.experitest.com:8010/Maven2/</url>
 <layout>default</layout>
 </repository>
 </repositories>
 <dependencies>
 <dependency>
 <groupId>com.experitest</groupId>
 <artifactId>SeeTestClient</artifactId>
 <version>10.6</version>
 </dependency>
 </dependencies>
</project>

#10) Copy the previous code to EriBankTest.java and move the lines of code in @Before to the first of @Test method.

Figure 30 Maven Project – modified test

Maven Project

#11) Right click on pom.xml or the Project and choose Run As-> Maven Project.

#12) In the Run Configuration window, provide a goal clean test and click Run.

Then the Maven Project will start to build and the test will execute and the console window will show BUILD SUCCESS if the test is passed.

Figure 31 Maven Project Building

Maven project build

 Figure 32 Console Log

 Console Log

Conclusion

In this tutorial, we learned how to automate actions performed on the mobile apps using SeeTest Automation Record & Playback and scripting techniques.

We also came across few commands that would help us to perform simple actions like click and send a text etc.

In our next tutorial in this series, we will learn more about the advanced commands that are used in SeeTest Automation to perform complex actions on the mobile apps.