【问题标题】:Detox _ Failed to type string because keyboard was not shown on screen排毒 _ 无法键入字符串,因为屏幕上未显示键盘
【发布时间】:2018-01-15 15:31:16
【问题描述】:

我正在尝试使用 detox 测试我的 react-native 应用程序,等待文本输入可见并在其中输入文本。我的 spec JS 文件如下所示:

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

  it('Login to a test account', async () => {
    // LoginPage: entering phone number moving to next page
    await expect(element(by.id('LoginPage-phoneInput'))).toBeVisible()
    await element(by.id('LoginPage-phoneInput')).typeText('<someNumber>')
  })
})

我得到的错误是:

FiestTest
    1) Enter phone number and tap on button


  0 passing (15s)
  1 failing

  1) FiestTest Enter phone number and tap on button:
     Error: An action failed. Please refer to the error trace below.
Exception with Action: {
  "Action Name" : "Type '6219'",
  "Element Matcher" : "(((respondsToSelector(accessibilityIdentifier) && accessibilityID('ValidatePage-txtField')) && !(kindOfClass('RCTScrollView'))) || (kindOfClass('UIScrollView') && ((kindOfClass('UIView') || respondsToSelector(accessibilityContainer)) && ancestorThatMatches(((respondsToSelector(accessibilityIdentifier) && accessibilityID('ValidatePage-txtField')) && kindOfClass('RCTScrollView'))))))"
}

Error Trace: [
  {
    "Description" : "Failed to type string '6219', because keyboard was not shown on screen.",
    "Error Domain" : "com.google.earlgrey.ElementInteractionErrorDomain",
    "Error Code" : "2",
    "File Name" : "GREYKeyboard.m",
    "Function Name" : "+[GREYKeyboard typeString:inFirstResponder:error:]",
    "Line" : "168"
  }
]

我正在使用以下工具:

$ npm outdate
Package                                  Current          Wanted         Latest  Location
apollo-client                              1.4.0           1.9.0          1.9.0  <appName>
babel-jest                                19.0.0          19.0.0         20.0.3  <appName>
babel-plugin-module-resolver               2.7.0           2.7.1          2.7.1  <appName>
eslint                                    3.19.0          3.19.0          4.4.1  <appName>
eslint-plugin-import                       2.2.0           2.7.0          2.7.0  <appName>
eslint-plugin-node                         4.2.2           4.2.3          5.1.1  <appName>
eslint-plugin-react                       6.10.3          6.10.3          7.1.0  <appName>
eslint-plugin-react-native                 2.3.2           2.3.2          3.0.1  <appName>
graphql-tag                                2.2.1           2.4.2          2.4.2  <appName>
jest                                      19.0.2          19.0.2         20.0.4  <appName>
moment-jalaali                             0.6.1           0.6.1          0.7.0  <appName>
native-base                                2.1.4           2.3.1          2.3.1  <appName>
react                             16.0.0-alpha.6  16.0.0-alpha.6         15.6.1  <appName>
react-apollo                               1.4.2          1.4.11         1.4.11  <appName>
react-native                              0.44.0          0.44.0         0.47.1  <appName>
react-native-adjust                       4.11.3          4.11.4         4.11.4  <appName>
react-native-fcm                           6.2.3           6.2.3          8.0.0  <appName>
react-native-popup-menu                    0.7.3           0.7.5          0.8.0  <appName>
react-native-router-flux                  3.39.1          3.41.0  4.0.0-beta.16  <appName>
react-native-sentry                       0.12.9         0.12.12         0.15.1  <appName>
react-native-smart-splash-screen           2.3.3           2.3.4          2.3.4  <appName>
react-test-renderer                       15.4.2          15.4.2         15.6.1  <appName>
redux                                      3.6.0           3.7.2          3.7.2  <appName>
redux-persist                              4.6.0           4.8.3          4.8.3  <appName>

这是我的 podfile:

# Uncomment this line to define a global platform for your project
# platform :ios, '9.0'

target '<appName>' do
  # Uncomment this line if you're using Swift or would like to use dynamic frameworks
  # use_frameworks!

  # Pods for <appName>

  pod 'Firebase'
  pod 'Firebase/Core'
  pod 'Firebase/Analytics'
  pod 'Firebase/Messaging'

  pod 'Yoga', :path => '../node_modules/react-native/ReactCommon/yoga'
  pod 'React', :path => '../node_modules/react-native', :subspecs => [
    'RCTText',
    'RCTImage',
    'RCTNetwork',
    'RCTWebSocket',
  ]

  #target '<appName>-tvOSTests' do
  #  inherit! :search_paths
    # Pods for testing
  #end

  #target '<appName>Tests' do
  #  inherit! :search_paths
    # Pods for testing
  #end

end

#target '<appName>-tvOS' do
  # Uncomment this line if you're using Swift or would like to use dynamic frameworks
  # use_frameworks!

  # Pods for <appName>-tvOS

#  target '<appName>-tvOSTests' do
#    inherit! :search_paths
    # Pods for testing
#  end

#end

现在我只是使用如下所示的睡眠命令,以便让我有时间手动插入字符串。但是,这不应该是一个永久的解决方案......

function sleep (ms) {
return new Promise(resolve => setTimeout(resolve, ms))
}

await sleep(milliseconds)
// what needs to happen after I manually enter the code

我也在 GitHub 上的 Detox 问题中发表了评论,但想知道是否有人有相同的经历并在这里找到了解决方案。

提前感谢您的帮助!

【问题讨论】:

    标签: javascript ios testing react-native detox


    【解决方案1】:

    你应该跑

    await element(by.id('LoginPage-phoneInput')).tap();
    

    之前

    await element(by.id('LoginPage-phoneInput')).typeText('<someNumber>')
    

    【讨论】:

    • 这不适用于我使用 iOS Simulator 10.0 (835.5) 但我可以使用 replaceText()
    【解决方案2】:

    请在此处结帐: https://github.com/wix/detox/blob/master/docs/APIRef.ActionsOnElement.md

    关于 typeText() 方法

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

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

    【讨论】:

    • @ϹοδεMεδιϲ 这是答案吗?
    • @ϹοδεMεδιϲ 这是一个答案,而不是一个问题
    • 噢! RTFT 给我!
    • 有没有办法在设备配置中设置这个设置,比如 device.launchApp({ delete: true, launchArgs: { ConnectHardwareKeyboard: false } })
    【解决方案3】:

    我尝试了此处列出的各种解决方案,但无济于事。

    对我来说始终如一的工作是:

    • 点击应用中的任何文本字段
    • 模拟器 > 硬件 > 键盘 > 切换软件键盘 (⌘K)

    之所以起作用,是因为它会在任何时候点击文本字段元素时自动切换内置键盘,这是调用 typeText 时的必备先决条件。

    我添加了pull request 来更新现已批准的文档。

    【讨论】:

      【解决方案4】:

      我无法让软件键盘始终如一地工作,但.replaceText() 对我来说确实工作得很好。 waitFor() 也非常有用。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2020-12-27
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-05-23
        • 2011-11-09
        相关资源
        最近更新 更多