Is there a method to close and Reconnect an app in

2019-06-09 06:14发布

问题:

Is there any way to close and reconnect to my app in Xamarin UITest?

In my test case I want close and reconnect my Android app.

回答1:

Depending upon what you mean by "close", but here are a couple of ways...

In the MainActivty of your Xamarin.Forms Android app, add some UITest backdoors:

[Export("HardExitBackdoor")]
public void HardExitBackdoor()
{
    Log.Info("UITest", "Finish");
    Finish();
}

[Export("BackgroundBackdoor")]
public void BackgroundBackdoor()
{
    Log.Info("UITest", "MoveTaskToBack");
    MoveTaskToBack(true);
}

Now in your test, you can "exit" your app and "restart" it:

[Test]
public void HardRestartApp()
{
    app.Invoke("HardExitBackdoor");
    BeforeEachTest();
    WelcomeTextIsDisplayed();
}

[Test]
public void BackgroundedApp()
{
    app.Invoke("BackgroundBackdoor");
    BeforeEachTest();
    WelcomeTextIsDisplayed();
}

[Test]
public void SoftRestartApp()
{
    for (var i = 0; i < 10; i++)
        app.Back();
    BeforeEachTest();
    WelcomeTextIsDisplayed();
}

Update:

Android/Java and Calabash/Ruby

public class MainActivity extends Activity {
    ~~~~
    public void HardExitBackdoor() {
        Log.w(TAG, "finish");
        finish();
    }
    ~~~~
}

To invoke this method in Ruby/calabash-android:

backdoor "HardExitBackdoor"