我有一个应用程序,我正在尝试将Xamarin UI测试放在上面。我需要后门的应用程序,以绕过我的登录过程。我的后门方法很好。
[Activity(Label = "AppName", Icon = "@drawable/icon", Theme = "@style/Theme.Splash", MainLauncher = true, NoHistory = true)]
public class SplashActivity : Activity
{
protected override void OnCreate(Bundle savedInstanceState)
{
base.OnCreate(savedInstanceState);
StartActivity(typeof(MainActivity));
}
[Java.Interop.Export("BackDoor")]
public void BackDoor()
{
var myActivity = {Magic code to get reference to the the instance of MainActivity goes here}
}
}然而,它在我的飞溅屏幕上的触发,我需要它得到一个参考我的实际MainActivity,而不是我的SplashActivity。如何在我的MainActivity方法中获得对BackDoor的引用?
Xamarin后门文档:https://developer.xamarin.com/recipes/testcloud/start-activity-with-backdoor/ https://developer.xamarin.com/guides/testcloud/uitest/working-with/backdoors/
发布于 2017-01-13 06:59:58
如何检索当前活动
要检索MainActivity,可以使用@JamesMontemagno的CurrentActivityPlugin。
将当前活动NuGet包添加到您的Xamarin.Android项目中,然后,在Xamarin.Android项目中,您可以使用下面的代码来检索当前活动并检查它是否是MainActivity。
Activity currentActivity = Plugin.CurrentActivity.CrossCurrentActivity.Current.Activity as MainActivity;
if (!(currentActivity is MainActivity))
throw new System.Exception("Current Activity is not MainActivity");这个插件是GitHub上的开源。
发布于 2017-01-05 02:06:03
根据Android后门方法指南,它不能返回object类型,只能返回string、Java.Lang.String或void。请参阅:https://developer.xamarin.com/guides/testcloud/uitest/working-with/backdoors/
你不想像在指南中那样从后门开始下一个活动吗?如果是这样的话,就按照你链接得更紧密的指南吧。
此外,在使用object进行构建时,只需对BackDoor方法进行双重检查并返回NullReferenceException即可。但是,对于"{Magic获取对MainActivity实例的引用“,您可以这样做:
ActivityManager am = (ActivityManager)this.GetSystemService(Context.ActivityService);
var myActivity = am.GetRunningTasks(1)[0].TopActivity;myActivity将是对最高级活动的引用,但无论如何不能从BackDoor方法返回它。当然,您可以返回字符串描述。我不知道您为什么需要在测试代码中引用活动,因为在测试代码中没有太多的工作可以做。
https://stackoverflow.com/questions/41470478
复制相似问题