【问题标题】:Detox textInput not writing text排毒 textInput 不写文本
【发布时间】:2019-07-19 11:21:32
【问题描述】:

我在我的 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"
      }
    ]

【问题讨论】:

    标签: javascript ios react-native jestjs detox


    【解决方案1】:

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

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

    我认为这不是您想要做的,因为它会重置所有内容,这意味着您必须等待所有内容重新加载并在屏幕上移动,您将面临与之前的 post 相同的问题(注意您对waitFor 的使用不正确,请参阅我在您之前的帖子中的回答或重新阅读documentation)

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

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

    注意:确保硬件键盘已断开连接。否则,Detox 可能会在尝试输入文本时失败。

    要确保硬件键盘已断开连接,请从 Xcode 打开模拟器并确保取消选择硬件 -> 键盘 -> 连接硬件键盘(或按⇧⌘K)。

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

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

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

    【讨论】:

    • 是的,这行得通,更改了键盘设置并将其保留在 reloadReactNative() 中,谢谢
    猜你喜欢
    • 2018-05-13
    • 2019-04-02
    • 1970-01-01
    • 1970-01-01
    • 2020-03-16
    • 2020-02-20
    • 2019-11-11
    • 2018-08-26
    • 2020-03-04
    相关资源
    最近更新 更多