【问题标题】:Detox: iOS Simulator how to confirm alert message排毒:iOS 模拟器如何确认警报信息
【发布时间】:2018-05-11 08:23:41
【问题描述】:

我正在使用来自 react-native 的警报。

如何排毒以按警报消息上的“注销”按钮?

我尝试使用await element(by.text('Log out')).tap();

但我收到“匹配多个元素”错误。大概它会找到 3 个具有相同标签的元素。带有“注销”标签的原始按钮用于触发警报消息、警报消息标题和我要排毒的警报消息按钮。

Error Trace: [
  {
    "Description" : "Multiple elements were matched: (
    "<UILabel:0x7fe7964db910; AX=Y; AX.label='Log out'; AX.frame={{41, 234}, {238, 20.5}}; AX.activationPoint={160, 244.25}; AX.traits='UIAccessibilityTraitStaticText'; AX.focused='N'; frame={{16, 20}, {238, 20.5}}; opaque; alpha=1; UIE=N; text='Log out'>",
    "<UILabel:0x7fe7964dda90; AX=Y; AX.label='Log out'; AX.frame={{198.5, 322.5}, {58, 20.5}}; AX.activationPoint={227.5, 332.75}; AX.traits='UIAccessibilityTraitStaticText'; AX.focused='N'; frame={{0, 12}, {58, 20.5}}; opaque; alpha=1; UIE=N; text='Log out'>",
    "<RCTText:0x7fe79652f300; AX=Y; AX.label='Log out'; AX.frame={{16, 338.5}, {288, 17}}; AX.activationPoint={160, 347}; AX.traits='UIAccessibilityTraitStaticText'; AX.focused='N'; frame={{0, 0}, {288, 17}}; alpha=1>"
). Please use selection matchers to narrow the selection down to single element.",
    "Error Domain" : "com.google.earlgrey.ElementInteractionErrorDomain",
    "Error Code" : "5",
    "File Name" : "GREYElementInteraction.m",
    "Function Name" : "-[GREYElementInteraction grey_errorForMultipleMatchingElements:withMatchedElementsIndexOutOfBounds:]",
    "Line" : "956"
  }
]

我想一种方法是使用.atIndex(),但这意味着每次发生变化时我都需要使用索引来确定正确的元素。

有没有更好的方法来解决这个问题?

谢谢。

【问题讨论】:

    标签: ios react-native ios-simulator detox


    【解决方案1】:

    经过一番修改后,我最终使用了这个:

    await element(by.label('Log out').and(by.type('_UIAlertControllerActionView'))).tap();
    

    不确定这是否适用于每个 iOS 版本,但似乎适用于 10.3 和 11.1

    使用 Xcode 提供的 View Hierarchy Debugger 查看不同版本的 iOS 的类型是否发生了变化。

    【讨论】:

    • 伟大的发现!但是你是怎么发现它是_UIAlertControllerActionView ?如果我们需要检查例如照片库并点击图像并点击Choose 按钮怎么办?
    • 嗨,我相信我使用了 Xcode 中的Debug View Hierarchy
    • 对系统打开的警告框有效吗?比如请求用户的许可。
    • @arjun 它似乎不适用于系统打开的警报框。我尝试使用此解决方案,将 'Log out' 替换为 'Allow'(用于推送通知权限),但 Detox 仍以 Error: Error: Cannot find UI element. 退出
    • I enabled the permission in the detox init.js file 并且警报不再显示,但正如 Leo Natan--reinstate Monica 中所解释的那样,警报是从与 Detox 运行的进程和 it is not possible to interact with it 不同的进程中显示的。似乎discussion on this issue is limited to SO
    【解决方案2】:

    它应该可以通过文本查找元素

    await element(by.text('Log out')).tap();
    

    演示仓库:https://github.com/FDiskas/demonas/blob/c703840a991b2f3d96a18ac8c5120ee1d5f901f8/e2e/firstTest.spec.ts#L11

    【讨论】:

    【解决方案3】:

    您现在可以按原生对话框。在 iOS 上测试。 (未在 Android 上测试)

    如果您的按钮显示“确定”,即:

    Alert.alert(
      `Are you sure you would like to remove this image as the coming soon image?`,
      undefined,
      [
        {
          text: "No",
          style: "cancel",
        },
        {
          text: "OK",
          style: "destructive",
          onPress: this.onRemoveHero,
        },
      ]
    );
    

    您可以通过以下方式点击它:

    element(by.label("OK")).atIndex(0).tap();
    

    【讨论】:

    • 这在 detox 17.0.2 中运行良好,它破坏了一些定位元素的方式,如 RN 中的警报(您不能再使用简单的by.text('OK') 定位警报按钮
    • @Dantereve 你能澄清一下,by.text('OK') 曾经工作过吗?但现在唯一的方法是使用by.label('OK')?
    • 是的,by.text('OK') 与 Detox 16.x 一起使用,但在我目前正在使用 Detox 测试的两个应用程序上不再使用 Detox 17。解决方法是使用element(by.label("OK")).atIndex(0)。这肯定与此有关:Detox Issue 2156
    【解决方案4】:

    我写了一个实用函数,让你可以跨平台。

    功能

    /**
     * Detects a systme dialog button by label
     * 
     * @param {string} label
     * 
     * @returns {*}
     */
    export function systemDialog(label){
        if (device.getPlatform() === 'ios') {
            return element(by.label(label)).atIndex(0);
        }
    
        return element(by.text(label));
    }
    

    用法

    import { systemDialog } from "path to system dialog";
    
    ...
    
    await systemDialog('OK').tap();
    

    【讨论】:

      猜你喜欢
      • 2018-12-10
      • 2018-05-08
      • 1970-01-01
      • 1970-01-01
      • 2018-06-17
      • 2019-04-02
      • 2023-03-03
      • 1970-01-01
      • 2018-10-10
      相关资源
      最近更新 更多