最近在用Robotium框架做自動(dòng)化測試,遇到一個(gè)問題,就是sendKey()方法報(bào)錯(cuò)。
本質(zhì)的原因是因?yàn)槲覝y試的應(yīng)用有一個(gè)bug,當(dāng)我選擇若干次goback()后應(yīng)該退出到應(yīng)用軟件的主界面,但是卻直接退出應(yīng)用了。
我查了一下源代碼:
01 |
public void goBackToActivity(String name) |
03 |
boolean found = false ; |
04 |
for (Activity activity : activityList){ |
05 |
if (activity.getClass().getSimpleName().equals(name)) |
10 |
while (!getCurrentActivity().getClass().getSimpleName().equals(name)) |
13 |
inst.sendKeyDownUpSync(KeyEvent.KEYCODE_BACK); |
14 |
} catch (SecurityException e){ |
15 |
Assert.assertTrue( "Activity named " + name + " can not be returned to" , false ); |
21 |
for ( int i = 0 ; i < activityList.size(); i++) |
22 |
Log.d( "Robotium" , "Activity priorly opened: " + activityList.get(i).getClass().getSimpleName()); |
23 |
Assert.assertTrue( "No Activity named " + name + " has been priorly opened" , false ); |
如果在一個(gè)非測試應(yīng)用的界面,做sendkey操作就會(huì)拋出SecurityException異常。
日志如下:
02-23 10:29:25.343: INFO/TestRunner(22963): junit.framework.AssertionFailedError: Can not complete action!
02-23 10:29:25.343: INFO/TestRunner(22963): at junit.framework.Assert.fail(Assert.java:47)
02-23 10:29:25.343: INFO/TestRunner(22963): at junit.framework.Assert.assertTrue(Assert.java:20)
02-23 10:29:25.343: INFO/TestRunner(22963): at com.jayway.android.robotium.solo.RobotiumUtils.sendKeyCode(RobotiumUtils.java:56)
02-23 10:29:25.343: INFO/TestRunner(22963): at com.jayway.android.robotium.solo.Solo.sendKey(Solo.java:1792)