Appium: iOS UI Software Test Automation

Play Voice
July 22, 2024
Technology

Automation is now very popular for software testing and developing cycles. This is really important for software quality assurance, and the popularity of this approach speaks for itsely as most of manual test engineers eventually become automation engineers. Of course tags matter, but the real world also requires automated testing and reports of executed test cases, so sanity testing can be faster and more accurate. We can evaluate results at anytime, regardless of the version of build we have.

Most of companies give software engineers general tasks like requirement analysis, proof of concept (POC), documentation (or how to avoid it), module development, unit testing, and sending the build to the QA team. Do all these tasks truly show what software engineers do? Definitely yes, but what if we can also create a sanity testing of a developed module automatically by using an another program, basically software test automation.

Automation–A Critical Advantage

  • Resource reduction
  • Faster, more accurate results
  • Repeatable
  • Programable
  • Decrease in a part cycle time
  • Improves quality and reliability
  • Better maintenance of Test Suit

With so many benefits, why not consider it? Engineers who develop mobile apps can make automation scripts to maintain their sanity when doing UI automation testing. Let’s take a look at automation testing with Appium and python code.

Appium is an open source test automation tool, developed and supported by Sauce Labs to automate native and hybrid mobile apps. It uses JSON wire protocol internally to interact with iOS and Android apps through the Selenium WebDriver.

Python is a high level programming language, widely used for general purposes, such as for automation because many automation frameworks are available in this language.

Necessary tools/framework required to execute scripts with Appium:

  • Prepare with latest OS X and xCode 5.1.1 (Appium now supports the latest version)
  • Install Node here
  • Install python from here
  • Install pip sudo easy_install pip
  • Install nosetests pip install nose
  • Download the sublime text editor here
  • Download Appium UI client on your mac here

After completing these steps, open Appium from finder. It will look like this:

appiom ios_1

Get the build(.app) of the iPhone-Simulator from finder and make a .zip of your application. Choose path from Appium IOS settings. Settings on Appium should like this:

appium_settings
appium_settings
appium_settings_2
appium_settings_3

To apply automation we need recorded scripts. Appium can record your existing features into scripts very well. Appium supports recorded features and converts them into multiple scripting languages like:

  • Python (personal favorite)
  • C#
  • Ruby
  • Objective-c
  • Java
  • Node.js

Start Recording

  • Click on the Launch button from the Appium UI tool.
  • After seeing (twice): info: <— Get wd/hub/status/  200 xxx ms
  • Click on the Search button(Appium Doctor)
  • Click on the Record button

It will open the record functionality of Appium like this:

appium_record

After pressing the Send Keys button, it will directly effect the running application on the IOS Simulator and the script will generate something like this:

wd.find_element_by_xpath(“//UIAApplication[1]/UIAWindow[1]/UIASecureTextField[1]”).send_keys(“password”)

Based on your requirements, you can then record more scripts and keep them for automation.

Focusing on Scripting

As you can see here, the web driver is an instance of the Appium web driver. Actually, it deals with IOS via JSON wire protocol. Selenium is well known and widely used in automation testing. Selenium uses Appium, and it was made for IOS-Android UI automation testing. Here’s how Appium scripts work:

appium_scripts

         

appium_script_2

Examples of clean scripts in Python and executed in Appium:

Here are examples of clean scripts in Python that are executed in Appium. For this example we will use Login, Favorite and Logout. Start by taking Appium’s web driver instance into singletone.

1. Create a constant.py file like this:

  • Import OS
  • From datetime import datetime

#--Generic Variables for use through Appium connection
K_APP_FILE_NAME = ‘/MyAppl.zip'
K_APPIUM_VER = '1.0'
K_APP_PLATFORM_NAME = 'iOS'
K_APP_PLATFORM_VER = '7.1'
K_DEVICE_NAME = 'iPhone Retina (4-inch 64-bit)'

#--Webdriver property
K_WD_EXPLICITY_WAIT = 20
K_WD_REMOTE_URL = 'http://127.0.0.1:4723/wd/hub'

#--Result Files
K_RST_FILE = '/Result/test.txt'
K_RST_FILE_PATH = os.path.dirname(os.path.realpath(__file__)) + K_RST_FILE
K_RST_HTML_FILE = '/result.html'
K_RST_HTML_FILE_PATH = os.path.dirname(os.path.realpath(__file__)) + K_RST_HTML_FILE
K_RST_PDF_FILE = 'Report'

#--Execption/Fails
K_FILE_NOT_FOUND = 'File not found for given path %s'

class utility_functions():

#--Common utils for Apending string in test.txt file.
def write_comment_in_file(self,text):
f = open(K_RST_FILE_PATH, "a")
f.write('Time: %s ' % datetime.now())
f.write(text)
f.close()
return True

2. Create a file labeled sz_appium_webdriver.py like the script:

  • Import os
  • Import constant as const
  • Import unittest, time
  • from appium import webdriver
  • from time import sleep

#--SingleTone Class
class Singleton(object):
_instance = None
_desired_caps = None

#-- Initialized new instance of Appium Webdriver if not found instance while calling.
def __new__(cls, *args, **kwargs):
if not cls._instance:
cls._instance = super(Singleton, cls).__new__(cls, *args, **kwargs)
cls._desired_caps = {}
cls._desired_caps['appium-version'] = const.K_APPIUM_VER
cls._desired_caps['platformName'] = const.K_APP_PLATFORM_NAME
cls._desired_caps['platformVersion'] = const.K_APP_PLATFORM_VER
cls._desired_caps['deviceName'] = const.K_DEVICE_NAME
cls._desired_caps['app'] = os.path.dirname(os.path.realpath(__file__)) + const.K_APP_FILE_NAME

cls._instance = webdriver.Remote(const.K_WD_REMOTE_URL, cls._desired_caps)
cls._instance.implicitly_wait(const.K_WD_EXPLICITY_WAIT)

return cls._instance

3. Create a_sz_init_login.py for login scripts like this:

  • import os
  • import unittest, time
  • from time import sleep
  • import constant as const
  • from sz_appium_driver import Singleton

class ios_sz(unittest.TestCase):
def test_login(self):
try:
#--Variable of initialized appium webdriver
wd = Singleton()
success_login = False

wd.find_element_by_name("icon back").click()
wd.find_element_by_xpath("//UIAApplication[1]/UIAWindow[1]/UIAButton[2]").click()
wd.find_element_by_xpath("//UIAApplication[1]/UIAWindow[1]/UIATextField[1]").send_keys("sitanshu@zymrinc.com")
wd.find_element_by_xpath("//UIAApplication[1]/UIAWindow[1]/UIASecureTextField[1]").send_keys("password")

wd.find_element_by_xpath("//UIAApplication[1]/UIAWindow[1]/UIAButton[2]").click()

def is_alert_present(wd):
try:
wd.switch_to_alert().text
return True
except:
return False

#---INIT POINT OF AUTOMATION SCRIPT
if __name__ == '__main__':
suite = unittest.TestLoader().loadTestsFromTestCase(ios_sz)
unittest.TextTestRunner(verbosity=2).run(suite)

4. Record your features to save scripts.

You can create several scripts by recording your features and keeping them in separate python files. After that, you should create a config file that will execute the new scripts. Create a file named config.ini [nosetests]

verbosity = 2
#Used to display print statements on console
nocapture = 1
#test only some modules with comma seperated values
tests = a_test_sz_init_login.py, c_test_sz_favorite.py, d_test_sz_logout.py, etc…

5. Launch appium.

6. Open terminal and execute nosetests -c config.ini.

7. See the magic begin: the simulator will open and automatically execute.

Appium_IOS

You can enhance these scripts with test case reports with HTMLTestRunner here.After these implementations, you can see your test case results like this:

appium_results

Here we are starting Appium from UI Tools. We can also start Appium from the terminal. For that we need to install:

  • brew install node # get node.js
  • npm install -g appium # get appium
  • npm install wd # get appium client
  • appium & # start appium

Installing Appium via npm takes a few mins. If you directly call Appium (start appium server, same function as launch) it says -bash: appium command not found. So the four steps for the installation of nap and Appium are necessary.

You can also call these scripts and make the Appium server create a shell script.

Create a shell script:

start_appium_launch_appium.sh
#! /bin/sh
appium &
sleep 10
cd ~
cd Documents/SZ_Automation/SZ_Small_Big
python rep_sz.py > result.html

You can call it something like: sh start_appium_launch_appium.sh

Now you can use Appium to generate reports with automated testing! Happy automation to all you iOS developers!

Everything you need to know about outsourcing technology development
Access a special Introduction Package with everything you want to know about outsourcing your technology development. How should you evaluate a partner? What components of your solution that are suitable to be handed off to a partner? These answers and more below.

Conclusion

Have a specific concern bothering you?

Try our complimentary 2-week POV engagement
I have read and accept the Privacy Policy
Our Latest Blogs
How to Enhance Your DevOps Toolchain with Integrated CI/CD
Read More >
How is AI in DevOps Transforming Software Development
Read More >
Top DevOps Tools You Need to Streamline Your Workflow in 2024
Read More >

About The Author

Harsh Raval

Speak to our Experts
Lets Talk

Our Latest Blogs

November 15, 2024

How to Enhance Your DevOps Toolchain with Integrated CI/CD

Read More →
October 28, 2024

How is AI in DevOps Transforming Software Development

Read More →
October 23, 2024

Top DevOps Tools You Need to Streamline Your Workflow in 2024

Read More →