21 Apr

Appium with Python

Introduction:

Appium is an open source test automation tool for mobile applications. It allows you to test all the three types of mobile applications: native, hybrid and mobile web. It uses JSON wire protocol internally to interact with Android and iOS native apps using the Selenium WebDriver. 

It also allows you to run the automated tests on

  • Real devices
  • Emulators
  • Simulators

How to Download Appium?

Please click “Download Appium” in the below link

https://appium.io/

Install Appium Python Client:

Please follow below link to install Python2.7 and Eclipse editor for creating Python projects.

http://qaprogrammer.com/python-2/

To install Appium Python Client, please follow below link

https://github.com/appium/python-client

How to setup Appium on Android Platform?

Appium supports Android on OS X, Linux and Windows OS. 

In this blog, we will discuss Android on Windows OS.

Download Android SDK from below link

https://developer.android.com/studio/index.html

To install Android SDK follow below video link

 

Connect Android Phone to Windows OS system via USB cable and enable “Developer options” by navigating to Settings/About Phone and tapping build number seven times.You will get a notification saying that you’re now a developer.

Once you are a developer you need to navigate to “Developer options” and enable the USB debugging option.

On Android phone, install “Application Reader” app from Google play store. Application Reader app is used get the app Package and Activity details, which is used for testing the application.

Now open command prompt, navigate to Android SDK tools path and run the command to open Uiautomatorviewer.

To capture a screen, click the “Device Screenshot button” in the uiautomatorviewer tool. The screenshot of the page in your device is captured. You can move over the snapshot in the left-hand panel to see the UI components identified by the uiautomatorviewer.

Start the Appium application by double clicking the .exe file.

Sample Appium Program:

Here is the sample Appium program using Python to get temperature of location using “Weather” app. We need to install weather app before running this code.

# -*- coding: utf-8 -*-

”’

Author : Gandhi Gonugunta

”’

import unittest, time, os

from appium import webdriver

import time

class Android_WeatherApp_Test(unittest.TestCase):

    

    def setUp(self):

        

        desired_caps = {}

        desired_caps[‘platformName’] = ‘Android’

        desired_caps[‘platformVersion’] = ‘6.0’

        desired_caps[‘deviceName’] = ‘MyPhone’

        

        desired_caps[‘appPackage’] = ‘com.macropinch.swan’

        desired_caps[‘appActivity’] = ‘com.macropinch.swan.WeatherActivity2’

        

        self.driver = webdriver.Remote(‘http://localhost:4723/wd/hub’, desired_caps)

    def tearDown(self):

        “Tear down the test”

        self.driver.quit()

    def test_wifi(self):

        self.driver.implicitly_wait(30)

        time.sleep(5)

        print “In Weather App..”

        #city = raw_input(“Enter the city name to be checked: “)

        ## Checking the Weather in Hyderabad Location

        city = “Hyderabad”

        city = city.title()

       getcity = self.driver.find_element_by_xpath(“//android.widget.TextView[@index=’0′]”)

        

        if city not in getcity.get_attribute(‘text’):

            mcity = self.driver.find_element_by_xpath(“//android.widget.TextView[@index=’0′]”)

            mcity.click()

            tempcity = self.driver.find_element_by_xpath(“//android.widget.TextView[@resource-id=’android:id/text1′]”)

            tempcity.click()

            time.sleep(2)

        print “Checking temperature in Hyderabad Area..”

        gettemp = self.driver.find_element_by_xpath(“//android.widget.TextView[@index=’1′]”)

        print “Temp is :”,gettemp.get_attribute(‘text’)

#—START OF SCRIPT

if __name__ == ‘__main__’:

    suite = unittest.TestLoader().loadTestsFromTestCase(Android_WeatherApp_Test)

    unittest.TextTestRunner(verbosity=2).run(suite)

Please edit below lines in the code:

desired_caps[‘platformVersion’] = ‘6.0’   -> Your Android phone platform version(Settings/About Phone)

desired_caps[‘deviceName’] = ‘MyPhone’ -> Your Andriod phone Name

Use Application Reader app to get appPackage & appActivity and edit below two lines,

desired_caps[‘appPackage’] = ‘com.macropinch.swan’

desired_caps[‘appActivity’] = ‘com.macropinch.swan.WeatherActivity2’

 

 

 

 

Share this

Leave a reply