CTG Join our team Why CTG Back
Why CTG

We collaborate with clients to enhance digital agility, solving today's problems while preparing for future shifts.

Learn more

About Us

Delve into the values, mission, and vision that drive our company.

Partners

Explore our tech and industry partners, who amplify the value we provide.

CTG, a Cegeka Company

Learn more about our parent company, Cegeka, a global IT provider.

Corporate Responsibility

Read more about our commitment to being a good corporate citizen.

Culture

Discover the workplace culture we've built that makes CTG a great place to work.

Careers

Check out our employee benefits and start your career journey with CTG.

Learn More

Our Locations

Leadership Team

Company News

Events

Solutions Back

Applications

Empowering businesses with modern, scalable, efficient apps.

Cloud

Supporting all phases of your cloud journey.

Business Solutions

Offering Cegeka's MS Dynamics 365 solutions for pharma and life sciences.

Data

Enabling data-driven decision making and increased efficiency.

Service Desk

Providing 24x7 support with an exceptional end-user experience.

Talent

Rapidly delivering IT and business talent to accelerate digital initiatives.

Automation and AI

Leveraging tech to increase efficiency, lower costs, and empower employees.

Infrastructure

Laying the groundwork for a successful digital organization.

Advisory Services

Combining industry and tech expertise to align technology with business needs.

Testing

Ensuring the quality and reliability of enterprise software.

Cybersecurity

Enhancing the cyber resilience of your organization.

Industries Back
Industries

Our digital solutions are customized for the unique challenges, requirements, and regulations each industry we serve faces.

Explore industries

Healthcare

Addressing IT and operations challenges to enable organizations to better serve patients and members.

Pharma and Life Sciences

Offering Cegeka’s Microsoft Dynamics 365 and Power Platform solutions to navigate industry and regulatory challenges.

Energy

Empowering energy organizations to thrive with data-driven insight and process innovation.

Government

Modernizing operations to improve citizen engagement and service delivery for all levels of government.

Manufacturing

Enabling seamless, efficient operations and increased speed to market with enterprise apps.

Finance and Insurance

Meeting the industry's need for enhanced efficiency, automation, compliance, and customer experiences.

Insights Back
Insights

We're thrilled to share our insights and expertise with you. Learn about industry trends, how to navigate common challenges, client success stories, and more.

Case Studies

Learn how we collaborate with clients to ensure their success.

Videos

Explore videos about CTG’s solutions, client testimonials, and more.

Blogs

Read blogs about tech, innovation, business, employees, and more.

News

View company news and insights from our experts in top publications.

Resources

View e-books, white papers, guides, infographics, and more.

Webinars

Learn from our experts, based on real-world experience across industries.

Contact us
CTG Why CTG
Why CTG

We collaborate with clients to enhance digital agility, solving today's problems while preparing for future shifts.

Learn more

About Us

Delve into the values, mission, and vision that drive our company.

Partners

Explore our tech and industry partners, who amplify the value we provide.

CTG, a Cegeka Company

Learn more about our parent company, Cegeka, a global IT provider.

Corporate Responsibility

Read more about our commitment to being a good corporate citizen.

Culture

Discover the workplace culture we've built that makes CTG a great place to work.

Careers

Check out our employee benefits and start your career journey with CTG.

Learn More

Our Locations

Leadership Team

Company News

Events

Solutions

Applications

Empowering businesses with modern, scalable, efficient apps.

Cloud

Supporting all phases of your cloud journey.

Business Solutions

Offering Cegeka's MS Dynamics 365 solutions for pharma and life sciences.

Data

Enabling data-driven decision making and increased efficiency.

Service Desk

Providing 24x7 support with an exceptional end-user experience.

Talent

Rapidly delivering IT and business talent to accelerate digital initiatives.

Automation and AI

Leveraging tech to increase efficiency, lower costs, and empower employees.

Infrastructure

Laying the groundwork for a successful digital organization.

Advisory Services

Combining industry and tech expertise to align technology with business needs.

Testing

Ensuring the quality and reliability of enterprise software.

Cybersecurity

Enhancing the cyber resilience of your organization.

Industries
Industries

Our digital solutions are customized for the unique challenges, requirements, and regulations each industry we serve faces.

Explore industries

Healthcare

Addressing IT and operations challenges to enable organizations to better serve patients and members.

Pharma and Life Sciences

Offering Cegeka’s Microsoft Dynamics 365 and Power Platform solutions to navigate industry and regulatory challenges.

Energy

Empowering energy organizations to thrive with data-driven insight and process innovation.

Government

Modernizing operations to improve citizen engagement and service delivery for all levels of government.

Manufacturing

Enabling seamless, efficient operations and increased speed to market with enterprise apps.

Finance and Insurance

Meeting the industry's need for enhanced efficiency, automation, compliance, and customer experiences.

Insights
Insights

We're thrilled to share our insights and expertise with you. Learn about industry trends, how to navigate common challenges, client success stories, and more.

Case Studies

Learn how we collaborate with clients to ensure their success.

Videos

Explore videos about CTG’s solutions, client testimonials, and more.

Blogs

Read blogs about tech, innovation, business, employees, and more.

News

View company news and insights from our experts in top publications.

Resources

View e-books, white papers, guides, infographics, and more.

Webinars

Learn from our experts, based on real-world experience across industries.

Join our team Contact us
Home Blogs Blogs Android UI Automation Using Python Wrapper for UI Automator
Digital Transformation
Automation and AI
2 minutes reading

Android UI Automation Using Python Wrapper for UI Automator

Rick Cruz

Rick Cruz

March 13, 2018

What is UI Automator?

During Android application testing, I explored cross-app functional UI testing and came across a tool, labeled ‘UI Automator.’

The tool allows the user to test user interactions across applications and helps a tester to verify how the app behaves with respect to the corresponding user inputs through Menus, Keyboards, Dialogs etc. and navigates into a system or other applications.

UI Automator, a Java library, is available as a part of ‘Android SDK’ from API level 18 (Android OS 4.3 or above).

UI Automator Viewer

UI Automator Viewer is a UI tool that helps you to analyze the UI components available on the screen. Using the tool, a user can inspect the layout hierarchy and the properties of the individual UI components available on the screen.

Python Wrapper of Android

UI Automator requires a fundamental knowledge of Java to write UI automation tests which I was not familiar with. So, I shifted my focus towards Python wrapper written for UI Automator testing framework and I used the same to test UI interfaces in Android, since I was comfortable with Python.
Down below, I’m going to demonstrate how the UI Automator can be set up and how to run the sample test snippet in Python. Here you go.

Requirements:

  • Python ‘ in this demo I have used Python 2.7.10
  • Android SDK (API level above 18)
  • Pre-installed UI Automator library (Python wrapper)
  • pip install uiautomator (command to install uiautomator)
  • Android Device/Emulator (OS 4.3 above) connected to system using ADB
  • IDE for Python, currently using pycharm

Now, let me guide you through how to launch the ‘Google Clock’ application and set an alarm using the UI Automator & UI Automator Viewer, which is used to amass information of UI components available on the target device screen.

Follow the steps below to access the ‘Clock’ application from the device using UI Automator Viewer.
  1. Launch the Uiautomatorviewer
  2. Generate the screen dump
  3. Access the targeted application and note down the UI details required for automation
     

Similarly, use the Uiautomatorviewer tool and get the UI components of the ‘Clock’ application and generate the script for accessing ‘Clock’ to set an alarm.

importing the device object as 'me' from uiautomator library
from uiautomator import device as me

Using Selectors to identify specific UI object in current window, here textMatches,descriptionMatches and resourceIdMatches are selectors. Any one of the Selectors (e.g.: text, resource-id, description, etc) is sufficient to automate the Android applications.
me(textMatches="Clock").click()
me(descriptionMatches="Add alarm").click()
me(resourceIdMatches="android:id/hours").click()
me(descriptionMatches="6").click()
me(resourceIdMatches="android:id/minutes").click()
me(descriptionMatches="0").click()
me(textMatches="OK").click()

After setting the alarm pressing back to reach home screen
me.press.back()

If the testcase is executed successfully, you can mark it as ‘Passed’ and if it fails, with the help of try and except blocks in Python we can mark it as ‘Failed.’ Additionally, you can also add a report function to save the test case execution results in a CSV to verify the output.

The above code snippet should be saved as a ‘.py file’ by adding required functions to execute. A saved Python file can be executed as ‘Python ‘

In the above script, I have just showed you how to access a single application, but you can practically access more than one application using UI Automator with ease.

Benefits

  • Multiple applications can be tested using UI Automator.
  • In Python wrapper, a jar file to run tests isn’t mandatory; whereas in Java we need to push a jar file to the device to run the tests.

I hope this post helped you learn how to launch and execute UI automation for Android using UI Automation Viewer.

Rick Cruz

Rick Cruz

As Managing Director, Applications and Data, Rick has executive responsibility for the ongoing development of CTG’s Application and Data Solutions offerings and teams to deliver innovative, global services that help clients strategically address their business challenges. Rick is an accomplished IT leader and TOGAF Certified Enterprise Architect with 30+ years of IT experience, specializing in enterprise digital transformation strategy and execution, platform/solution architecture, information management, app development, quality assurance, and data/systems integration.

More of Rick Cruz articles