【问题标题】:Detox - Enter on Numpad排毒 - 按数字键输入
【发布时间】:2018-05-25 18:28:20
【问题描述】:

我想知道如何使用本机键盘输入数字,然后使用“\n”在 Detox 上的普通字符串上像 typeText 一样输入数字

// await typeText('${screen_id}_screen_question_${question_id}_answer_input_', '\n');

我怎样才能用数字实现这一点?

每当我输入 typeText ('n') 时,它都会给我GREYKeyboard: No known SHIFT key was found in the hierarchy.

在我的假设中,因为小键盘键没有 Enter 键。但仍然不确定它为什么要寻找 Shift 键。

谢谢

【问题讨论】:

    标签: react-native detox


    【解决方案1】:

    使用 react-native 的 Keyboard 模块来隐藏键盘。

    和这个问题类似:detox-how-to-test-multiline-textinput

    例子:

    import {Keyboard} from 'react-native'
    
    import React, { Component } from 'react'
    import {
      AppRegistry,
      StyleSheet,
      Alert,
      TouchableWithoutFeedback,
      TouchableOpacity,
      View,
      Text,
      TextInput
    } from 'react-native'
    
    class example extends Component {
      constructor(props) {
        super(props)
      }
    
      render() {
        return (
          <TouchableWithoutFeedback 
            onPress={Keyboard.dismiss}
            style={styles.container}
          >
            <View style={styles.form}>
              <View style={styles.input}>
                <TextInput
                  testID='input'
                  style={styles.inputText}
                  keyboardType="numeric"
                />
              </View>
              <TouchableOpacity
                testID='next'
                style={styles.button}
                onPress={() => Alert.alert("Button pressed")}
              >
                <Text>Next</Text>
              </TouchableOpacity>
            </View>
          </TouchableWithoutFeedback>
        )
      }
    }
    
    const styles = StyleSheet.create({
      container: {
        flex: 1
      },
      form: {
        flex: 1,
        justifyContent: 'center',
        alignItems: 'center'
      },
      input: {
        height: 20,
        width: 200,
        borderColor: 'gray',
        borderWidth: 1
      },
      inputText: {
        flex: 1
      },
      button: {
        margin: 20,
        padding: 5,
        borderColor: 'gray',
        borderWidth: 1
      }
    })
    
    AppRegistry.registerComponent('example', () => example)
    

    测试:

    it('Hide num keyboard', async () => {
      const inputElement = element(by.id('input'));
      await expect(inputElement).toBeVisible();    
      await inputElement.typeText('1234567890');
      // click somewhere outside the input
      await inputElement.tapAtPoint({x: 0, y: -1});
      const buttonElement = element(by.id('next'));
      await expect(buttonElement).toBeVisible();
      await buttonElement.tap();
    });
    

    结果:

    【讨论】:

    • 这实际上是一个非常聪明的方法。最后,我最终在键盘上创建了一个完成按钮来完成按下完成以隐藏键盘。但这是一个合法的好方法。如果解决了问题,我会尝试标记为答案。谢谢@Antoni4
    • 太好了,不客气。当我需要测试多行文本输入时,这是唯一对我有用的方法,因为键盘隐藏了提交按钮并且 Detox 无法单击它。
    猜你喜欢
    • 2017-12-08
    • 2018-01-15
    • 1970-01-01
    • 2017-08-20
    • 2018-05-08
    • 2013-06-01
    • 2018-05-03
    • 2019-05-24
    • 2017-05-30
    相关资源
    最近更新 更多