【问题标题】:Detox Substring Match Using by.text使用 by.text 进行排毒子字符串匹配
【发布时间】:2021-07-22 01:16:23
【问题描述】:

我正在尝试按文本选择一个元素,但实际文本会发生变化,因为它包含当前时间戳。有没有办法在不对 getAttributes() 进行子字符串搜索的情况下选择它?我不能使用 getAttributes,因为我们还将在 android 上进行测试。

我正在使用的选择:

await expect(element(by.text("James has initiated the chatbot: ShallowFaqChatbot"))).toBeVisible();

元素:

  {
      hittable: true,
      activationPoint: { x: 153.5, y: 14.75 },
      normalizedActivationPoint: { x: 0.5, y: 0.5 },
      elementFrame: { y: 9, x: 9, width: 307, height: 29.5 },
      enabled: true,
      elementBounds: { y: 0, x: 0, width: 307, height: 29.5 },
      layer: '<CALayer: 0x600002b907c0>',
      safeAreaInsets: { right: 0, top: 0, left: 0, bottom: 0 },
      visible: true,
      elementSafeBounds: { y: 0, x: 0, width: 307, height: 29.5 },
      label: 'James has initiated the chatbot: ShallowFaqChatbot - 11:08 am',
      className: 'RCTTextView',
      frame: { y: 473, x: 34, width: 307, height: 29.5 },
      text: 'James has initiated the chatbot: ShallowFaqChatbot - 11:08 am'
  }

您可以看到它具有我要查找的文本的属性文本,但是由于“- 11:08 am”,detox 无法抓取它。

【问题讨论】:

    标签: xcode react-native testing jestjs detox


    【解决方案1】:

    很遗憾,Detox 不支持正则表达式匹配器:https://github.com/wix/Detox/issues/1441

    这里最简单的解决方法是将testID 添加到元素并在其上进行匹配。

    await expect(element(by.id("James has initiated the chatbot: ShallowFaqChatbot"))).toBeVisible();
    

    您无需使用.and 来进行此匹配。这不是一个理想的解决方案,但由于 testID 会被隐藏起来,这应该是适合您的情况的有效解决方案。

    【讨论】:

      【解决方案2】:

      您可以尝试使用detox "and" matcher

      为此,您必须设置元素的testID。 现在,假设您的元素的testIDsuper_text_field

      如果您想检查此元素是否包含“某物”之类的特定文本,您可以使用以下解决方法:

      await expect(element(by.id('super_text_field').and(by.text('something')))).toBeVisible()
      

      当然,这种解决方法有其局限性和假设,但我希望它适用于您的情况!

      【讨论】:

      • 问题是 by.text 仍然会进行完全匹配,因此使用带有 by.id 元素的 'and' 匹配器无济于事。
      • 不幸的是,我认为 detox 无法进行模式匹配:( 相反,您可以尝试以下解决方法:(1) 您可以将组件重构为两个较小的组件,然后提取这个“时间”部分进入第二个。确保第一个有testID。然后你可以检查第一个是否有你想要的文本。(2)尽量确保“时间”部分是固定的。我我很确定您可以控制它,因此请尝试确保将其设置为“21:37”。然后您将能够进行全文匹配
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2023-03-06
      • 1970-01-01
      • 1970-01-01
      • 2013-03-22
      • 1970-01-01
      • 2023-03-23
      • 2020-10-16
      相关资源
      最近更新 更多