Appium Blog Series – Part 3 – Using Appium automation script to Check SQLite DB Content of a Device!

Appium is an Open Source mobile application user-interface (UI) testing framework, and is used to automate native, hybrid and web mobile apps. It enables cross-platform mobile application testing by using common API for both Android and iOS platform test scripts, thereby enhancing code-reusability.

Appium provides flexibility of writing and running automation tests in any language such as, Java, C#, PHP, Ruby, Python, Perl, Objective-C.

In the following example, we will demonstrate how to connect to Android SQLite database via Appium script using core Java, JDBC, and run SQL Queries to perform data validation checks.

About SQLite database:

SQLite is the default database engine on Android, which is an open source, lightweight transactional database that stores app data to a text file with a file extension .db, on the device. To access this .db file via Appium scripts, we first need to pull the .db file to our local machine. The steps to achieve this are listed below:

  • Download eclipse plugin, com.questiod.sqlitemanager.jar and save in your eclipsedropins folder and restart eclipse.
  • Download latest version of sqlite-jdbc-x.x.x.jar, and add it to java build path in eclipse, by right clicking on project > Properties > Java Build Path > Libraries > Add External JARs >
  • I have a sample sqlite database on my device with name book.db, and it has 1 table Title with 4 records as shown below. Connect Android Device to laptop/computer In USB Debugging mode.
  • In eclipse, click DDMS icon shown below, and select book.db file as shown in below file location on device, click import icon highlighted below to pull this file from device and save it to a folder in pc.

The following code snippet opens a jdbc connection to sqlite database book.db, and run’s select sql query to fetch data from Title table and print results to eclipse console.

Connection con = null;
 Class.forName("org.sqlite.JDBC");
 //create a jdbc connection to book.db located in below file path
 con = DriverManager.getConnection("jdbc:sqlite:E:\test\book.db");
 con.setAutoCommit(false);

Statement sqlstmt = null;
//using above connection, create a statement object
sqlstmt = con.createStatement();
//run select sql query on Title table, and store results in Resultset.
ResultSet res = sqlstmt.executeQuery(“select * from Title;”);
//iterate for all data in Title table and print it.
while (res.next()) {
int BookId = res.getInt(“BookId”);
String BookName = res.getString(“BookName”);
String BookAuthor = res.getString(“BookAuthor”);
System.out.println(“BookId = ” + BookId);
System.out.println(“BookName = ” + BookName);
System.out.println(“BookAuthor = ” + BookAuthor);
System.out.println();
}
res.close();
sqlstmt.close();
con.close();

Read Other Appium related blogs

How to Automate Testing for Android Native Apps Using Appium

Don’t miss my next blog on – Running Appium Automation Script on Android Emulators

Automated Device Certification Testing On Cloud AWS Device Farm with Appium

Appium – A Good Open Source Mobile App Testing Framework

Learn more about Trigent’s automation testing services.

Appium Blog Series – Part 2 – Running Appium Automation Script on Android Emulators

About Appium

Appium is an Open Source mobile application UI testing framework, used to automate native, hybrid and web mobile apps. It enables cross-platform mobile app testing by using common API for both Android and iOS platform test scripts, thereby enhancing code-reusability. Appium provides flexibility of writing and running automation tests in any language such as, Java, C#, PHP, Ruby, Python, Perl, Objective-C.

About Android Emulator

Without an Android device, we can still run automation tests of Android Native App’s using an Emulator which mimics the OS and hardware of an Android device on a computer.

In Android SDK folder, there is an interface called as AVD Manager, which creates a virtual mobile device (Emulator) to run on a computer. Running Emulator starts a virtual console in which we can install Android apps.

In the following example, I will demonstrate automation of Android mobile phone’s default installed calculator app on Android Emulator.

Prerequisites for running Appium automation test for a native Android app on Emulator:

  • Java/JDK is installed and path for JAVA_HOME is set on the windows system.
  • Android SDK is installed and required packages of Android API’s are downloaded in SDK Manager.
  • Intel HAXM is installed.
  • Appium is installed.

Start Android Emulator:

In SDK folder on computer, run AVD Manager.exe, AVD Manager Dialog as shown below:

Click Create button, enter below details to create a new AVD.

AVD Manager shows newly created AVD as below, select this AVD and click Start.

In Launch Options dialog click Launch and in 10 to 15 minutes, Android Emulator will be started as below.

In eclipse create a Java project, with package name Android and class name AndroidEmulatorCalculator.

Set Desired Capabilities:

Following code snippet in AndroidEmulatorCalculator class, sets Desired Capabilities for Calculator App.

// Created object of DesiredCapabilities class.
 DesiredCapabilities capabilities = new DesiredCapabilities();

//Set android deviceName desired capability to Android Emulator.
capabilities.setCapability(“deviceName”, “Android Emulator”);

//Set BROWSER_NAME desired capability to Android.
capabilities.setCapability(“browserName”, “Android”);

//Set android VERSION from device desired capability.
capabilities.setCapability(“platformVersion”, “6.0”);

// Set android platformName desired capability to Android.
capabilities.setCapability(“platformName”, “Android”);

// Set android appPackage desired capability.
capabilities.setCapability(“appPackage”, “com.android.calculator2”);

// Set android appActivity desired capability.
capabilities.setCapability(“appActivity”, “com.android.calculator2.Calculator”);

Android Driver:

Following code snippet Creates an object of AndroidDriver class with Desired Capabilities as mentioned above and sets Appium server address with port number, and launches Android calculator app on device.

AndroidDriver driver;
 driver = new AndroidDriver(new URL("http://127.0.0.1:4723/wd/hub"), capabilities);

The code snippet given below shows a simple addition on calculator app by using id tag to locate calculator app elements.

driver.findElement(By.id("com.android.calculator2:id/digit_4")).click();
 driver.findElement(By.id("com.android.calculator2:id/op_add")).click();
 driver.findElement(By.id("com.android.calculator2:id/digit_8")).click();
 driver.findElement(By.id("com.android.calculator2:id/eq")).click();
 String result = driver.findElement(By.className("android.widget.EditText")).getText();
 System.out.println("Result of Addition is : " + result);

Run Automation Test

Start Appium server, and run AndroidEmulatorCalculator class as TestNG test. Automation test will open calculator app on Emulator, clicks on buttons in this sequence -> 4, +, 8 and =.

Gets result from text area of calculator app, and print it in eclipse console.

Learn more why trigent is one of the best automation testing companies.

Read Other Appium related blogs

How to Automate Testing for Android Native Apps Using Appium

Using Appium automation script to Check SQLite DB Content of a Device!

Automated Device Certification Testing On Cloud AWS Device Farm with Appium

Appium – A Good Open Source Mobile App Testing Framework

Appium Blog Series – Part 1 – How to Automate Testing for Android Native Apps Using Appium

Appium is an open source mobile application user interface (UI) testing framework, and is used to automate native, hybrid and web mobile apps. It enables cross-platform mobile app testing by using common API for both Android and iOS platform test scripts, thereby enhancing code-re-usability. Appium provides flexibility of writing and running automation tests in languages such as, Java, C#, PHP, Ruby, Python, Perl, Objective-C.

Appium Architecture In Android:

Appium is a webserver that exposes a REST API. Appium client establishes connection with Appium Server through JSON Wire Protocol by creating a JSON object called as `Desired Capabilities’. Appium Server then creates an automation session by running a session id for the client, based on Key-Value pairs of Desired Capabilities set in Appium client. Appium Server then connects with the UI-Automator provided by Android SDK. UI-Automator communicates with Bootstrap.jar which will run user commands from the test script, on the mobile app in device.

Prerequisites for running Appium automation test for a native Android app:

  • Java/JDK is installed and path for JAVA_HOME is set on the windows system.
  • Android SDK is installed and required packages of Android API’s are downloaded in SDK Manager. Path for ANDROID_HOME, platform-tools is set on the windows system.
  • Eclipse IDE is installed. TestNG is installed.
  • Eclipse ADT Plugin is installed and configured to integrate android development environment in eclipse IDE.
  • Microsoft .Net Framework and Node JS is Installed.
  • Appium is installed.
  • Latest Selenium Webdriver jar files for java client, Appium Client library jar file, Google-gson jar file are downloaded.
  • Android Device is connected to your laptop/computer In USB Debugging mode.

In eclipse create a Java project, with package name Android and class name AndroidCalculator as shown below.

Add selenium webdriver’s jar files, Appium Client library jar file, Google-gson jar file in to java build path, by right clicking on project > Properties > Java Build Path > Libraries > Add External JARs >

Set Desired Capabilities:

We need to provide the following set of Desired Capabilities to provide Appium Server with details of automation session we want to set up:

  • Android Device Name – In command prompt, run command adb devices to show Android Device Name.
  • Android OS Version – In android device, Open settings -> About phone -> Android version
  • Android Software App Package Name, App Activity Name – In command prompt, run command adb logcat, and open Calculator App in Android phone and press ctrl+c. Log show’s App Package Name = com.android.calculator2, App Activity Name = com.android.calculator2.Calculator.

In class AndroidCalculator, following code snippet, creates a object of DesiredCapabilities class.

DesiredCapabilities capabilities = new DesiredCapabilities();

//Set android deviceName desired capability.
capabilities.setCapability(“deviceName”, “Q88HQ4S4SC89S8NB”);

//Set BROWSER_NAME desired capability to Android.
capabilities.setCapability(CapabilityType.BROWSER_NAME, “Android”);

//Set android VERSION from device desired capability.
capabilities.setCapability(CapabilityType.VERSION, “5.0”);

// Set android platformName desired capability to Android.
capabilities.setCapability(“platformName”, “Android”);

// Set android appPackage desired capability.
capabilities.setCapability(“appPackage”, “com.android.calculator2”);

// Set android appActivity desired capability.
capabilities.setCapability(“appActivity”, “com.android.calculator2.Calculator”);

Android Driver:

Following code snippet creates an object of AndroidDriver class with Desired Capabilities as mentioned above and sets Appium server address with port number, and launches android calculator app on device.

AndroidDriver driver;
 driver = new AndroidDriver(new URL("http://127.0.0.1:4723/wd/hub"), capabilities);

Locate elements in Android App:

To locate elements in Android App, we use UI Automator Viewer tool provided by Android SDK.

  1. Run uiautomatorviewer.bat file from SDKtools folder.
  2. In Android device, open calculator app.
  3. In UI Automator Viewer tool, click on Device Screenshot image button.
  4. UI Automator Viewer now shows calculator app’s screenshot on left pane, and right pane show’s calculator app’s UI element’s hierarchy view with node structure that explain’s how elements are arranged. Ex: In calculator snapshot, button 4 is selected, and right pane shows different properties of button 4, which can be used in test script for locating button 4.

Code The snippet below shows  a simple addition on calculator app by using id tag to locate calculator app elements.

driver.findElement(By.id("com.android.calculator2:id/digit_4")).click();
 driver.findElement(By.id("com.android.calculator2:id/op_add")).click();
 driver.findElement(By.id("com.android.calculator2:id/digit_8")).click();
 driver.findElement(By.id("com.android.calculator2:id/eq")).click();
 String result = driver.findElement(By.className("android.widget.EditText")).getText();
 System.out.println("Result of Addition is : " + result);

Run Automation Test:

Start Appium server, and make configuration changes as shown in the below snapshots and click on Run button.

…to be continued.

Read Other Appium related blogs

Don’t miss my next blog on – Running Appium Automation Script on Android Emulators

Using Appium automation script to Check SQLite DB Content of a Device!

Automated Device Certification Testing On Cloud AWS Device Farm with Appium

Appium – A Good Open Source Mobile App Testing Framework

Exit mobile version