您好,登錄后才能下訂單哦!
在工作中需要在沒有項目源碼的情況下直接使用robotium測試目標android平臺launcher,平臺的版本基于當前最新的android 4.4.2。之前在驗證可行性的時候使用本人同樣使用android4.4.2的測試手機htc incredable s針對一個只有apk的notepad應用做過同樣的驗證,在測試手機上運行完全沒有問題。該測試代碼如下:
package com.example.android.notepad.tryout; import com.robotium.solo.Solo; import android.test.ActivityInstrumentationTestCase2; import android.widget.TextView; import android.app.Activity; @SuppressWarnings("rawtypes") public class NotePadTest extends ActivityInstrumentationTestCase2{ private static Solo solo = null; public Activity activity; private static final int NUMBER_TOTAL_CASES = 2; private static int run = 0; private static Class<?> launchActivityClass; //對應re-sign.jar生成出來的信息框里的兩個值 private static String mainActiviy = "com.example.android.notepad.NotesList"; private static String packageName = "com.example.android.notepad"; static { try { launchActivityClass = Class.forName(mainActiviy); } catch (ClassNotFoundException e) { throw new RuntimeException(e); } } @SuppressWarnings("unchecked") public NotePadTest() { super(packageName, launchActivityClass); } @Override public void setUp() throws Exception { //setUp() is run before a test case is started. //This is where the solo object is created. super.setUp(); //The variable solo has to be static, since every time after a case's finished, this class TCCreateNote would be re-instantiated // which would lead to soto to re-instantiated to be null if it's not set as static //TextView title = (TextView)getActivity().findViewById(Ref.id.title); if(solo == null) { NotePadTest.solo = new Solo(getInstrumentation(),getActivity()); } } @Override public void tearDown() throws Exception { //Check whether it's the last case executed. run += countTestCases(); if(run >= NUMBER_TOTAL_CASES) { solo.finishOpenedActivities(); } } public void testAddNoteCNTitle() throws Exception { //Thread.sleep(5000); solo.clickOnMenuItem("Add note"); solo.enterText(0, "中文標簽筆記"); solo.clickOnMenuItem("Save"); solo.clickInList(0); solo.clearEditText(0); solo.enterText(0, "Text 1"); solo.clickOnMenuItem("Save"); solo.assertCurrentActivity("Expected NotesList Activity", "NotesList"); solo.clickLongOnText("中文標簽筆記"); solo.clickOnText("Delete"); } public void testAddNoteEngTitle() throws Exception { solo.clickOnMenuItem("Add note"); solo.enterText(0, "English Title Note"); solo.clickOnMenuItem("Save"); solo.clickInList(0); solo.clearEditText(0); solo.enterText(0, "Text 1"); solo.clickOnMenuItem("Save"); solo.assertCurrentActivity("Expected NotesList Activity", "NotesList"); solo.clickLongOnText("English Title Note"); solo.clickOnText("Delete"); } }
@Override public void setUp() throws Exception { //setUp() is run before a test case is started. //This is where the solo object is created. super.setUp(); //The variable solo has to be static, since every time after a case's finished, this class TCCreateNote would be re-instantiated // which would lead to soto to re-instantiated to be null if it's not set as static if(solo == null) { NotePadTest.solo = new Solo(getInstrumentation(),getActivity()); } }
/** * Utility method for launching an activity with a specific Intent. * * <p><b>NOTE:</b> The parameter <i>pkg</i> must refer to the package identifier of the * package hosting the activity to be launched, which is specified in the AndroidManifest.xml * file. This is not necessarily the same as the java package name. * * @param pkg The package hosting the activity to be launched. * @param activityCls The activity class to launch. * @param intent The intent to launch with * @return The activity, or null if non launched. */ @SuppressWarnings("unchecked") public final <T extends Activity> T launchActivityWithIntent( String pkg, Class<T> activityCls, Intent intent) { intent.setClassName(pkg, activityCls.getName()); intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); T activity = (T) getInstrumentation().startActivitySync(intent); getInstrumentation().waitForIdleSync(); return activity; }
/** * Synchronously wait for the application to be idle. Can not be called * from the main application thread -- use {@link #start} to execute * instrumentation in its own thread. */ public void waitForIdleSync() { validateNotAppThread(); Idler idler = new Idler(null); mMessageQueue.addIdleHandler(idler); mThread.getHandler().post(new EmptyRunnable()); idler.waitForIdle(); }這里按照本人的理解做的事情大概如下:
solo = new Solo(getInstrumentation());因為我們的launcher在robotium在kill掉原來的launcher進程的時候就會自動起來,所以并不需要手動的去getActivity()去啟動。這種方法在不能啟動起來的apk如notepad上面就不行,不信你去掉getActivity()的調用,保證notepad不會啟動或者放到前臺。但是如果你在開始測試前先把notepad手動起來并放到前臺,測試還是會正常進行的。比如以下的驗證性代碼:
package com.example.android.notepad.tryout; import com.robotium.solo.Solo; import android.test.ActivityInstrumentationTestCase2; import android.widget.TextView; import android.app.Activity; @SuppressWarnings("rawtypes") public class NotePadTest extends ActivityInstrumentationTestCase2{ private static Solo solo = null; public Activity activity; private static final int NUMBER_TOTAL_CASES = 2; private static int run = 0; private static Class<?> launchActivityClass; //對應re-sign.jar生成出來的信息框里的兩個值 private static String mainActiviy = "com.example.android.notepad.NotesList"; private static String packageName = "com.example.android.notepad"; static { try { launchActivityClass = Class.forName(mainActiviy); } catch (ClassNotFoundException e) { throw new RuntimeException(e); } } @SuppressWarnings("unchecked") public NotePadTest() { super(packageName, launchActivityClass); } @Override public void setUp() throws Exception { //setUp() is run before a test case is started. //This is where the solo object is created. super.setUp(); //The variable solo has to be static, since every time after a case's finished, this class TCCreateNote would be re-instantiated // which would lead to soto to re-instantiated to be null if it's not set as static //TextView title = (TextView)getActivity().findViewById(Ref.id.title); if(solo == null) { NotePadTest.solo = new Solo(getInstrumentation());//, getActivity()); } } @Override public void tearDown() throws Exception { //Check whether it's the last case executed. run += countTestCases(); if(run >= NUMBER_TOTAL_CASES) { solo.finishOpenedActivities(); } } public void testAddNoteCNTitle() throws Exception { //getActivity(); Thread.sleep(5000); solo.clickOnMenuItem("Add note"); solo.enterText(0, "中文標簽筆記"); solo.clickOnMenuItem("Save"); solo.clickInList(0); solo.clearEditText(0); solo.enterText(0, "Text 1"); solo.clickOnMenuItem("Save"); solo.assertCurrentActivity("Expected NotesList Activity", "NotesList"); solo.clickLongOnText("中文標簽筆記"); solo.clickOnText("Delete"); } }初始化solo的時候和testcase里面都沒有去調用getActivity(),但是在testcase開始前先睡眠5秒,如果在這5秒的過程中你手動把notepad給啟動起來,那么睡眠時間過后測試會繼續正常運行。
@Override public MyActivity getActivity() { if (mActivity == null) { Intent intent = new Intent(getInstrumentation().getTargetContext(), MyActivity.class); intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); // register activity that need to be monitored. monitor = getInstrumentation().addMonitor(MyActivity.class.getName(), null, false); getInstrumentation().getTargetContext().startActivity(intent); mActivity = (MyActivity) getInstrumentation().waitForMonitor(monitor); setActivity(mActivity); } return mActivity; }鑒于本人現在只是做前期的可行×××,夠用就好,且周末手頭上也沒有目標機器在手進行驗證,所以有興趣的朋友就自己去嘗試下吧,
作者 | 自主博客 | 微信 | CSDN |
天地會珠海分舵 | http://techgogogo.com | 服務號:TechGoGoGo 掃描碼:
| 向AI問一下細節 推薦閱讀:
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。 猜你喜歡最新資訊相關推薦相關標簽AI
助 手
宜城市|
玛曲县|
武陟县|
凤台县|
苍溪县|
道真|
于田县|
吉木萨尔县|
长汀县|
嘉兴市|
横峰县|
泉州市|
繁昌县|
台南市|
株洲市|
高碑店市|
景宁|
肇州县|
泾川县|
芜湖市|
碌曲县|
普陀区|
云龙县|
嫩江县|
桂平市|
安多县|
望江县|
龙游县|
巫溪县|
青铜峡市|
响水县|
宁阳县|
德昌县|
井研县|
修文县|
巫山县|
从江县|
电白县|
洮南市|
尉氏县|
南投市|
|