SlideShare a Scribd company logo
Atsuto Yamada


UiDevice uiDevice = UiDevice.getInstance(InstrumentationRegistry.getInstrumentation());

UiObject2 button = uiDevice.findObject(By.res("info.akkuma.example:id/submit"));

button.click();
@Before

public void startApp() {

mDevice = 
       UiDevice.getInstance(InstrumentationRegistry.getInstrumentation());

mDevice.pressHome();



String launcherPackage = getLauncherPackageName();

mDevice.wait(Until.hasObject(By.pkg(launcherPackage).depth(0)), TIMEOUT);



Context context = InstrumentationRegistry.getContext();

final Intent intent =
       context.getPackageManager().getLaunchIntentForPackage(APP_PACKAGE);

intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK);

context.startActivity(intent);



mDevice.wait(Until.hasObject(By.pkg(APP_PACKAGE).depth(0)), TIMEOUT);

}
ホームアプリへの遷移を待つ
LaunchIntentで起動する


@Test

public void testLogin() {

UiObject2 emailForm = mDevice.findObject(

By.res("info.akkuma.spoonwithuiautomator:id/email"));

emailForm.setText("hoge@example.jp");



UiObject2 passwordForm = mDevice.findObject(

By.res("info.akkuma.spoonwithuiautomator:id/password"));

passwordForm.setText("dragon");



UiObject2 submitButton = mDevice.findObject(

By.res("info.akkuma.spoonwithuiautomator:id/submit"));

submitButton.click();



mDevice.wait(Until.hasObject(By.text("ホーム")), TIMEOUT);



UiObject2 loginSuccessText = mDevice.findObject(By.text("ログイン完了"));



Assert.assertNotNull(loginSuccessText);

}
R.idリソースが利用できない
Activityを特定できる文字列でActivityの遷移を待つ
<?xml version="1.0" encoding="utf-8"?>

<manifest xmlns:android="http://schemas.android.com/apk/res/android"

xmlns:tools="http://schemas.android.com/tools"

package=“com.example.hoge.test”>



<uses-sdk android:minSdkVersion="14"

android:targetSdkVersion="22"

tools:overrideLibrary="android.support.test.uiautomator.v18"/>



<application>

<uses-library android:name="android.test.runner" />

</application>



<instrumentation tools:replace="android:label"

android:name="android.support.test.runner.AndroidJUnitRunner"

android:targetPackage="com.example.hoge"

android:handleProfiling="false"

android:functionalTest="false"

android:label="@string/instrumentation_label"/>



</manifest>
ライブラリ側のuses-sdkをoverride
@RunWith(AndroidJUnit4.class)

@SdkSuppress(minSdkVersion = 18)

public class MainActivityUiTest {

private static final String APP_PACKAGE =
"info.akkuma.spoonwithuiautomator";

private static final int TIMEOUT = 5000;

private UiDevice mDevice;



@Before

public void startApp() {

mDevice = UiDevice.getInstance(
InstrumentationRegistry.getInstrumentation());

mDevice.pressHome();



String launcherPackage = getLauncherPackageName();
SdkSuppressでテスト毎に
動作するAPI Levelを指定できる
UiObject obj1 = mDevice.findObject(
new UiSelector().resourceId("com.example.hoge:id/form"));
UiObject2 obj2 = mDevice.findObject(By.res("com.example.hoge:id/form"));
無印UiObjectはつぶせ!
1.3sec
0.2sec
public static File screenshot(Activity activity, String tag, String testClassName,

String testMethodName) {
public static File screenshot(UiDevice uiDevice, String tag, String testClassName,

String testMethodName) {
private static void takeScreenshot(File file, final Activity activity) throws IOException {

View view = activity.getWindow().getDecorView();

final Bitmap bitmap = Bitmap.createBitmap(view.getWidth(), view.getHeight(),
ARGB_8888);



if (Looper.myLooper() == Looper.getMainLooper()) {

// On main thread already, Just Do It™.

drawDecorViewToBitmap(activity, bitmap);

} else {

// On a background thread, post to main.

final CountDownLatch latch = new CountDownLatch(1);

activity.runOnUiThread(new Runnable() {

@Override public void run() {

try {

drawDecorViewToBitmap(activity, bitmap);

} finally {

latch.countDown();

}

}

});

try {

latch.await();

} catch (InterruptedException e) {

String msg = "Unable to get screenshot " + file.getAbsolutePath();

Log.e(TAG, msg, e);

throw new RuntimeException(msg, e);

}


(略)
private static void takeScreenshot(File file, UiDevice uiDevice) throws IOException {

uiDevice.takeScreenshot(file);

}
最上位のViewをBitmap出力
Fileに保存
スクリーンショットを
Fileに保存
UIAutomatorでボット作ってみた
UIAutomatorでボット作ってみた
UIAutomatorでボット作ってみた
UIAutomatorでボット作ってみた

More Related Content

Similar to UIAutomatorでボット作ってみた

Tk2323 lecture 3 intent
Tk2323 lecture 3   intentTk2323 lecture 3   intent
Tk2323 lecture 3 intent
MengChun Lam
 
Pertemuan 03 - Activities and intents.pptx
Pertemuan 03 - Activities and intents.pptxPertemuan 03 - Activities and intents.pptx
Pertemuan 03 - Activities and intents.pptx
MUHAMMADRIFKIPERMANA2
 
Chapter 11.5
Chapter 11.5Chapter 11.5
Chapter 11.5sotlsoc
 
Day 5
Day 5Day 5
Creating a Facebook Clone - Part XIV - Transcript.pdf
Creating a Facebook Clone - Part XIV - Transcript.pdfCreating a Facebook Clone - Part XIV - Transcript.pdf
Creating a Facebook Clone - Part XIV - Transcript.pdf
ShaiAlmog1
 
03 page navigation and data binding in windows runtime apps
03   page navigation and data binding in windows runtime apps03   page navigation and data binding in windows runtime apps
03 page navigation and data binding in windows runtime apps
WindowsPhoneRocks
 
Creating a Facebook Clone - Part XIV.pdf
Creating a Facebook Clone - Part XIV.pdfCreating a Facebook Clone - Part XIV.pdf
Creating a Facebook Clone - Part XIV.pdf
ShaiAlmog1
 
Notifications
NotificationsNotifications
Notifications
Youssef ELBOUZIANI
 
Developing social simulations with UbikSim
Developing social simulations with UbikSimDeveloping social simulations with UbikSim
Developing social simulations with UbikSim
Emilio Serrano
 

Similar to UIAutomatorでボット作ってみた (9)

Tk2323 lecture 3 intent
Tk2323 lecture 3   intentTk2323 lecture 3   intent
Tk2323 lecture 3 intent
 
Pertemuan 03 - Activities and intents.pptx
Pertemuan 03 - Activities and intents.pptxPertemuan 03 - Activities and intents.pptx
Pertemuan 03 - Activities and intents.pptx
 
Chapter 11.5
Chapter 11.5Chapter 11.5
Chapter 11.5
 
Day 5
Day 5Day 5
Day 5
 
Creating a Facebook Clone - Part XIV - Transcript.pdf
Creating a Facebook Clone - Part XIV - Transcript.pdfCreating a Facebook Clone - Part XIV - Transcript.pdf
Creating a Facebook Clone - Part XIV - Transcript.pdf
 
03 page navigation and data binding in windows runtime apps
03   page navigation and data binding in windows runtime apps03   page navigation and data binding in windows runtime apps
03 page navigation and data binding in windows runtime apps
 
Creating a Facebook Clone - Part XIV.pdf
Creating a Facebook Clone - Part XIV.pdfCreating a Facebook Clone - Part XIV.pdf
Creating a Facebook Clone - Part XIV.pdf
 
Notifications
NotificationsNotifications
Notifications
 
Developing social simulations with UbikSim
Developing social simulations with UbikSimDeveloping social simulations with UbikSim
Developing social simulations with UbikSim
 

Recently uploaded

ethical hacking in wireless-hacking1.ppt
ethical hacking in wireless-hacking1.pptethical hacking in wireless-hacking1.ppt
ethical hacking in wireless-hacking1.ppt
Jayaprasanna4
 
Cosmetic shop management system project report.pdf
Cosmetic shop management system project report.pdfCosmetic shop management system project report.pdf
Cosmetic shop management system project report.pdf
Kamal Acharya
 
addressing modes in computer architecture
addressing modes  in computer architectureaddressing modes  in computer architecture
addressing modes in computer architecture
ShahidSultan24
 
Planning Of Procurement o different goods and services
Planning Of Procurement o different goods and servicesPlanning Of Procurement o different goods and services
Planning Of Procurement o different goods and services
JoytuBarua2
 
Courier management system project report.pdf
Courier management system project report.pdfCourier management system project report.pdf
Courier management system project report.pdf
Kamal Acharya
 
Democratizing Fuzzing at Scale by Abhishek Arya
Democratizing Fuzzing at Scale by Abhishek AryaDemocratizing Fuzzing at Scale by Abhishek Arya
Democratizing Fuzzing at Scale by Abhishek Arya
abh.arya
 
HYDROPOWER - Hydroelectric power generation
HYDROPOWER - Hydroelectric power generationHYDROPOWER - Hydroelectric power generation
HYDROPOWER - Hydroelectric power generation
Robbie Edward Sayers
 
Vaccine management system project report documentation..pdf
Vaccine management system project report documentation..pdfVaccine management system project report documentation..pdf
Vaccine management system project report documentation..pdf
Kamal Acharya
 
Immunizing Image Classifiers Against Localized Adversary Attacks
Immunizing Image Classifiers Against Localized Adversary AttacksImmunizing Image Classifiers Against Localized Adversary Attacks
Immunizing Image Classifiers Against Localized Adversary Attacks
gerogepatton
 
Design and Analysis of Algorithms-DP,Backtracking,Graphs,B&B
Design and Analysis of Algorithms-DP,Backtracking,Graphs,B&BDesign and Analysis of Algorithms-DP,Backtracking,Graphs,B&B
Design and Analysis of Algorithms-DP,Backtracking,Graphs,B&B
Sreedhar Chowdam
 
Event Management System Vb Net Project Report.pdf
Event Management System Vb Net  Project Report.pdfEvent Management System Vb Net  Project Report.pdf
Event Management System Vb Net Project Report.pdf
Kamal Acharya
 
TECHNICAL TRAINING MANUAL GENERAL FAMILIARIZATION COURSE
TECHNICAL TRAINING MANUAL   GENERAL FAMILIARIZATION COURSETECHNICAL TRAINING MANUAL   GENERAL FAMILIARIZATION COURSE
TECHNICAL TRAINING MANUAL GENERAL FAMILIARIZATION COURSE
DuvanRamosGarzon1
 
COLLEGE BUS MANAGEMENT SYSTEM PROJECT REPORT.pdf
COLLEGE BUS MANAGEMENT SYSTEM PROJECT REPORT.pdfCOLLEGE BUS MANAGEMENT SYSTEM PROJECT REPORT.pdf
COLLEGE BUS MANAGEMENT SYSTEM PROJECT REPORT.pdf
Kamal Acharya
 
weather web application report.pdf
weather web application report.pdfweather web application report.pdf
weather web application report.pdf
Pratik Pawar
 
Forklift Classes Overview by Intella Parts
Forklift Classes Overview by Intella PartsForklift Classes Overview by Intella Parts
Forklift Classes Overview by Intella Parts
Intella Parts
 
在线办理(ANU毕业证书)澳洲国立大学毕业证录取通知书一模一样
在线办理(ANU毕业证书)澳洲国立大学毕业证录取通知书一模一样在线办理(ANU毕业证书)澳洲国立大学毕业证录取通知书一模一样
在线办理(ANU毕业证书)澳洲国立大学毕业证录取通知书一模一样
obonagu
 
road safety engineering r s e unit 3.pdf
road safety engineering  r s e unit 3.pdfroad safety engineering  r s e unit 3.pdf
road safety engineering r s e unit 3.pdf
VENKATESHvenky89705
 
Hybrid optimization of pumped hydro system and solar- Engr. Abdul-Azeez.pdf
Hybrid optimization of pumped hydro system and solar- Engr. Abdul-Azeez.pdfHybrid optimization of pumped hydro system and solar- Engr. Abdul-Azeez.pdf
Hybrid optimization of pumped hydro system and solar- Engr. Abdul-Azeez.pdf
fxintegritypublishin
 
Top 10 Oil and Gas Projects in Saudi Arabia 2024.pdf
Top 10 Oil and Gas Projects in Saudi Arabia 2024.pdfTop 10 Oil and Gas Projects in Saudi Arabia 2024.pdf
Top 10 Oil and Gas Projects in Saudi Arabia 2024.pdf
Teleport Manpower Consultant
 
WATER CRISIS and its solutions-pptx 1234
WATER CRISIS and its solutions-pptx 1234WATER CRISIS and its solutions-pptx 1234
WATER CRISIS and its solutions-pptx 1234
AafreenAbuthahir2
 

Recently uploaded (20)

ethical hacking in wireless-hacking1.ppt
ethical hacking in wireless-hacking1.pptethical hacking in wireless-hacking1.ppt
ethical hacking in wireless-hacking1.ppt
 
Cosmetic shop management system project report.pdf
Cosmetic shop management system project report.pdfCosmetic shop management system project report.pdf
Cosmetic shop management system project report.pdf
 
addressing modes in computer architecture
addressing modes  in computer architectureaddressing modes  in computer architecture
addressing modes in computer architecture
 
Planning Of Procurement o different goods and services
Planning Of Procurement o different goods and servicesPlanning Of Procurement o different goods and services
Planning Of Procurement o different goods and services
 
Courier management system project report.pdf
Courier management system project report.pdfCourier management system project report.pdf
Courier management system project report.pdf
 
Democratizing Fuzzing at Scale by Abhishek Arya
Democratizing Fuzzing at Scale by Abhishek AryaDemocratizing Fuzzing at Scale by Abhishek Arya
Democratizing Fuzzing at Scale by Abhishek Arya
 
HYDROPOWER - Hydroelectric power generation
HYDROPOWER - Hydroelectric power generationHYDROPOWER - Hydroelectric power generation
HYDROPOWER - Hydroelectric power generation
 
Vaccine management system project report documentation..pdf
Vaccine management system project report documentation..pdfVaccine management system project report documentation..pdf
Vaccine management system project report documentation..pdf
 
Immunizing Image Classifiers Against Localized Adversary Attacks
Immunizing Image Classifiers Against Localized Adversary AttacksImmunizing Image Classifiers Against Localized Adversary Attacks
Immunizing Image Classifiers Against Localized Adversary Attacks
 
Design and Analysis of Algorithms-DP,Backtracking,Graphs,B&B
Design and Analysis of Algorithms-DP,Backtracking,Graphs,B&BDesign and Analysis of Algorithms-DP,Backtracking,Graphs,B&B
Design and Analysis of Algorithms-DP,Backtracking,Graphs,B&B
 
Event Management System Vb Net Project Report.pdf
Event Management System Vb Net  Project Report.pdfEvent Management System Vb Net  Project Report.pdf
Event Management System Vb Net Project Report.pdf
 
TECHNICAL TRAINING MANUAL GENERAL FAMILIARIZATION COURSE
TECHNICAL TRAINING MANUAL   GENERAL FAMILIARIZATION COURSETECHNICAL TRAINING MANUAL   GENERAL FAMILIARIZATION COURSE
TECHNICAL TRAINING MANUAL GENERAL FAMILIARIZATION COURSE
 
COLLEGE BUS MANAGEMENT SYSTEM PROJECT REPORT.pdf
COLLEGE BUS MANAGEMENT SYSTEM PROJECT REPORT.pdfCOLLEGE BUS MANAGEMENT SYSTEM PROJECT REPORT.pdf
COLLEGE BUS MANAGEMENT SYSTEM PROJECT REPORT.pdf
 
weather web application report.pdf
weather web application report.pdfweather web application report.pdf
weather web application report.pdf
 
Forklift Classes Overview by Intella Parts
Forklift Classes Overview by Intella PartsForklift Classes Overview by Intella Parts
Forklift Classes Overview by Intella Parts
 
在线办理(ANU毕业证书)澳洲国立大学毕业证录取通知书一模一样
在线办理(ANU毕业证书)澳洲国立大学毕业证录取通知书一模一样在线办理(ANU毕业证书)澳洲国立大学毕业证录取通知书一模一样
在线办理(ANU毕业证书)澳洲国立大学毕业证录取通知书一模一样
 
road safety engineering r s e unit 3.pdf
road safety engineering  r s e unit 3.pdfroad safety engineering  r s e unit 3.pdf
road safety engineering r s e unit 3.pdf
 
Hybrid optimization of pumped hydro system and solar- Engr. Abdul-Azeez.pdf
Hybrid optimization of pumped hydro system and solar- Engr. Abdul-Azeez.pdfHybrid optimization of pumped hydro system and solar- Engr. Abdul-Azeez.pdf
Hybrid optimization of pumped hydro system and solar- Engr. Abdul-Azeez.pdf
 
Top 10 Oil and Gas Projects in Saudi Arabia 2024.pdf
Top 10 Oil and Gas Projects in Saudi Arabia 2024.pdfTop 10 Oil and Gas Projects in Saudi Arabia 2024.pdf
Top 10 Oil and Gas Projects in Saudi Arabia 2024.pdf
 
WATER CRISIS and its solutions-pptx 1234
WATER CRISIS and its solutions-pptx 1234WATER CRISIS and its solutions-pptx 1234
WATER CRISIS and its solutions-pptx 1234
 

UIAutomatorでボット作ってみた

  • 2.
  • 3.
  • 4.
  • 5.
  • 6. 
 UiDevice uiDevice = UiDevice.getInstance(InstrumentationRegistry.getInstrumentation());
 UiObject2 button = uiDevice.findObject(By.res("info.akkuma.example:id/submit"));
 button.click();
  • 7.
  • 8.
  • 9. @Before
 public void startApp() {
 mDevice =         UiDevice.getInstance(InstrumentationRegistry.getInstrumentation());
 mDevice.pressHome();
 
 String launcherPackage = getLauncherPackageName();
 mDevice.wait(Until.hasObject(By.pkg(launcherPackage).depth(0)), TIMEOUT);
 
 Context context = InstrumentationRegistry.getContext();
 final Intent intent =        context.getPackageManager().getLaunchIntentForPackage(APP_PACKAGE);
 intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK);
 context.startActivity(intent);
 
 mDevice.wait(Until.hasObject(By.pkg(APP_PACKAGE).depth(0)), TIMEOUT);
 } ホームアプリへの遷移を待つ LaunchIntentで起動する
  • 10. 
 @Test
 public void testLogin() {
 UiObject2 emailForm = mDevice.findObject(
 By.res("info.akkuma.spoonwithuiautomator:id/email"));
 emailForm.setText("hoge@example.jp");
 
 UiObject2 passwordForm = mDevice.findObject(
 By.res("info.akkuma.spoonwithuiautomator:id/password"));
 passwordForm.setText("dragon");
 
 UiObject2 submitButton = mDevice.findObject(
 By.res("info.akkuma.spoonwithuiautomator:id/submit"));
 submitButton.click();
 
 mDevice.wait(Until.hasObject(By.text("ホーム")), TIMEOUT);
 
 UiObject2 loginSuccessText = mDevice.findObject(By.text("ログイン完了"));
 
 Assert.assertNotNull(loginSuccessText);
 } R.idリソースが利用できない Activityを特定できる文字列でActivityの遷移を待つ
  • 11.
  • 12.
  • 13.
  • 14. <?xml version="1.0" encoding="utf-8"?>
 <manifest xmlns:android="http://schemas.android.com/apk/res/android"
 xmlns:tools="http://schemas.android.com/tools"
 package=“com.example.hoge.test”>
 
 <uses-sdk android:minSdkVersion="14"
 android:targetSdkVersion="22"
 tools:overrideLibrary="android.support.test.uiautomator.v18"/>
 
 <application>
 <uses-library android:name="android.test.runner" />
 </application>
 
 <instrumentation tools:replace="android:label"
 android:name="android.support.test.runner.AndroidJUnitRunner"
 android:targetPackage="com.example.hoge"
 android:handleProfiling="false"
 android:functionalTest="false"
 android:label="@string/instrumentation_label"/>
 
 </manifest> ライブラリ側のuses-sdkをoverride
  • 15. @RunWith(AndroidJUnit4.class)
 @SdkSuppress(minSdkVersion = 18)
 public class MainActivityUiTest {
 private static final String APP_PACKAGE = "info.akkuma.spoonwithuiautomator";
 private static final int TIMEOUT = 5000;
 private UiDevice mDevice;
 
 @Before
 public void startApp() {
 mDevice = UiDevice.getInstance( InstrumentationRegistry.getInstrumentation());
 mDevice.pressHome();
 
 String launcherPackage = getLauncherPackageName(); SdkSuppressでテスト毎に 動作するAPI Levelを指定できる
  • 16. UiObject obj1 = mDevice.findObject( new UiSelector().resourceId("com.example.hoge:id/form")); UiObject2 obj2 = mDevice.findObject(By.res("com.example.hoge:id/form")); 無印UiObjectはつぶせ! 1.3sec 0.2sec
  • 17.
  • 18.
  • 19.
  • 20. public static File screenshot(Activity activity, String tag, String testClassName,
 String testMethodName) { public static File screenshot(UiDevice uiDevice, String tag, String testClassName,
 String testMethodName) {
  • 21. private static void takeScreenshot(File file, final Activity activity) throws IOException {
 View view = activity.getWindow().getDecorView();
 final Bitmap bitmap = Bitmap.createBitmap(view.getWidth(), view.getHeight(), ARGB_8888);
 
 if (Looper.myLooper() == Looper.getMainLooper()) {
 // On main thread already, Just Do It™.
 drawDecorViewToBitmap(activity, bitmap);
 } else {
 // On a background thread, post to main.
 final CountDownLatch latch = new CountDownLatch(1);
 activity.runOnUiThread(new Runnable() {
 @Override public void run() {
 try {
 drawDecorViewToBitmap(activity, bitmap);
 } finally {
 latch.countDown();
 }
 }
 });
 try {
 latch.await();
 } catch (InterruptedException e) {
 String msg = "Unable to get screenshot " + file.getAbsolutePath();
 Log.e(TAG, msg, e);
 throw new RuntimeException(msg, e);
 } 
 (略) private static void takeScreenshot(File file, UiDevice uiDevice) throws IOException {
 uiDevice.takeScreenshot(file);
 } 最上位のViewをBitmap出力 Fileに保存 スクリーンショットを Fileに保存