最近在用Robotium框架做自动化测试,遇到一个问题,就是sendKey()方法报错。
本质的原因是因为我测试的应用有一个bug,当我选择若干次goback()后应该退出到应用软件的主界面,但是却直接退出应用了。
我查了一下源代码:
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 ); |
如果在一个非测试应用的界面,做sendkey操作就会抛出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)