【问题标题】:Using XCTAssertEqual in a Xcode UI test with react-native在带有 react-native 的 Xcode UI 测试中使用 XCTAssertEqual
【发布时间】:2016-08-31 10:56:12
【问题描述】:

我正在使用 Xcode UI 测试器测试一个应用程序。应用程序的每个页面都有一些标题文本。当测试访问一个页面时,我想使用XCTAssertEqual测试标题文本。

页面的 react-native 如下所示

  <View style={[AppStyles.appContainer, AppStyles.container]}>
    <NavigationBar
      title={<NavbarTitle title= {this.state.title} />}
      statusBar={{style: 'light-content', hidden: false}}
      style={AppStyles.navbar}
      tintColor={AppConfig.primaryColor}
      leftButton={leftButton} />
    <route.component navigator={navigator} route={route} {...route.passProps} onNavigationStateChange={this.onNavigationStateChange}  />
  </View>

NavBarTitle 组件的代码是

var NavbarTitle = React.createClass({
  render: function() {
    return (
      <Text style={[AppStyles.baseText, AppStyles.strong, AppStyles.navbarTitle]}>{this.props.title}</Text>
    );
  }
});

我想把断言写成这样

XCTAssertEqual(NavbarTitle.text, 'Sign In')

如何使用 XCTAssertEqual 找到 NavbarTitle.text?

【问题讨论】:

    标签: ios xcode react-native


    【解决方案1】:

    一种方法是只查找标题文本,而不是标题元素。以下代码等待文本出现,然后断言它确实存在。

      let exists = NSPredicate(format: "exists == true")
      let signIn = app.staticTexts[" Sign In"]
      expectationForPredicate(exists, evaluatedWithObject: signIn, handler: nil)
      waitForExpectationsWithTimeout(15, handler: nil)
      XCTAssert(signIn.exists)
    

    这可能会在页面上恰好显示“登录”的其他页面上产生误报,因此最好识别确切的元素。

    【讨论】:

      猜你喜欢
      • 2018-01-20
      • 1970-01-01
      • 2017-08-29
      • 2015-12-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-09-14
      • 2018-06-25
      相关资源
      最近更新 更多