【问题标题】:iOS/UI Automation: UIAActionSheet does not have possibilities to manipulate with buttonsiOS/UI 自动化:UIAActionSheet 无法使用按钮进行操作
【发布时间】:2011-07-12 03:13:53
【问题描述】:

我的问题与 XCode 的 Instruments 工具中的 UI 自动化模板有关。 UI 自动化如何支持 UIActionSheet 测试?我知道有一个 UIAActionSheet 元素,我能够在我的应用程序中获得它。但我不知道如何从操作表中获取和操作按钮。 UI 自动化不为这些按钮提供任何元素。 UI 自动化文档也没有关于此事的任何信息。请参阅下面的链接。看起来这个控件没有为按钮使用 UIButton 类,而是以某种特定的方式呈现它们。你能给我一些线索如何从 UIAActionSheet 访问按钮吗?谢谢。

http://developer.apple.com/library/ios/#documentation/ToolsLanguages/Reference/UIAActionSheetClassReference/UIAActionSheet/UIAActionSheet.html#//apple_ref/doc/uid/TP40009895

【问题讨论】:

    标签: ios instruments ui-automation ios-ui-automation


    【解决方案1】:

    如果你想像我一样保持井井有条:

    function getTarget() {
        return UIATarget.localTarget();
    }
    
    function getApp() {
        return getTarget().frontMostApp();
    }
    
    function getWindow() {
        return getApp().mainWindow();
    }
    
    function tapActionSheetButtonInIndex(index) {
        var actionSheet = getApp().actionSheet() || getWindow().popover().actionSheet();
        actionSheet.buttons()[index].tap(); 
    }
    

    【讨论】:

      【解决方案2】:

      由于我的操作表位于弹出框中,因此它返回一个 nil 元素:

      UIATarget.localTarget().frontMostApp().actionSheet();
      

      这确实会返回操作表:

      UIATarget.localTarget().frontMostApp().mainWindow().popover().actionSheet();
      

      但它有零元素()和按钮()。

      所以我使用 MikhailV 的功能来点击按钮。

      var target = UIATarget.localTarget();
      var actionSheet = UIATarget.localTarget().frontMostApp().mainWindow().popover().actionSheet();
      var buttonIndex = 1;
      
      tapActionSheetButton(target, actionSheet, buttonIndex);
      

      【讨论】:

        【解决方案3】:

        我找到了使用直接攻丝模拟的解决方案。这不是一个很好的解决方案,但至少它有效。我在 Apple Developer Forum 中问过同样的问题,但没有得到任何回应。

        所以,下面的函数是我的解决方案。我已经对其进行了测试,并且表现良好。我仍然会继续寻找更好的方法。

        function tapActionSheetButton(target, actionSheet, buttonIndex)
        {
            var headerHeight = 28;
            var buttonHeight = 50;
        
            if (buttonIndex >= 0) 
            {
                var actionSheetRect = actionSheet.rect();
                UIALogger.logMessage("actionSheet:{rect:{origin:{x:" + actionSheetRect.origin.x.toString() 
                                     + ", y:" + actionSheetRect.origin.y.toString()
                                     + "}, size:{width:" + actionSheetRect.size.width.toString()
                                     + ", height:" + actionSheetRect.size.height.toString()
                                     + "}}}");
                var xOffset = actionSheetRect.size.width / 2;
                var yOffset = headerHeight + buttonIndex * buttonHeight + buttonHeight / 2;
                if (yOffset < actionSheetRect.size.height) {
                    var tap_x = actionSheetRect.origin.x + xOffset;
                    var tap_y = actionSheetRect.origin.y + yOffset;
                    target.tap({x:tap_x, y:tap_y});
                }
            }
            else
            {
                var message = "Cannot tap button " + buttonIndex.toString() + ". It does not exist";
                throw message;
            }
        }
        

        【讨论】:

          【解决方案4】:

          如您所见,有一种方法可以为每个操作表返回默认按钮,即取消按钮。由于操作表没有其他“默认”按钮,您需要自己实现这些按钮。
          值得苹果参考的一件事是它跳过了从父类继承的给定类的方法。 UIAActionSheet(像大多数 UIA 元素一样)从 UIAElement 类继承所有方法。检查this reference。您应该能够通过调用从操作表中获取所有按钮的数组

          var arrButtons = objActionSheet.buttons();
          

          然后您可以通过名称属性检查您需要哪个属性(再次来自 UIAElement name() 的方法)。

          如果您检查数组中您感兴趣的元素,您可以使用 UIAElement 方法 elements() 直接调用该按钮(假设您不会更改 ActionSheet)。
          例如,如果您想点击 ActionSheet 树中的第二个按钮,您只需调用

          UIATarget.localTarget().frontMostApp().actionSheet().elements()[1].tap();
          

          也许你可以跳过 localTarget(),不确定,但上面的代码应该可以工作。

          【讨论】:

          • 感谢您的回复。是的。我确实理解 UI 自动化的想法。问题是我在 UIActionSheet 实例中声明了几个按钮。我也尝试获取 actionSheet.buttons() 但它返回一个空数组。
          • 设置 isAccessibilityElement 也无济于事。 _myActionSheet.isAccessibilityElement = 是; _myActionSheet.accessibilityLabel = @"myActionSheet" 虽然脚本显示了 actionSheet 的正确名称 actionSheet.name() == "myActionSheet"
          • 嗯......如果数组为空,这很奇怪。 elements() 方法是否也返回空数组?当应用程序调用 AvtionSheet 时,您是否实际看到了按钮?
          • elements() 方法也返回一个空数组。我认为这是 UIActionSheet 类的特定实现。它内部不使用 UIButton 实例,直接绘制按钮。我已尝试创建自己的 UIActionSheetExt 类来支持 UIAActionSheet,但它在 UIActionSheet 中不可用。
          • 现在你现在 :) 做事 Apple 认可的方式,否则不支持 :P 考虑使用标准的实现控制的方式,或使用其他自动化方式cocoawithlove.com/2009/12/…
          猜你喜欢
          • 2017-01-31
          • 2016-01-02
          • 2016-11-26
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2012-09-17
          • 1970-01-01
          • 2020-08-25
          相关资源
          最近更新 更多