使用 detox 通过 ID 测试查找元素 - React Native

Posted

技术标签:

【中文标题】使用 detox 通过 ID 测试查找元素 - React Native【英文标题】:Find element by ID testing with detox - React Native 【发布时间】:2021-02-18 04:40:24 【问题描述】:

我正在尝试测试特定 ID 是否具有某些属性,但我没有成功找到它。相反,它会找到许多其他具有相同属性的 id,这告诉我它没有停止在我正在引导测试停止的 ID 中。 另外,我找不到明确的排毒测试指南

```it("is expected to have elements identifiable by testID", async () => 
await expect(element(by.id("assignment-36"))).toBeVisible();
await expect(element(by.label("Build a Web page"))).toBeVisible();
await expect(element(by.label("Points: 320"))).toBeVisible();````

但我得到的错误是:


    Test Failed: Multiple elements found for “MATCHER(label == “Points: 320”)”
    TIP: To print view hierarchy on failed actions/matches, use log-level verbose or higher.```

【问题讨论】:

【参考方案1】:

从错误消息中可以看出,您有多个元素可见,标签为Points: 320,因此您需要在测试中更具体地确定您希望哪个元素可见。

最好的方法是将 testID 分配给元素以唯一标识它们 https://reactnative.dev/docs/view 或者您可以使用 atIndex 选择元素的第 n 次出现 - https://github.com/wix/Detox/blob/master/docs/APIRef.Matchers.md#atindexindex

【讨论】:

以上是关于使用 detox 通过 ID 测试查找元素 - React Native的主要内容,如果未能解决你的问题,请参考以下文章