±à¼ÍƼö: |
±¾ÎÄÀ´×ÔÓÚ¼òÊé,±¾ÎÄËù½²µÄ×Ô¶¯»¯²âÊÔUI
Automator×îÏÔÖøµÄÌØµã¾ÍÊÇ£¬¿ÉÒÔÓë¶à¸öapp½øÐн»»¥¡£ |
|
ʹÓÃ
Ê×ÏÈÎÒÃÇÔÚAndroid StudioÖÐн¨Ò»¸öÏîÄ¿£¬È¡ÃûΪUIAutomatorTests¡£Í¬Ê±É¾³ý×Ô¶¯Éú³ÉµÄһЩÎļþ£¬×îÖÕĿ¼½á¹¹ÈçÏ£º

Paste_Image.png
½ÓÏÂÀ´ÎÒÃÇ¿´¿´ÈçºÎÒ»²½Ò»²½µÄʹÓÃEspresso£¬Ê×ÏÈÔÚ¸ùĿ¼µÄ build.gradle ÎļþÖÐÌí¼ÓÏÂÃæµÄÒýÈë¡£
ext {
buildToolsVersion = "24.0.1"
supportLibVersion = "24.2.0"
uiautomatorVersion = "2.1.1"
runnerVersion = "0.5"
rulesVersion = "0.5"
}
|
ÔÚappĿ¼ÖеÄbuild.gradle ÎļþÖÐÌí¼ÓÏÂÃæµÄÒýÈ룬¸ù¾ÝÌáʾµã»÷Sync Now¡£
dependencies
{
compile fileTree (dir: 'libs', include: ['*.jar'])
// App dependencies
compile 'com.android.support:appcompat-v7:' +
rootProject .supportLibVersion;
compile 'com.android .support:support-annotations
:' + rootProject .supportLibVersion;
androidTestCompile 'com .android.support :support-annotations
:' + rootProject .supportLibVersion;
androidTestCompile 'com .android .support .test
: runner :' + rootProject.runnerVersion ;
// UiA utomator Testing
androidTestCompile 'com .android .support .test
.uiautomator :uiautomator-v18:' + rootProject
.uiautomatorVersion ;
androidTestCompile 'org .hamcrest:hamcrest-integration
:1.3'
}
|
½ÓÏÂÀ´ÎÒÃÇÔÚAutomatorTest.classÖбàд²âÊÔ´úÂ룬ΪÁ˲âÊÔ¶à¸öapp£¬ÕâÀïÎÒÃÇÑ¡ÔñÉÏÆªÎÄÕÂÖеÄEspressoTestsºÍÊÖ»úÖеÄÉèÖÃapp¡£

Paste_Image.png
ÔÚEspressoTestsÖÐÎÒÃÇÊäÈëÊý¾Ýµã»÷¼ÆË㣬Ȼºóͨ¹ýÉèÖÃappΪÊÖ»ú¸ü»»Ò»¸öÒ»¸öÁåÉù¡£ÏêϸµÄ²âÊÔ´úÂëÈçÏ£º
package me.shihao
.uiautomatortests;
import android .content.Context;
import android .content.Intent;
import android .support.test.InstrumentationRegistry;
import android .support.test.filters.SdkSuppress;
import android .support.test.runner.AndroidJUnit4;
import android .support.test.uiautomator.By;
import android .support.test.uiautomator.UiDevice;
import android .support.test.uiautomator.UiObject;
import android .support.test.uiautomator.UiScrollable;
import android .support.test.uiautomator.UiSelector;
import android .support.test.uiautomator.Until;
import android .support.v7.widget.RecyclerView;
import android .widget.Button;
import android .widget.EditText;
import android .widget.ImageButton;
import android .widget.ListView;
import android .widget.ScrollView;
import android .widget.TextView;
import org.junit.Test;
import org.junit .runner.RunWith;
import static org .hamcrest.Matchers.notNullValue;
import static org .junit.Assert.*;
@RunWith (AndroidJUnit4.class)
@SdkSuppress (minSdkVersion = 18)
public class AutomatorTest {
private static final String PACKAGE_ESPRESSOTESTS
= "me .shihao .espressotests ";
private static final String PACKAGE_ SETTING =
"com.android.settings";
@Test
public void testEspressoTestsApp () throws Exception
{
// ³õʼ»¯Ò»¸öUiDevice¶ÔÏó
UiDevice mDevice = UiDevice.getInstance (InstrumentationRegistry
.getInstrumentation ());
// µã»÷home¼ü£¬»Øµ½home½çÃæ
mDevice.pressHome();
String launcherPackage = mDevice .getLauncherPackageName
();
assertThat (launcherPackage, notNullValue());
mDevice .wait (Until.hasObject(By.pkg(launcherPackage).depth(0)),
3);
// Æô¶¯espressotests App
Context context = InstrumentationRegistry .getContext();
Intent intent = context .getPackageManager ()
.getLaunchIntentForPackage (PACKAGE_ESPRESSOTESTS)
;
// Çå³ýÒÔǰµÄʵÀý
intent.addFlags (Intent.FLAG_ACTIVITY_CLEAR_TASK);
context .startActivity (intent);
// µÈ´ýÓ¦ÓóÌÐòÆô¶¯
mDevice.wait (Until.hasObject(By.pkg(PACKAGE_ESPRESSOTESTS).depth(0)),
3);
//ͨ¹ýidÕÒµ½ÊäÈë¿òÒ»
UiObject edt1 = mDevice.findObject (new UiSelector
() .resourceId ("me.shihao .espressotests
:id/editText")
.className (EditText.class));
//ÍùÀïÃæÊäÈë×Ö·û2
edt1.setText("2");
//ͨ¹ýidÕÒµ½ÊäÈë¿ò¶þ
UiObject edt2 = mDevice .findObject(new UiSelector
() .resourceId ("me.shihao .espressotests
:id/editText2")
.className ( EditText.class));
//ÍùÀïÃæÊäÈë5
edt2.setText("5");
//ͨ¹ýÎı¾"¼ÆËã"ÕÒµ½°´Å¥
UiObject btn = mDevice.findObject(new UiSelector().text("¼ÆËã")
.className ( Button .class ) );
//Ö´Ðеã»÷ʼþ£¬¼ÆËã½á¹û
btn.click ();
//ͨ¹ýidÕÒµ½ÏÔʾ½á¹ûµÄ textview
UiObject tvResult = mDevice.findObject (new UiSelector()
.resourceId ( "me.shihao .espressotests:id/textView")
. className(TextView.class) );
//ÅжϽá¹ûÓëÔ¤ÆÚÊÇ·ñÆ¥Åä
assertEquals (tvResult.getText(), "¼ÆËã½á¹û£º7");
//ͨ¹ýÎı¾"RecycleView"ÕÒµ½°´Å¥
UiObject btnRecycleView = mDevice .findObject
(new UiSelector() .text ("RecycleView")
.className(Button.class));
//Ö´Ðеã»÷ʼþÌø×ªµ½ÁíÒ»¸ö½çÃæ
btnRecycleView.click ();
//ͨ¹ýidÕÒµ½recycleview
UiScrollable recycleview = new UiScrollable(new
UiSelector()
. className (RecyclerView.class)
. resourceId ("me.shihao.espressotests:id/recycleview"));
//»¬¶¯µ½µ×²¿
recycleview.flingForward();
//»¬¶¯µ½¶¥²¿
recycleview.flingBackward();
UiObject item5 = recycleview.getChild (new UiSelector().text("Item
5"));
//µã»÷Item 5£¬È»ºó»áµ¯³öÒ»¸ö¶Ô»°¿ò
item5.click();
//ͨ¹ýÎı¾"È·¶¨"ÕÒµ½¶Ô»°¿òÖеÄÈ·¶¨°´Å¥
UiObject btnConfirm = mDevice.findObject (new
UiSelector().text("È·¶¨") . className
(Button.class));
//µã»÷È·¶¨¹Ø±Õ¶Ô»°¿ò
btnConfirm.click();
//ÁíÍâÒ»ÖÖ·½Ê½ÕÒµ½Item 2
UiObject item = mDevice.findObject(new UiSelector()
. className (RecyclerView.class)
.resourceId ("me.shihao.espressotests:id/recycleview")
.childSelector (new UiSelector().text("Item
2") ) );
//µã»÷µ¯³ö¶Ô»°¿ò
item.click();
//µã»÷·µ»Ø¹Ø±Õ¶Ô»°¿ò
mDevice.pressBack();
} @Test
public void testSettingApp() throws Exception
{
//³õʼ»¯Ò»¸öUiDevice¶ÔÏó
Context context = InstrumentationRegistry.getContext();
UiDevice mDevice = UiDevice.getInstance (InstrumentationRegistry
.getInstrumentation ());
//»Øµ½home½çÃæ
mDevice.pressHome();
// Æô¶¯ÉèÖÃ
Intent intent = context.getPackageManager ().
getLaunchIntentForPackage ( PACKAGE_SETTING );
// Çå³ýÒÔǰµÄʵÀý
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK);
context. startActivity (intent);
//ͨ¹ýidÕÒµ½scrollview
UiScrollable scrollview = new UiScrollable (new
UiSelector(). className (ScrollView.class) .resourceId("com"
+
".android .settings : id/dashboard")
);
//»¬¶¯µ½µ×²¿
scrollview.flingForward();
//ͨ¹ýÎı¾ÕÒµ½¹ØÓÚÊÖ»ú
UiObject aboutPhone = scrollview.getChild (new
UiSelector().text("¹ØÓÚÊÖ»ú"));
//µã»÷Ìø×ªµ½ÊÖ»úÐÅÏ¢½çÃæ
aboutPhone.click();
//ͨ¹ýdescriptionÕÒµ½ÏòÉÏ·µ»ØµÄ ImageButton
UiObject ibtnBack = mDevice.findObject(new UiSelector().className
(ImageButton.class) .description("ÏòÉϵ¼º½"));
//µã»÷·µ»Ø
ibtnBack.click();
//»¬¶¯µ½°üº¬"ÌáʾÒôºÍ֪ͨ"µÄµØ·½
scrollview.scrollTextIntoView("ÌáʾÒôºÍ֪ͨ");
//ͨ¹ýÏÔʾµÄÎı¾ÕÒµ½¿Ø¼þ
UiObject notify = scrollview.getChild (new UiSelector().text("ÌáʾÒôºÍ֪ͨ"));
//µã»÷Ìø×ªµ½ÏÂÒ»¸ö½çÃæ
notify.click();
//ͨ¹ýÏÔʾµÄÎı¾"ÊÖ»úÁåÉù"ÕÒµ½¿Ø¼þ
UiObject sound = mDevice.findObject (new UiSelector().text("ÊÖ»úÁåÉù"));
//µã»÷Ìø×ªµ½ÁåÉù¶Ô»°¿ò
sound.click();
//ͨ¹ýidÕÒµ½ÁåÉùÁбí
UiScrollable listview = new UiScrollable(new UiSelector().className
(ListView .class) .resourceId
(" android:id /select_dialog_listview")
);
//»î¶¯µ½°üº¬"Beat Plucker"´¦
listview.scrollTextIntoView ( "Beat Plucker");
//ͨ¹ýÏÔʾµÄÎı¾ÕÒµ½¸ÃÏî
UiObject beat = listview.getChild(new UiSelector().text("Beat
Plucker") );
//Ö´Ðеã»÷Ñ¡ÖÐÁåÉù
beat.click();
//ͨ¹ýÎı¾"È·¶¨"ÕÒµ½¶Ô»°¿òÖеÄÈ·¶¨°´Å¥
UiObject btnConfirm = mDevice.findObject(new UiSelector().text("È·¶¨")
. className (Button.class));
//µã»÷È·¶¨¹Ø±Õ¶Ô»°¿ò
btnConfirm.click();
//ͨ¹ýidÕÒµ½ÏÔʾ½á¹ûµÄTextView
UiObject tvSound = mDevice.findObject (new UiSelector()
.resourceId ("android : id/ summary"
).className(TextView
. class) );
//±È½ÏÓëÔ¤ÆÚ½á¹ûÊÇ·ñÒ»ÖÂ
assertEquals(tvSound.getText(), "Beat Plucker");
//µã»÷home¼ü
mDevice.pressHome();
//µã»÷×î½üÓ¦Óüü
mDevice.pressRecentApps();
//ͨ¹ýÀàÃûÕÒµ½ÏÔʾ×î½üappµÄ¿Ø¼þ TaskStackView
UiScrollable taskStackView = new UiScrollable
(new UiSelector(). className (" com.android
.systemui.recents.views" +
".TaskStackView") );
//»¬¶¯µ½°üº¬"EspressoTests"´¦
taskStackView.scrollTextIntoView ("EspressoTests");
//ͨ¹ýÏÔʾµÄÎı¾ÕÒµ½item
UiObject espressoTestsApp = taskStackView.getChild(new
UiSelector() .text (" EspressoTests")
);
//µã»÷Çл»µ½Ç°ÃæµÄespressoTestsApp
espressoTestsApp .click();
}
}
|
ÔËÐÐЧ¹ûÈçÏ£º

ÔËÐÐЧ¹û.gif

ÔËÐÐЧ¹û.gif
UI Automator ViewerʹÓÃ
´ÓÉÏÃæµÄ²âÊÔ´úÂë¿ÉÒÔ¿´µ½£¬ÎÒÃÇÐèÒªÊ×ÏÈÖªµÀÄ¿±ê¿Ø¼þµÄһЩÊôÐÔÖµ£¬È»ºóÔÙÎ§ÈÆÎÒÃǵÄÄ¿±êÊôÐÔ¹¹½¨Ò»¸öÆ¥Å乿Ôò¡£¶øÊµ¼ÊÖÐÎÒÃDz¢²»ÖªµÀappµÄʵÏÖ£¬¿Ø¼þµÄÊôÐÔ²¢²»ÊÇÄÇôÃ÷ÏÔ£¬»òÕß²¢Ã»ÓÐÄÇôÈÝÒ×»ñÈ¡µ½£¬Õâʱ£¬ÎÒÃÇ¿ÉÒÔʹÓÃAndroidÌṩµÄuiautomatorviewer¹¤¾ß°ïÖúÎÒÃǽøÐзÖÎö¡£
½ÓÏÂÀ´ÎÒÃǾͽ²Ò»ÏÂÈçºÎʹÓã¬Android StudioÖеã»÷Tools
>> Android >> Android Device Monitor

Paste_Image.png
ÏÂÃæÏÔʾµÄ¾ÍÊǽçÃæ£¬×î×ó±ß»áÏÔʾÁ¬½ÓµÄÉ豸
Paste_Image.png
µã»÷»á½ØÍ¼·ÖÎöÉ豸µ±Ç°ÏÔʾ½çÃæ²¼¾Ö

Paste_Image.png
È»ºóÓұ߻áÏÔʾ²¼¾Ö½á¹¹ÒÔ¼°viewÏêϸµÄÐÅÏ¢¡£
Paste_Image.png
|