先上 JAVA 代码,再解释

try {
                    View fastLoginDialog = solo.getView("xxxxxxx:id/has_account",0);
                    solo.clickOnView(fastLoginDialog);

                    View username = solo.getView("xxxxxxx:id/edit_account",0);
                    EditText un = (EditText)username;                 
                    solo.typeText(un, "hh1906");
                    View pwd = solo.getView("xxxxxxx:id/edit_pwd",0);
                    EditText pw = (EditText)pwd;
                    solo.typeText(pw, "123456");
                    solo.clickOnButton("登 录");
                    Thread.sleep(5000);


              } catch (AssertionFailedError e) {
                  //登录操作
                    View rb = solo.getView("xxxxxxx:id/right_bt",0);  
                    solo.clickOnView(rb);  
                    View avatar = solo.getView("xxxxxxx:id/avatar",0);  
                    solo.clickOnView(avatar); 
                    View username = solo.getView("xxxxxxx:id/edit_account",0);
                    EditText un = (EditText)username;             
                    solo.typeText(un, "hh1906");
                    View pwd = solo.getView("xxxxxxx:id/edit_pwd",0);
                    EditText pw = (EditText)pwd;
                    solo.typeText(pw, "123456");
                    solo.clickOnButton("登 录");
                    Thread.sleep(5000);

              }

是这样,在首页时会根据用户是否是第一次使用应用而弹出一个弹出框,如果用户第一次使用则弹出 “立即体验” 弹框,如果用户不是第一次使用则不弹出,我想根据弹框中的的某个元素(代码中 try 块中第一行)是否能够获取到来判断是否弹出了框从而执行不同的操作,即分别执行 try 块或 catch 块中的内容,这个要怎么弄?上面的代码执行后用例直接 failed,具体错误是 junit.framework.AssertionFailedError: View with id: 'xxxxxxx:id/has_account' is not found!
at com.jayway.android.robotium.solo.Solo.getView(Solo.java:2024)
at xxxxxxx.test.LoginTest.testLogin(LoginTest.java:82)
at java.lang.reflect.Method.invokeNative(Native Method)
at android.test.InstrumentationTestCase.runMethod(InstrumentationTestCase.java:214)
at android.test.InstrumentationTestCase.runTest(InstrumentationTestCase.java:199)
at android.test.ActivityInstrumentationTestCase2.runTest(ActivityInstrumentationTestCase2.java:192)
at android.test.AndroidTestRunner.runTest(AndroidTestRunner.java:191)
at android.test.AndroidTestRunner.runTest(AndroidTestRunner.java:176)
at android.test.InstrumentationTestRunner.onStart(InstrumentationTestRunner.java:554)
at android.app.Instrumentation$InstrumentationThread.run(Instrumentation.java:1701)

并不会执行 catch 块中的内容,对 java 不熟,最近刚弄 robotium,求大神指教


↙↙↙阅读原文可查看相关链接,并与作者交流