Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
2.6k views
in Technique[技术] by (71.8m points)

c# - 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

what am I doing wrong ?


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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(@"....UITestUITestUITest.AndroidinReleasecom.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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...