مشخصات مقاله
استفاده از Robotium در اندروید-چهارچوب تست robotium
استفاده از Robotium در اندروید-چهارچوب تست robotium
این فصل از مقاله های آموزشی اندروید به نحوه ی تست برنامه های اندروید با استفاده از چهارچوب تست robotium می پردازد .
فهرست محتوا
1. Robotium چیست و به چه منظور به چهارچوب تست اندروید افزوده شده ؟
2. نصب Robotium
3. مثال
4. Robotium API
5. مرحله ی اجرای تست ها
6. تمرین : طراحی تست های Robotium
- ایجاد پروژه ی آزمایشی و افزودن Robotium
- ساخت پروژه ی آزمایشی و اضافه کردن کتابخانه ی Robotium
- اصلاح و تنظیم برنامه
Robotium چیست و به چه منظور به چهارچوب تست اندروید افزوده شده ؟
Robotiumیک افزونه بر چهارچوب تست متعلق به خود سیستم اندروید است که به منظور آسان سازی امر نوشتن تست های رابط کاربری برای برنامه های اندروید تعبیه شده و بکار گرفته می شود. تست های robotium از ActivityInstrumentationTestCase2 به ارث برده و به برنامه نویس اجازه می دهند test case هایی را در تمامی activity های اندروید ایجاد کند .
تست های Robotium برنامه های کاربردی تحت آزمایش را به عنوان جعبه سیاه (blackbox) می شناسند, به این معنا که تنها با رابط کاربری تعامل برقرار می کند و به کد داخلی برنامه ی مورد نظر کاری ندارد .
با مراجعه به URL زیر می توان به صفحه ی اصلی Robotium دسترسی پیدا کرد :
http://code.google.com/p/robotium/
چهارچوب تست اندروید را به منظور آسان سازی طراحی تست ارائه می دهد. کلاس اصلی که برای انجام تست با Robotium بکار می رود, کلاس Solo می باشد .
Solo با دستورات تست (instrumentation) test case (موردی که باید آزمایش شود) و اولین activity ی که باید تست شود مقداردهی اولیه می شود.
نصب Robotium
برای استفاده از Robotium در پروژه ی آزمایشی اندروید خود, باید کتابخانه ی Robotium.jar را از
http://code.google.com/p/robotium/
دانلود کنید .
در حال حاضر آخرین ورژن منتشر شده Robotium 5.2.1 هست .
در مرحله ی آخر, Robotium.jar را به Build path پروژه ی خود اضافی کنید .
مثال
کدی که ذیل مشاهده می کنید استفاده از Robotium را در یک تست رابط کاربری activity به نمایش می گذارد .
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 | package de.vogella.android.test.target.test; import junit.framework.Assert; import android.test.ActivityInstrumentationTestCase2; import com.robotium.solo.Solo; import de.vogella.android.test.target.SimpleActivity; import de.vogella.android.test.target.SimpleListActivity; public class SimpleActivityTest extends ActivityInstrumentationTestCase2 { private Solo solo; public SimpleActivityTest() { super (SimpleActivity. class ); } public void setUp() throws Exception { solo = new Solo(getInstrumentation(), getActivity()); } @Override public void tearDown() throws Exception { solo.finishOpenedActivities(); } } <button></button> |
ذیل مثال دیگری ارائه شده که پیاده سازی یک لیست را نمایش می دهد .
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 | // check that we have the right activity solo.assertCurrentActivity( "wrong activity" , SimpleActivity. class ); // Click a button which will start a new Activity // Here we use the ID of the string to find the right button solo.clickOnButton(solo.getString(R.string.button1)); // Validate that the Activity is the correct one solo.assertCurrentActivity( "wrong activity" , SimpleListActivity. class ); solo.clickInList( 1 ); // searchForText has a timeout of 5 seconds assertTrue(solo.waitForText( "Android" )); // Assertion solo.clickInList( 2 ); assertTrue(solo.waitForText( "iPhone" )); // Assertion solo.clickInList( 3 ); assertTrue(solo.waitForText( "Blackberry" )); // Assertion solo.goBack(); solo.clickOnButton( "Button2" ); solo.clickOnButton( "Button3" ); // open the menu solo.sendKey(Solo.MENU); solo.clickOnText( "Preferences" ); solo.clickOnText( "User" ); solo.clearEditText( 0 ); Assert.assertTrue(solo.searchText( "" )); solo.enterText( 0 , "http//:www.vogella.com" ); Assert.assertTrue(solo.searchText( "http//:www.vogella.com" )); solo.goBack(); <button></button> |
Robotium API
کلاس Solo توابع متعددی را ویژه ی صدازنی رابط کاربری اندروید ارائه می دهد . تعداد زیادی از متدهای مذکور در لیست ذیل فهرست شده است .
جدول 1. متدهای آزمایشی کلاس solo
جهت قرار گیری activity را می توان با استفاده از متد solo.setActivityOrientation(Solo.LANDSCAPE) تنظیم کرد .
به منظور استفاده از internationalized strings می توانید به فایل های منبع رشته (resource file) با فراخوانی متد getString(id) از پروژه ی تحت آزمایش دسترسی پیدا کرد . برای مثال :
1 2 3 | // Here we use the ID of the string to find the right button solo.clickOnButton(solo.getString(de.vogella.android.test.target.R.string.button1)); <button></button> |
مرحله ی اجرای تست ها
جهت راه اندازی تست Robotium از Eclipse, روی کلاس آزمایشی راست کلیک کرده و گزینه های Run-As → Android JUnit Test انتخاب کنید .
البته می توان Robotium را از خط فرمان (command line) نیز اجرا کرد .
1 2 3 | adb shell am instrument -w de.vogella.android.test.tester/android.test.InstrumentationTestRunner <button></button> |
تمرین : طراحی تست های Robotium
پروژه ی جدیدی به نام com.vogella.android.test.robotium.target ایجاد کنید . حال سه دکمه در اولین activity نمایش دهید . در صورت کلیک روی اولین دکمه, باید activity ی به نمایش گذاشته شود که یک لیست را نشان می دهد .
ایجاد پروژه ی آزمایشی و افزودن Robotium
اکنون پروژه ی آزمایشی به نام com.vogella.android.test.robotium.targetTest ایجاد کنید . پوشه ای به نام libs ایجاد کرده و فایل JAR را داخل آن جای گذاری کنید .
در صورت جای گذاری فایل ذکر شده (JAR) در این پوشه, ابزار و تجهیزات سیستم اندروید (ویژه ی Eclipse) به صورت خودکار فایل JAR را به Build path پروژه ی شما اضافه می کند . قراردهی فایل JAR در پوشه ای با نام متفاوت, مکرراً منجر به استثنای ClassNotFoundExeception برای کلاس Solo می شود
ساخت پروژه ی آزمایشی و اضافه کردن کتابخانه ی Robotium
کلاس زیر را تعریف کنید .
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 | package de.vogella.android.test.target.test; import junit.framework.Assert; import android.test.ActivityInstrumentationTestCase2; import com.robotium.solo.Solo; import de.vogella.android.test.target.SimpleActivity; import de.vogella.android.test.target.SimpleListActivity; public class SimpleActivityTest extends ActivityInstrumentationTestCase2 { private Solo solo; public SimpleActivityTest() { super (SimpleActivity. class ); } public void setUp() throws Exception { solo = new Solo(getInstrumentation(), getActivity()); } @Override public void tearDown() throws Exception { solo.finishOpenedActivities(); } public void testListItemClickShouldDisplayToast() throws Exception { // check that we have the right activity solo.assertCurrentActivity( "wrong activity" , SimpleActivity. class ); // Click a button which will start a new Activity // Here we use the ID of the string to find the right button solo.clickOnButton(solo .getString(de.vogella.android.test.target.R.string.button1)); // assert that the current activity is the SimpleListActivity.class solo.assertCurrentActivity( "wrong activity" , SimpleListActivity. class ); solo.clickInList( 1 ); // searchForText has a timeout of 5 seconds assertTrue(solo.waitForText( "Android" )); // Assertion solo.clickInList( 2 ); assertTrue(solo.waitForText( "iPhone" )); // Assertion solo.clickInList( 3 ); assertTrue(solo.waitForText( "Blackberry" )); // Assertion solo.goBack(); solo.clickOnButton( "Button2" ); solo.clickOnButton( "Button3" ); } public void testListItemClickShouldDisplayToast() throws Exception { // open the menu solo.sendKey(Solo.MENU); solo.clickOnText( "Preferences" ); solo.clickOnText( "User" ); solo.clearEditText( 0 ); Assert.assertTrue(solo.searchText( "" )); solo.enterText( 0 , "http//:www.vogella.com" ); Assert.assertTrue(solo.searchText( "http//:www.vogella.com" )); solo.goBack(); } } <button></button> |
اصلاح و تنظیم برنامه
ابتدا اطمینان کسب کنید که تست مطابق با خصوصیات تعیین شده طراحی و تعبیه شده, سپس برنامه ی کاربردی اندروید خود را گونه ای تنظیم کنید که مرحله ی تست را با موفقیت پشت سر بگذارد .