排毒文本输入不写文本

Detox textInput not writing text

我在我的 react-native 项目上使用 Detox 并想在登录屏幕上输入一个名称,但 detox 无法识别 textInput。 这是我的文本代码

describe('SCA', () => {
  beforeEach(async () => {
    await device.reloadReactNative();
  });

  it('should have splash screen', async () => {
    await expect(element(by.id('splash'))).toBeVisible();
  });
  it('should show login screen', async () => {
    await waitFor(element(by.id('login'))).toBeVisible();
  });
  it('test login screen name input', async () => {
    await element(by.id('name')).typeText('Liam')
  });
});

文本输入代码:

<TextInput
        testID="name"
        style={styles.input}
        onChangeText={value => this.setState({ name: value }) }
        placeholder={'Name ... '}
        placeholderTextColor='white'
        value={name} />

这是我遇到的错误:

 ● SCA › test login screen name input

    Failed: [Error: Error: Cannot find UI element.
    Exception with Action: {
      "Action Name":  "Type 'Liam'",
      "Element Matcher":  "((!(kindOfClass('RCTScrollView')) && (respondsToSelector(accessibilityIdentifier) && accessibilityID('name'))) || (((kindOfClass('UIView') || respondsToSelector(accessibilityContainer)) && parentThatMatches(kindOfClass('RCTScrollView'))) && ((kindOfClass('UIView') || respondsToSelector(accessibilityContainer)) && parentThatMatches((respondsToSelector(accessibilityIdentifier) && accessibilityID('name'))))))",
      "Recovery Suggestion":  "Check if the element exists in the UI hierarchy printed below. If it exists, adjust the matcher so that it accurately matches element."
    }


    Error Trace: [
      {
        "Description":  "Interaction cannot continue because the desired element was not found.",
        "Error Domain":  "com.google.earlgrey.ElementInteractionErrorDomain",
        "Error Code":  "0",
        "File Name":  "GREYElementInteraction.m",
        "Function Name":  "-[GREYElementInteraction matchedElementsWithTimeout:error:]",
        "Line":  "124"
      }
    ]

您目前正在每次测试之间重新加载设备。

beforeEach(async () => {
  await device.reloadReactNative();
});

我认为这不是您想要做的,因为它会重置所有内容,这意味着您必须等待所有内容重新加载并在屏幕上移动,您将面临与之前相同的问题 (NB your usage of waitFor is incorrect, see my answer on your previous post or re-read the documentation)

您可以在 documentation 中阅读有关 .typeText 的更多信息。

使用.typeText时的一个常见错误是没有断开硬件键盘

Note: Make sure hardware keyboard is disconnected. Otherwise, Detox may fail when attempting to type text.

To make sure hardware keyboard is disconnected, open the simulator from Xcode and make sure Hardware -> Keyboard -> Connect Hardware Keyboard is deselected (or press ⇧⌘K).

我在使用 .typeText 时经常做的一件事是确保该元素存在

所以我会在您使用 .typeText 之前添加以下内容以确保它可见。

await expect(element(by.id('name'))).toBeVisible();