Robotium 如何根据 元素定位是否成功 来选择执行分支

zhangyang · October 27, 2014 · Last by 剪烛 replied at October 28, 2014 · 1538 hits

先上 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,求大神指教

共收到 8 条回复 时间 点赞
1Floor has deleted

如果有 bug,每次使用都会弹出,怎么处理?
如果有另一个 bug,每次使用都不弹出,怎么处理?

#2 楼 @sanlengjingvv you ask me? I ask who....我这不是在问大家嘛 ……-^ 每次弹出就每次执行 try 块,每次不弹出就每次执行 catch 块 关键是现在这样写不行,只要 solo.getView("xxxxxxx:id/has_account",0) 这句一出错,整个用例就结束了。。

这么复杂,为什么不直接 if else 呢?

#4 楼 @shixue33 if else 不行,我如此机智怎么会想不到 if else ==! 关键是 语句执行出错,如果语句执行时不出错能返回个比如是否 null 就可以用 if else 来判断

问题解决了,捕获 AssertionFailedError 是不行的,捕获不到,得捕获它上一级 即 Error

#5 楼 @hobbs = =!robotium 不是提供了 waitforview 的方法么。。。。不过你这样也行

#7 楼 @shixue33 看了下 waitforview 的 API 确实可以,太感谢了,学习了,主要对 robotium 还不熟,方法还没学全

需要 Sign In 后方可回复, 如果你还没有账号请点击这里 Sign Up