ios - 使用 Xamarin UITest 在 iOS 选择器中选择一个项目?

标签 ios picker xamarin.uitest

我们正在使用 UITest 测试我们的 Xamarin.Forms 应用,并且需要在 iOS 上选择一个 Picker 的值。

可以通过项目集合中的行号进行选择,如下所示:

app.Query(x => x.Class("UIPickerView").Invoke("selectRow", 1, "inComponent", 0, "animated", true))

我们在上面的示例中选择第 1 行。

但是,我们需要能够按值进行选择 - 例如,对于具有标题列表的选择器,要选择“Mrs”标题,我们需要执行以下操作:

app.Query(x => x.Class("UIPickerView").Invoke("selectValue", "Mrs", "inComponent", 0, "animated", true))

但是,UIPickerView 上没有可用的 selectValue(或类似)方法(引用 here)。

一个想法是获取项目中的行数,并遍历它们 - 但是当我尝试调用 getNumberOfRows 时,我收到以下错误:

app.Query(x => x.Class("UIPickerView").Invoke("numberOfRows", "inComponent", 0))

Error while performing Query([unknown]) Exception: System.Exception: Invoking an iOS selector requires either 0 or an uneven number of arguments (they have to match up pairwise including method name). at Xamarin.UITest.Queries.InvokeHelper.AppTypedSelector (Xamarin.UITest.Queries.AppQuery appQuery, Xamarin.UITest.Queries.ITokenContainer tokenContainer, System.Object[] queryParams, System.String methodName, System.Object[] arguments, System.Boolean explicitlyRequestedValue) [0x0011a] in <2a16c16730a54859bda72c6bc1c728f7>:0 at Xamarin.UITest.Queries.InvokeHelper.Invoke (Xamarin.UITest.Queries.AppQuery appQuery, System.String methodName, System.Object[] arguments) [0x00000] in <2a16c16730a54859bda72c6bc1c728f7>:0 at Xamarin.UITest.Queries.AppQuery.Invoke (System.String methodName, System.Object arg1, System.Object arg2) [0x00000] in <2a16c16730a54859bda72c6bc1c728f7>:0 at .m__0 (Xamarin.UITest.Queries.AppQuery x) [0x0000b] in <0de9804cff324d049415e25573e8da8a>:0 at Xamarin.UITest.SharedApp.Expand[T] (System.Func2[T,TResult] typedQuery) [0x0000c] in <2a16c16730a54859bda72c6bc1c728f7>:0 at Xamarin.UITest.iOS.iOSApp+<>c__DisplayClass17_01[T].b__0 () [0x00000] in <2a16c16730a54859bda72c6bc1c728f7>:0 at Xamarin.UITest.Utils.ErrorReporting.With[T] (System.Func`1[TResult] func, System.Object[] args, System.String memberName) [0x0000e] in <2a16c16730a54859bda72c6bc1c728f7>:0 Exception: Error while performing Query([unknown])

错误的原因很清楚 - 该方法需要一对特定的参数,但我看不出如何正确地制定查询。

关于如何按值而不是行号进行选择和/或如何获取项目数的任何想法/指示?

最佳答案

更好的方案是写一个UITest后门,然后在后门中找到Picker的Renderer,直接控制它。

解决方案的核心是由 Xamarin 论坛上的@LandLu 提供给我的:

您应该首先获取当前 View Controller 。然后迭代 subview 以获得您的选择器渲染器。 这是我为您提供的依赖服务:

[assembly: Dependency(typeof(FindViewClass))]
namespace Demo.iOS
{
    public class FindViewClass : IFindView
    {
        public void FindViewOfClass()
        {
            UIViewController currentViewController = topViewController();
            getView(currentViewController.View);
        }

        List<PickerRenderer> pickerList = new List<PickerRenderer>();
        void getView(UIView view)
        {
            if (view is PickerRenderer)
            {
                pickerList.Add((PickerRenderer)view);
            }
            else
            {
                foreach (UIView subView in view.Subviews)
                {
                    getView(subView);
                }               
            }
        }

        UIViewController topViewController()
        {
            return topViewControllerWithRootViewController(UIApplication.SharedApplication.KeyWindow.RootViewController);
        }

        UIViewController topViewControllerWithRootViewController(UIViewController rootViewController)
        {
            if (rootViewController is UITabBarController)
            {
                UITabBarController tabbarController = (UITabBarController)rootViewController;
                return topViewControllerWithRootViewController(tabbarController.SelectedViewController);
            }
            else if (rootViewController is UINavigationController)
            {
                UINavigationController navigationController = (UINavigationController)rootViewController;
                return topViewControllerWithRootViewController(navigationController.VisibleViewController);
            }
            else if (rootViewController.PresentedViewController != null)
            {
                UIViewController presentedViewController = rootViewController.PresentedViewController;
                return topViewControllerWithRootViewController(presentedViewController);
            }
            return rootViewController;
        }
    }
}

完成后,我可以通过 Renderer 的 Element 属性选择我想要的索引/项目。

编辑 我已经把它变成了 GitHub repository它实现了一组后门,使 iOS Picker 选择更容易

关于ios - 使用 Xamarin UITest 在 iOS 选择器中选择一个项目?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55490057/

相关文章:

ios - 多点连接发送和接收 NSManagedObject

ios - 在 iOS 10 上从 JPG 原始数据创建 UIImage 时获取 "JPEGDecompressSurface : Picture decode failed:"

azure - AppCenter 结果为 TesPlan。 Azure 开发运营

ios - "Done"MPMoviePlayerController 中的按钮在全屏模式下不起作用

ios - 从另一个应用程序以编程方式打开 iOS 设置应用程序中的键盘设置屏幕

c++ - Opengl 颜色选择器不给对象着色

SwiftUI 分段选取器在单击时不会切换

使用 SegmentedPickerStyle 的 Picker 内的 SwiftUI 自定义字体大小

android - Xamarin UITest 无法输入文本

c# - Xamarin.UITest:如何暂停测试步骤