Fet*_*mos 2 c# xamarin.android xamarin xamarin.forms xamarin-test-cloud
我用Xamarin Forms(Android)创建了应用程序.我打造了xamarin ui测试项目(Xamarin.UiTest = 1.3.7).我需要使用后门.这是我的代码:
public class MainActivity : FormsApplicationActivity
{
....
[Java.Interop.Export("Test")]
public void Test() { }
}
Run Code Online (Sandbox Code Playgroud)
它是单元测试中的调用方法
app.Invoke("Test");
Run Code Online (Sandbox Code Playgroud)
我得到这个例外:
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)
Run Code Online (Sandbox Code Playgroud)
对于Xamarin Android项目,它的代码是可行的.
如何使用xamarin ui测试的后门方法与xamarin表单项目?这是我在git上的测试项目.
作品在我们的优良Xamarin.Forms的解决方案,我会仔细检查要导出的方法MainActivity(这是一个唯一一个Xamarin.Forms基于Android的项目,您可以添加casbash后门到),并做了卡斯巴WaitForElement,以确保主活动之前运行在Backdoor调用发生.
Forms解决方案/项目进行快速测试.[[object CalabashRootView] > PhoneWindow$DecorView]
[ActionBarOverlayLayout] id: "decor_content_parent"
[FrameLayout > ... > LabelRenderer] id: "content"
[FormsTextView] text: "Welcome to Xamarin Forms!"
Run Code Online (Sandbox Code Playgroud)
MainActivity课堂上:[Activity (Label = "UITestBackDoorForms.Droid", Icon = "@drawable/icon", MainLauncher = true, ConfigurationChanges = ConfigChanges.ScreenSize | ConfigChanges.Orientation)]
public class MainActivity : global::Xamarin.Forms.Platform.Android.FormsApplicationActivity
Run Code Online (Sandbox Code Playgroud)
[Export("MyBackdoorMethod")]
public void MyBackdoorMethod()
{
System.Diagnostics.Debug.WriteLine("In through the backdoor - do some work");
}
Run Code Online (Sandbox Code Playgroud)
[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");
}
Run Code Online (Sandbox Code Playgroud)
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
Run Code Online (Sandbox Code Playgroud)
Xamarin Test Cloud Agent将尝试按以下顺序查找方法:
参考:https://developer.xamarin.com/guides/testcloud/uitest/working-with/backdoors/
Xamarin Test Cloud Agent将尝试按以下顺序查找方法:
[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");
}
Run Code Online (Sandbox Code Playgroud)
>>> 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"
Run Code Online (Sandbox Code Playgroud)
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");
}
Run Code Online (Sandbox Code Playgroud)
再次测试并成功,您的调试输出被写入logcat.
I/mono-stdout( 8641): In through the backdoor - do some work
Run Code Online (Sandbox Code Playgroud)