<commit_before>---
title: "UiAutomator Basics"
excerpt: "Everything you need to know to start using UiAutomator"
comments: true
related: true
header:
    overlay_image: /assets/images/uiautomator_basics.jpg
    overlay_filter: 0.25
date: 2017-08-19
categories:
  - android
tags:
  - testing
  - android
  - uiautomator
---


{% include toc title="UiAutomator Basics" icon="file-text" %}
## Why UiAutomator?
Time to time my colleagues from other departments ask me about framework I'm using for automating UI tests on Android and AndroidTV devices. And they really wonder when I tell them I'm using **UiAutomator** for those purposes. Not because they think it's not appropriate solution for their tasks, but rather because they haven't even heard about this kind of framework.

From my side it's odd to hear that. No, don't get me wrong, I know about all those fancy frameworks like Appium, Espresso, Robotium etc (and we'll talk about them in my further posts). But let's face the truth - most of the functionality these instruments provide you'll never use, and to support them you'll have to have whole team of automation engineers. And that's expensive, right?

On the other hand there is UiAutomator.
> UiAutomator is a light-weight, easy-to-learn library, developed by Google to make things done fast, when you don't want to spend lot's of time on developing test code and its maintainance.

Still not convinced? What if I tell you this little tool can help you to build fully functional, extendable and reliable test frameworks? Tempting, isn't it? Stay tuned!

**Disclaimer:** We're going to talk about UiAutomator2 since it's the most recent version, which includes all the fixes of the problems UiAutomator(1) had.
{: .notice--info}

## How to get started with UiAutomator?
### Android Studio
First of all, you'll need to configure your Android Studio to be able to use support libraries, which include UiAutomator. This can be done in Android Studio settings under Android SDK panel.

{% include figure image_path="/assets/images/android_support.jpg" alt="how to configure uiautomator" caption="Go to Android Studio Settings, open Android SDK panel and enable Android Support Repository" %}

### Gradle
Next thing we'll need is to set up Gradle. To do that, following changes should be added to `build.gradle` file. 

```groovy
android {
	...
    defaultConfig {
        ...
        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
    }
...
dependencies {
	...
    androidTestCompile 'com.android.support.test.uiautomator:uiautomator-v18:2.1.3'
}
```
Since other settings in the file depend on your app and what it's built for, they are up to you. But these two lines must be included into `build.gradle` in order to use UiAutomator in your project.

That's basically it! It wasn't difficult, right? You're ready to start writing you first test.
<commit_msg>Change image size<commit_after>---
title: "UiAutomator Basics"
excerpt: "Everything you need to know to start using UiAutomator"
comments: true
related: true
header:
    overlay_image: /assets/images/uiautomator_basics.jpg
    overlay_filter: 0.75
date: 2017-08-19
categories:
  - android
tags:
  - testing
  - android
  - uiautomator
---

{% include toc title="UiAutomator Basics" icon="file-text" %}
## Why UiAutomator?
Time to time my colleagues from other departments ask me about framework I'm using for automating UI tests on Android and AndroidTV devices. And they really wonder when I tell them I'm using **UiAutomator** for those purposes. Not because they think it's not appropriate solution for their tasks, but rather because they haven't even heard about this kind of framework.

From my side it's odd to hear that. No, don't get me wrong, I know about all those fancy frameworks like Appium, Espresso, Robotium etc (and we'll talk about them in my further posts). But let's face the truth - most of the functionality these instruments provide you'll never use, and to support them you'll have to have whole team of automation engineers. And that's expensive, right?

On the other hand there is UiAutomator.
> UiAutomator is a light-weight, easy-to-learn library, developed by Google to make things done fast, when you don't want to spend lot's of time on developing test code and its maintainance.

Still not convinced? What if I tell you this little tool can help you to build fully functional, extendable and reliable test frameworks? Tempting, isn't it? Stay tuned!

**Disclaimer:** we're going to talk about UiAutomator2 since it's the most recent version, which includes all the fixes of the problems UiAutomator(1) had.
{: .notice--info}

## How to get started with UiAutomator?
### Android Studio
First of all, you'll need to configure your Android Studio to be able to use support libraries, which include UiAutomator. This can be done in Android Studio settings under Android SDK panel.

{% include figure image_path="/assets/images/android_support.jpg" alt="how to configure uiautomator" caption="Go to Android Studio Settings, open Android SDK panel and enable Android Support Repository" %}

### Gradle
Next thing we'll need is to set up Gradle. To do that, following changes should be added to `build.gradle` file. 

```groovy
android {
	...
    defaultConfig {
        ...
        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
    }
...
dependencies {
	...
    androidTestCompile 'com.android.support.test.uiautomator:uiautomator-v18:2.1.3'
}
```
Since other settings in the file depend on your app and what it's built for, they are up to you. But these two lines must be included into `build.gradle` in order to use UiAutomator in your project.

That's basically it! It wasn't difficult, right? You're ready to start writing you first test.

{'fraction_non_alphanumeric': 0.07178602243313201, 'fraction_numerical': 0.006212251941328731, 'mean_word_length': 4.2690909090909095, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '518989', 'n_tokens_mistral': 1652, 'n_tokens_neox': 1536, 'n_words': 792}