'Trying to invoke a backdoor method in xamarin.uitests getting a failure

I have a xamarin.uitest project and an app project, added in main activity in the app project: MainLauncher = true and the method:

    [Export("MyInvokeMethod")]
    public void MyInvokeMethod()
    {
        Console.WriteLine("TEST");
    }

and in my project test added a test that only invoke the method:

    [Test]
    public void MyInvokeMethodTest()
    {
        _app.Invoke("MyInvokeMethod");
    }

and I got : System.Exception : Invoke for MyInvokeMethod failed with outcome: ERROR No such method found: MyInvokeMethod()

I did everything like here : https://docs.microsoft.com/en-us/appcenter/test-cloud/frameworks/uitest/features/backdoors

Tried also in iOS: in AppDelegate:

and in the test project:

[Test]
public void MyInvokeMethodTest()
{
    _app.Invoke("RefreshMethod:");
}

still nothing changed what am I doing wrong ?



Solution 1:[1]

Follow the steps below. I made the test with the code you provided. It works well on my side.

  1. Create the Xamarin.forms named UITest project and add the code below in MainActivity. It may be necessary to add a reference to the Mono.Android.Export.dll assembly in the Xamarin.Android project.

    public class MainActivity : global::Xamarin.Forms.Platform.Android.FormsAppCompatActivity
    {
       protected override void OnCreate(Bundle savedInstanceState)
         {
              ......
             LoadApplication(new App());
         }
     [Java.Interop.Export("MyInvokeMethod")]
     public void MyInvokeMethod()
     {
         Console.WriteLine("TEST");
     }
    }       
    
  2. Create the UITest project named UITest1 in the same solution. And then add reference with the UITest.Android.

  • In AppInitializer.cs, add ApkFile in StartApp method.

    public static IApp StartApp(Platform platform)
     {
         if (platform == Platform.Android)
         {
             return ConfigureApp.Android
                  .EnableLocalScreenshots()
                  //.InstalledApp("com.companyname.app2")
                  .ApkFile(@"....\UITest\UITest\UITest.Android\bin\Release\com.companyname.uitest.apk")
                  .StartApp();
         }
    
         return ConfigureApp.iOS.StartApp();
     }
    
  • Add the code below in Tests.cs.

    [Test]
      public void InvokeTest()
      {    
          // Invoke the backdoor method MainActivity.MyBackDoorMethod
          app.Invoke("MyInvokeMethod");
      }
    
  1. In Test Explorer, you could run the test.

enter image description here

Solution 2:[2]

Please check whether your Android App is deployed in its latest version to the emulator. Have in mind that it is only re-deployed automatically when you launch the App itself in VS.

I guess most of us write tests like:

  • Start writing a new UI Test in the UI Test project
  • Add a new exported backdoor method for that UI Test into the Android project
  • Invoke that method in the new UI Test
  • Launch the new UI Test

In the last step the UI Test project + Android project will be compiled but the Android project will NOT be re-deployed to the emulator, because this is only triggered when launching the Android project itself. So when the test now runs, it won't find the new backdoor method - because it's actually not yet there in the emulator. There is still the old version of the app.

To solve this, simply re-deploy the changed + compiled Android App manually to the emulator before starting the test (right click on the project + "Deploy")

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Wendy Zang - MSFT
Solution 2 Torben Schramme