簡體   English   中英

Xamarin Forms的BackDoor(Android)

[英]BackDoor for Xamarin Forms(Android)

我使用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測試中使用后門方法? 這是我在git上的測試項目

在我們的正常工作Xamarin.Forms的解決方案,我會仔細檢查,你是在出口的方法MainActivity (這在只有一個Xamarin.Forms基於Android的項目,您可以添加casbash后門到),並做了卡斯巴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”的活動。 我傾向於使用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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM