Skip to content

Commit eb169db

Browse files
committed
Adds UI automator example
1 parent 28402e3 commit eb169db

25 files changed

Lines changed: 581 additions & 0 deletions

File tree

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
.gradle
2+
/local.properties
3+
/.idea/workspace.xml
4+
/.idea/libraries
5+
.DS_Store
6+
/build
7+
/captures
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
/build
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
apply plugin: 'com.android.application'
2+
3+
android {
4+
compileSdkVersion 19
5+
buildToolsVersion "23.0.0"
6+
7+
defaultConfig {
8+
applicationId "com.vogella.android.test.uiautomator"
9+
minSdkVersion 19
10+
targetSdkVersion 23
11+
versionCode 1
12+
versionName "1.0"
13+
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
14+
}
15+
buildTypes {
16+
release {
17+
minifyEnabled false
18+
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
19+
}
20+
}
21+
}
22+
23+
dependencies {
24+
androidTestCompile 'com.android.support.test:runner:0.3'
25+
androidTestCompile 'junit:junit:4.12'
26+
testCompile 'junit:junit:4.12'
27+
androidTestCompile 'org.hamcrest:hamcrest-library:1.3'
28+
androidTestCompile 'com.android.support.test:runner:0.3'
29+
androidTestCompile 'com.android.support.test:rules:0.3'
30+
androidTestCompile 'com.android.support.test.uiautomator:uiautomator-v18:2.1.1'
31+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
# Add project specific ProGuard rules here.
2+
# By default, the flags in this file are appended to flags specified
3+
# in /home/vogella/Android/Sdk/tools/proguard/proguard-android.txt
4+
# You can edit the include path and order by changing the proguardFiles
5+
# directive in build.gradle.
6+
#
7+
# For more details, see
8+
# http://developer.android.com/guide/developing/tools/proguard.html
9+
10+
# Add any project specific keep options here:
11+
12+
# If your project uses WebView with JS, uncomment the following
13+
# and specify the fully qualified class name to the JavaScript interface
14+
# class:
15+
#-keepclassmembers class fqcn.of.javascript.interface.for.webview {
16+
# public *;
17+
#}
Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
package com.vogella.android.test.uiautomator;
2+
3+
import android.content.Intent;
4+
import android.content.pm.PackageManager;
5+
import android.content.pm.ResolveInfo;
6+
import android.support.test.InstrumentationRegistry;
7+
import android.support.test.rule.ActivityTestRule;
8+
import android.support.test.runner.AndroidJUnit4;
9+
import android.support.test.uiautomator.By;
10+
import android.sucom.vogella.android.test.uiautomatorpport.test.uiautomator.UiDevice;
11+
import android.support.test.uiautomator.UiObject;
12+
import android.support.test.uiautomator.UiObjectNotFoundException;
13+
import android.support.test.uiautomator.UiScrollable;
14+
import android.support.test.uiautomator.UiSelector;
15+
import android.support.test.uiautomator.Until;
16+
17+
import org.hamcrest.Matchers;
18+
import org.junit.Before;
19+
import org.junit.Rule;
20+
import org.junit.Test;
21+
import org.junit.runner.RunWith;
22+
23+
import static org.hamcrest.Matchers.*;
24+
import static org.junit.Assert.*;
25+
26+
@RunWith(AndroidJUnit4.class)
27+
public class MyUiAutomatorTest {
28+
@Rule
29+
public ActivityTestRule<MainActivity> mActivityRule = new ActivityTestRule<MainActivity>(MainActivity.class);
30+
31+
private UiDevice mDevice;
32+
33+
@Before
34+
public void setUp() {
35+
// Initialize UiDevice instance
36+
37+
mDevice = UiDevice.getInstance(InstrumentationRegistry.getInstrumentation());
38+
// Start from the home screen
39+
mDevice.pressHome();
40+
41+
mDevice.wait(Until.hasObject(By.pkg(getLauncherPackageName()).depth(0)), 1000);
42+
}
43+
44+
@Test
45+
public void checkSettings() throws UiObjectNotFoundException {
46+
47+
// Simulate a short press on the HOME button.
48+
mDevice.pressHome();
49+
50+
// We’re now in the home screen. Next, we want to simulate
51+
// a user bringing up the All Apps screen.
52+
// If you use the uiautomatorviewer tool to capture a snapshot
53+
// of the Home screen, notice that the All Apps button’s
54+
// content-description property has the value “Apps”. We can
55+
// use this property to create a UiSelector to find the button.
56+
UiObject allAppsButton = new UiObject(new UiSelector().description("Apps"));
57+
58+
// Simulate a click to bring up the All Apps screen.
59+
allAppsButton.clickAndWaitForNewWindow();
60+
61+
// In the All Apps screen, the Settings app is located in
62+
// the Apps tab. To simulate the user bringing up the Apps tab,
63+
// we create a UiSelector to find a tab with the text
64+
// label “Apps”.
65+
UiObject appsTab = new UiObject(new UiSelector().text("Apps"));
66+
67+
// Simulate a click to enter the Apps tab.
68+
appsTab.click();
69+
70+
// Next, in the apps tabs, we can simulate a user swiping until
71+
// they come to the Settings app icon. Since the container view
72+
// is scrollable, we can use a UiScrollable object.
73+
UiScrollable appViews = new UiScrollable(
74+
new UiSelector().scrollable(true));
75+
76+
// Set the swiping mode to horizontal (the default is vertical)
77+
appViews.setAsHorizontalList();
78+
79+
// create a UiSelector to find the Settings app and simulate
80+
// a user click to launch the app.
81+
UiObject settingsApp = appViews
82+
.getChildByText(new UiSelector()
83+
.className(android.widget.TextView.class.getName()),
84+
"Settings");
85+
settingsApp.clickAndWaitForNewWindow();
86+
87+
// Validate that the package name is the expected one
88+
UiObject settingsValidation = new UiObject(
89+
new UiSelector()
90+
.packageName("com.android.settings"));
91+
assertThat(settingsValidation.exists(), equalTo(true));
92+
}
93+
94+
private String getLauncherPackageName() {
95+
// Create launcher Intent
96+
final Intent intent = new Intent(Intent.ACTION_MAIN);
97+
intent.addCategory(Intent.CATEGORY_HOME);
98+
99+
// Use PackageManager to get the launcher package name
100+
PackageManager pm = InstrumentationRegistry.getContext().getPackageManager();
101+
ResolveInfo resolveInfo = pm.resolveActivity(intent, PackageManager.MATCH_DEFAULT_ONLY);
102+
return resolveInfo.activityInfo.packageName;
103+
}
104+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
3+
package="com.vogella.android.test.uiautomator" >
4+
5+
<application
6+
android:allowBackup="true"
7+
android:icon="@mipmap/ic_launcher"
8+
android:label="@string/app_name"
9+
android:theme="@android:style/Theme.Holo.Light.DarkActionBar" >
10+
<activity
11+
android:name=".MainActivity"
12+
android:label="@string/app_name" >
13+
<intent-filter>
14+
<action android:name="android.intent.action.MAIN" />
15+
16+
<category android:name="android.intent.category.LAUNCHER" />
17+
</intent-filter>
18+
</activity>
19+
</application>
20+
21+
</manifest>
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
package com.vogella.android.test.uiautomator;
2+
3+
import android.app.Activity;
4+
import android.os.Bundle;
5+
import android.view.Menu;
6+
import android.view.MenuItem;
7+
8+
public class MainActivity extends Activity {
9+
10+
@Override
11+
protected void onCreate(Bundle savedInstanceState) {
12+
super.onCreate(savedInstanceState);
13+
setContentView(R.layout.activity_main);
14+
}
15+
16+
@Override
17+
public boolean onCreateOptionsMenu(Menu menu) {
18+
// Inflate the menu; this adds items to the action bar if it is present.
19+
getMenuInflater().inflate(R.menu.menu_main, menu);
20+
return true;
21+
}
22+
23+
@Override
24+
public boolean onOptionsItemSelected(MenuItem item) {
25+
// Handle action bar item clicks here. The action bar will
26+
// automatically handle clicks on the Home/Up button, so long
27+
// as you specify a parent activity in AndroidManifest.xml.
28+
int id = item.getItemId();
29+
30+
//noinspection SimplifiableIfStatement
31+
if (id == R.id.action_settings) {
32+
return true;
33+
}
34+
35+
return super.onOptionsItemSelected(item);
36+
}
37+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
2+
xmlns:tools="http://schemas.android.com/tools"
3+
android:layout_width="match_parent"
4+
android:layout_height="match_parent"
5+
android:paddingBottom="@dimen/activity_vertical_margin"
6+
android:paddingLeft="@dimen/activity_horizontal_margin"
7+
android:paddingRight="@dimen/activity_horizontal_margin"
8+
android:paddingTop="@dimen/activity_vertical_margin"
9+
tools:context=".MainActivity">
10+
11+
<TextView
12+
android:layout_width="wrap_content"
13+
android:layout_height="wrap_content"
14+
android:text="@string/hello_world" />
15+
16+
</RelativeLayout>
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<menu xmlns:android="http://schemas.android.com/apk/res/android"
2+
xmlns:app="http://schemas.android.com/apk/res-auto"
3+
xmlns:tools="http://schemas.android.com/tools"
4+
tools:context=".MainActivity">
5+
<item
6+
android:id="@+id/action_settings"
7+
android:orderInCategory="100"
8+
android:showAsAction="never"
9+
android:title="@string/action_settings" />
10+
</menu>
3.34 KB
Loading

0 commit comments

Comments
 (0)