通过使用排毒进行ID测试查找元素-React Native

问题描述

我正在测试一个特定的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.```

解决方法

从错误消息中看起来,您似乎有多个带有标签Points: 320的可见元素,因此您需要在测试中更加明确地指出希望看到的元素。

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