مشخصات مقاله
تست لایه ی UI اپلیکیشن/رابط کاربری با استفاده از فریم ورک تست گیری Robotium
Robotium
Robotium یک افزونه برای فریم ورک تست گیری اندروید است و صرفا برای آسان سازی نوشتن تست های نرم افزاری برای UI اپلیکیشن طراحی شده است. تست های Robotium اعضای کلاس ActivityInstrumentationTestCase2 را به ارث برده و به شما این امکان را می دهد تا test case هایی (مورد آزمایش) بنویسید که چندین activity را شامل می شود.
تست هایی که بر اساس Robotium طراحی می شوند منحصرا با لایه UI اپلیکیشن تعامل دارند. به عبارت دیگر این نوع تست ها فرض می گیرند که اپلیکیشن مورد آزمایش یک جعبه ی سیاه است که محتویات و کد داخل آن اطلاعاتی ندارد.
Robotium، همان طور که قبلا ذکر شد، نوعی افزونه برای فریم ورک تست گیری اندروید می باشد (چارچوب طراحی تست نرم افزاری اندروید را بسط می دهد) که نوشتن تست برای اپلیکیشن های اندرویدی را آسان می سازد.
کلاس اصلی فریم ورک مزبور برای طراحی تست، Solo می باشد. این کلاس به مجرد ساخت test case و تست activity مقداردهی اولیه می شود.
نصب Robotium
جهت تست Robotium در پروژه ی اندرویدی خود، لازم است کتابخانه (dependency) مورد نیاز آخرین ویرایش Robotium را به فایل build اضافه نمایید.
1 2 3 4 5 | dependencies { // Unit testing dependencies androidTestCompile 'com.jayway.android.robotium:robotium:5.4.12' } <button></button> |
در زمان تنظیم مقاله ی حاضر، 5.4.1 جدیدترین ویرایش Rbotium می باشد.
نمونه ای از پیاده سازی تست های Robotium
کد زیر نحوه ی پیاده سازی و استفاده از فریم ورک Robotium در تست یک activity (تست لایه ی رابط کاربری اپلیکیشن) را نمایش می دهد.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 | 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< simpleactivity > { 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(); } } </ simpleactivity >< button ></ button > |
در زیر نمونه ی دیگری از کاربرد متد تست گیری 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 | // 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 توابعی برای فراخوانی کامپوننت های رابط کاربری ( کامپوننت های UI اپلیکیشن اندرویدی) در اختیار توسعه قرار می دهد. در جدول زیر تعدادی از این توابع را همراه با شرح کاربرد مشاهده می نمایید.
می توانید با فراخوانی متد solo.setActivityOrientation(Solo.LANDSCAPE) وضعیت نمایش (orientation) صفحه ی جاری (activity) را تنظیم نمایید.
جهت تست رشته های ترجمه شده و بین المللی (internationalized strings)، می بایست با فراخوانی متد getString(id) به فایل resources از پروژه مورد تست دسترسی پیدا کنید. مثال:
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 را راه اندازی می کنید، با کلیک راست بر روی test class (کلاس آزمایشی) و انتخاب آیتم Run-As ▸ Android JUnit Test از منو، اجرا نمایید.
در صورت تمایل می توانید تست های Robotium را با درج دستور زیر در پنجره ی فرمان، اجرا نمایید.
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 ایجاد کرده و فایل Robotium JAR را در آن جایگذاری نمایید.
پس از جایگذاری فایل JAR در پوشه ی نام برده، مجموعه ابزار Android (android tooling) محیط کاری Eclipse خود به صورت اتوماتیک آن را به build path پروژه ی شما اضافه می کنید. لازم به ذکر است که قرار دادن فایل JAR در پوشه ای با اسم متفاوت، غالبا سبب رخداد خطای ClassNotFoundExeception برای کلاس Solo می شود.
ایجاد پروژه ی آزمایشی و افزودن کتابخانه ی Robotium
کلاس آزمایشی (test class) زیر را پیاده سازی نمایید.
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 | 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<simpleactivity> { 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(); } } </simpleactivity><button></button> |
برطرف کردن خطاهای اپلیکیشن
فرض کنید تست با تنظمیات و پیاده سازی شما از عملکرد صحیح برخوردار می باشد ولی اپلیکیشن نتیجه ی مورد انتظار را ارائه نمی دهد. اپلیکیشن را طوری ویرایش کنید که تست با موفقیت انجام شود.