简体   繁体   中英

BackDoor for Xamarin Forms(Android)

I created app with Xamarin Forms(Android). I creaded xamarin ui test project( Xamarin.UiTest = 1.3.7). I need the use backdoor. It is my code:

public class MainActivity : FormsApplicationActivity
{  
       ....
  [Java.Interop.Export("Test")]
  public void Test()  { }
}

it is call method in unit test

app.Invoke("Test");

I get this exception:

 20-04-2016 12:02:36.805 +03:00 - 72182 - Error while performing Invoke("Test", null)
 Exception: System.Exception: Invoke for StartActivityTwo failed with outcome: ERROR
 No such method found: Test()
 in Xamarin.UITest.Android.AndroidGestures.Invoke(String methodName, Object[] arguments)
 in Xamarin.UITest.Utils.ErrorReporting.With[T](Func`1 func, Object[] args, String memberName)

For Xamarin Android project it code is work.

How to use a backdoor method on xamarin ui test with xamarin form project? It is my test project on git .

Works fine in our Xamarin.Forms solutions, I would double check that you are exporting the method in the MainActivity (which is the only one in a Xamarin.Forms based Android project that you can add casbash backdoors to) and doing a casbah WaitForElement to ensure that the main activity is running before the Backdoor invoke takes place.

A quick test using the Default/Template based Forms solution/project.

In Android (Xamarin.Forms` based) Project:

Repl Tree:

[[object CalabashRootView] > PhoneWindow$DecorView]
  [ActionBarOverlayLayout] id: "decor_content_parent"
    [FrameLayout > ... > LabelRenderer] id: "content"
      [FormsTextView] text: "Welcome to Xamarin Forms!"

Within the MainActivity class:

[Activity (Label = "UITestBackDoorForms.Droid", Icon = "@drawable/icon", MainLauncher = true, ConfigurationChanges = ConfigChanges.ScreenSize | ConfigChanges.Orientation)]
public class MainActivity : global::Xamarin.Forms.Platform.Android.FormsApplicationActivity

Backdoor Exported Method:

    [Export("MyBackdoorMethod")]
    public void MyBackdoorMethod()
    {
        System.Diagnostics.Debug.WriteLine("In through the backdoor - do some work");
    }

In Test Project:

[Test]
public void InvokeBackdoor()
{
    // Wait for the Activity to load
    app.WaitForElement(c => c.Marked("decor_content_parent"));

    // Invoke the backdoor method MainActivity.MyBackDoorMethod
    app.Invoke("MyBackdoorMethod");
}

LogCat Output:

I/System.out( 5754): params: {json={"query":"* marked:'decor_content_parent'","operation":{"method_name":"query","arguments":[]}}
I/System.out( 5754): }
~~~
I/System.out( 5754): URI: /backdoor
I/System.out( 5754): params: {json={"method_name":"MyBackdoorMethod","arguments":[]}
I/System.out( 5754): }
~~~
I/mono-stdout( 5754): In through the backdoor - do some work

The Xamarin Test Cloud Agent will try to locate the method in the following order:

Backdoors

Ref: https://developer.xamarin.com/guides/testcloud/uitest/working-with/backdoors/

The Xamarin Test Cloud Agent will try to locate the method in the following order:

  • The Android.App.Application subclass.
  • The current activity.
  • The context of the root view.

Update (User supplied code):

Test Code Before:

[Test]
public void AppLaunches()
{
    app.Repl();
    //app.Screenshot("First screen.");
    //Assert.IsTrue(true);
    app.WaitForElement(c => c.Marked("action_bar_overlay_layout"));
    app.Invoke("Test");
}

Repl Output:

>>> tree                                                                        
[[object CalabashRootView] > PhoneWindow$DecorView]                             
  [ActionBarOverlayLayout] id: "decor_content_parent"
    [FrameLayout > ... > Platform_DefaultRenderer] id: "content"
      [ButtonRenderer]
        [Button] text: "Test1"
      [ButtonRenderer]
        [Button] text: "Test2"
      [ButtonRenderer]
        [Button] text: "Test3"

Problem:

1) You are waiting for an element named "action_bar_overlay_layout", there is an Activity named "decor_content_parent" that you can wait for. I tend to use what is shown via the top level output of the Repl tree, it is easiest to match up and for others to follow along with.

2) You were trying to invoke a method exported as Test but in MainActivity.as it is flagged as [Export("MyBackdoorMethod")] .

Code Changes After:

[Test]
public void AppLaunches()
{
    app.Repl();
    app.WaitForElement(c => c.Marked("decor_content_parent"));
    app.Invoke("MyBackdoorMethod");
}

Ran test again and success, your debug output is written to logcat .

Logcat:

I/mono-stdout( 8641): In through the backdoor - do some work

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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