如何检查应用程序当前运行的屏幕?

时间:2015-10-01 17:41:49

标签: ios9 xcode-ui-testing

有没有办法直接检查XCUIApplication当前正在运行/在屏幕上可见的屏幕?

我希望能够XCTAssert应用程序当前正在显示屏幕'X'。我以为我可能只是为每个屏幕创建一个隐藏的UIElement或按钮,然后断言该元素存在于屏幕上。有没有人有更优雅或直接的方式来做这件事?

3 个答案:

答案 0 :(得分:1)

  1. 创建一个UI标签,以便能够引用当前屏幕。
  2. 设置该UI Label的accessibilityLabel,以便您可以通过XCUIElementQuery找到它。
  3. 设置该UI标签的accessibilityValue,以便您可以确定应用程序所在的状态/屏幕。
  4. 在应用程序代码中......     UILabel * currentScreen;

    String jrxmlFile = "\path\to\file\report.jrxml";
    String jasperDestination = "\path\to\file\report.jasper";
    JasperCompileManager.compileReportToFile(jrxmlFile, jasperDestination);
    

    在UI测试代码中......

    self.currentScreen.accessibilityLabel = @"Current Screen";
    
    <insert_code_to_change_state/screen_in_app>
    
    self.currentScreen.accessibilityValue = @"<insert_current_state/screen_of_app";
    

答案 1 :(得分:0)

我假设每个屏幕都有一些标题。在这种情况下,您可以使用以下方法。

//Get the top most navigation bar element
XCUIElement *currentNavigationBar = [app.navigationBars elementBoundByIndex:0];
XCTAssertEqualObjects(currentNavigationBar.identifier, @"Screen1 Name");

//Perform UI operations
XCTAssertEqualObjects(currentNavigationBar.identifier, @"Screen2 Name");

//Performa more UI operation
XCTAssertEqualObjects(currentNavigationBar.identifier, @"Screen3 Name");

所以,当你访问currentNavigationBar时,它会查询最新的。

答案 2 :(得分:0)

您可以像这样指定每个viewController的标题(如果它是导航控制器),

self.navigationItem.title = @"Your title";

然后在AppDelegate中创建一个用户定义的方法来查找visibleViewController,如此处所述,Get the top ViewController in iOS Swift 并用类似的东西读取视图控制器的标题,

NSString currentController = self.navigationController.visibleViewController.title;

您应该能够在UITest Target的appDelegate对象上调用此方法,并根据title值编写测试,

if currentController == "myViewControllerTitle" {
}
相关问题