简体   繁体   English

我如何模拟后退按钮的单击

[英]How can i simulate a Back-Button click

so i was working with Robotium to automate tests and i'm having a little problem. 所以我和Robotium一起使测试自动化,但我遇到了一个小问题。 Robotium is able to "sendkey(keyevent.keycode_back)" so i can return from a view to the previous but now there is another (android native) activity which opens when i click on "forgotten password" (hyperlink) and i would require android itself to simulate a back button click. Robotium能够“发送密钥(keyevent.keycode_back)”,因此我可以从视图返回到上一个,但是现在有另一个(android native)活动会在我单击“忘记密码”(超链接)时打开,并且我需要android本身以模拟后退按钮的单击。 is there a way to do so? 有办法吗?

As for the code i'm working on: 至于我正在处理的代码:

@FlakyTest(tolerance = 2)
public void testperformMsgListItemandItemSelectTestAfterStartInLandscape_ShouldSucceed() throws Exception{
    try {

        mSolo.clickOnText("Forgotten Password?");
        assertFalse(mSolo.getCurrentActivity().hasWindowFocus());
        mSolo.sendKey(KeyEvent.KEYCODE_HOME);
        assertTrue(mSolo.getCurrentActivity().hasWindowFocus());
        mSolo.clickOnText("Register Now!");
        assertFalse(mSolo.getCurrentActivity().hasWindowFocus());

    } catch (AssertionError err) {
        mSolo.fail(getName(), err);
        throw err;
    }
}

Create a KeyEvent and dispatch it. 创建一个KeyEvent并调度它。

For example: 例如:

KeyEvent kdown = new KeyEvent(KeyEvent.ACTION_DOWN, KeyEvent.KEYCODE_BACK);
Activity.dispatchKeyEvent(kdown);

an alternative method is to just call finish() in your activity. 另一种方法是在您的活动中仅调用finish() That disposes of the current activity and takes you back to the previous activity, which is exactly what the Android OS does when you click the back button. 这样就处理了当前活动,并带您回到上一个活动,这正是您单击后退按钮时Android OS所做的。

If the current activity belongs to the app you're testing, use 如果当前活动属于您正在测试的应用,请使用

mSolo.goBack();

Robotium cannot perform any actions if the current activity belongs to another app, so to write useful tests you can't ever let your tests launch other apps. 如果当前活动属于另一个应用程序,Robotium将无法执行任何操作,因此要编写有用的测试,就不能让您的测试启动其他应用程序。

if >=API LEVEL5: 如果> = API LEVEL5:

   super.onBackPressed()

else 其他

    KeyEvent kdown = new KeyEvent(KeyEvent.ACTION_DOWN, KeyEvent.KEYCODE_BACK);
    Activity.dispatchKeyEvent(kdown);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM