【问题标题】:BackDoor for Xamarin Forms(Android)Xamarin 表单的后门(Android)
【发布时间】:2016-04-20 22:57:20
【问题描述】:

我使用 Xamarin Forms(Android) 创建了应用程序。我创建了 xamarin ui 测试项目(Xamarin.UiTest = 1.3.7)。我需要使用后门。这是我的代码:

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

是单元测试中的调用方法

app.Invoke("Test");

我得到了这个例外:

 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)

对于 Xamarin Android 项目,它的代码是有效的。

如何在 xamarin 表单项目的 xamarin ui 测试中使用后门方法?是my test project on git

【问题讨论】:

    标签: c# xamarin xamarin.android xamarin.forms xamarin-test-cloud


    【解决方案1】:

    在我们的Xamarin.Forms 解决方案中运行良好,我会仔细检查您是否正在导出MainActivity 中的方法(这是基于Xamarin.Forms 的Android 项目中唯一可以添加casbash 后门的方法)和执行 casbah WaitForElement 以确保在 Backdoor 调用发生之前主要活动正在运行。

    使用基于默认/模板的Forms 解决方案/项目进行快速测试。

    在 Android(基于 Xamarin.Forms`)项目中:

    复制树:

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

    MainActivity 类中:

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

    后门导出方式:

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

    在测试项目中:

    [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 输出:

    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
    

    Xamarin 测试云代理将尝试按以下顺序定位方法:

    后门

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

    Xamarin 测试云代理将尝试按以下顺序定位方法:

    • Android.App.Application 子类。
    • 当前活动。
    • 根视图的上下文。

    更新(用户提供的代码):

    之前的测试代码:

    [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");
    }
    

    复制输出:

    >>> 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"
    

    问题:

    1) 你在等待一个名为“action_bar_overlay_layout”的元素,有一个名为“decor_content_parent”的Activity,你可以等待。我倾向于使用通过 Repl 树的顶级输出显示的内容,它最容易匹配并且其他人也可以跟随。

    2) 您试图调用导出为Test 的方法,但在MainActivity.as 中它被标记为[Export("MyBackdoorMethod")]

    之后的代码更改:

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

    再次运行测试并成功,您的调试输出已写入logcat

    Logcat:

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

    【讨论】:

    • 感谢您的回答。但是调用对我不起作用。这是我的测试项目-github.com/FetFrumos/XamarinFormsUiTest。请告诉我是什么问题
    • @FetFrumos 我更新了我的答案,查看标题为“更新(用户提供的代码):”的部分,现在可以使用 ;-)
    • @FetFrumos 很高兴听到,快乐的 Xammie 编码
    猜你喜欢
    • 1970-01-01
    • 2022-07-29
    • 2017-05-19
    • 1970-01-01
    • 2018-04-18
    • 1970-01-01
    • 2021-01-16
    • 2019-01-23
    • 1970-01-01
    相关资源
    最近更新 更多