如何测试移动应用程序如何前台化?

alt*_*alt 6 testing automated-tests react-native

我有一个create-react-native-app,我需要编写一个测试来防止用户将应用程序带回前台时问题的回归(似乎很难重新连接 socket.io 连接)。

目前我必须通过以下一些步骤手动测试:

  1. 打开应用程序
  2. 登录
  3. 等待登录完成
  4. 切换到另一个应用程序(后台)
  5. 等待后台应用程序失去其服务器连接(约 20 秒)
  6. 重新打开应用程序(前台)
  7. 检查是否出现问题

我该如何为此编写自动化测试?我jest在应用程序中用于单元测试。

Har*_*ani -1

基本用法

要查看当前状态,您可以检查 AppState.currentState,它将保持最新状态。但是,currentState 在启动时将为 null,而 AppState 通过桥检索它。

import React, {Component} from 'react'
import {AppState, Text} from 'react-native'

class AppStateExample extends Component {

  state = {
    appState: AppState.currentState
  }

  componentDidMount() {
    AppState.addEventListener('change', this._handleAppStateChange);
  }

  componentWillUnmount() {
    AppState.removeEventListener('change', this._handleAppStateChange);
  }

  _handleAppStateChange = (nextAppState) => {
    if (this.state.appState.match(/inactive|background/) && nextAppState === 'active') {
      console.log('App has come to the foreground!')
    }
    this.setState({appState: nextAppState});
  }

  render() {
    return (
      <Text>Current state is: {this.state.appState}</Text>
    );
  }

}
Run Code Online (Sandbox Code Playgroud)