【问题标题】:Why can't I access the Back button in my iPhone application using a UI Automation script?为什么我无法使用 UI 自动化脚本访问 iPhone 应用程序中的后退按钮?
【发布时间】:2011-04-21 19:24:48
【问题描述】:

我有一个简单的函数应该检查视图是否在主界面,如果不是,将其带到主界面:

function returnHome() {
    if (UIATarget.localTarget().frontMostApp().navigationBar().name() == mainTitle) return true;

    // set tab bar to calculations
    UIALogger.logMessage("Set tab bar to main.");
    if (UIATarget.localTarget().frontMostApp().tabBar().selectedButton().name() != mainTabName) {
        UIATarget.localTarget().frontMostApp().tabBar().buttons()[mainTabName].tap();
    }

    // go back to the home
    UIALogger.logMessage("Go back to home.");
    var backButton = UIATarget.localTarget().frontMostApp().mainWindow().buttons()["Back"];
    if (backButton.isValid()) {
        backButton.tap();
    } else {
        UIALogger.logError("Could not find 'Back' button!");
    }
}

现在,我无法通过错误“找不到‘返回’按钮!”。它就在上面,在左角,正盯着我,嘲弄我!

UIATarget.localTarget().frontMostApp().logElementTree() 给了我以下树:

2) UIAApplication [name:MyApplication value:(null) rect:{{x:0, y:20}, {width:320, height:460}}]
3) UIAWindow [name:(null) value:(null) rect:{{x:0, y:0}, {width:320, height:480}}]
4) UIAImage [name:(null) value:(null) rect:{{x:0, y:108}, {width:320, height:367}}]
4) UIANavigationBar [name:(null) value:(null) rect:{{x:0, y:64}, {width:320, height:44}}]
5) UIASegmentedControl [name:(null) value:(null) rect:{{x:5, y:71}, {width:310, height:30}}]
6) UIAButton [name:SementedControllerTagC, 3 of 3 value:(null) rect:{{x:212, y:71}, {width:103, height:30}}]
6) UIAButton [name:SementedControllerTagB, 2 of 3 value:(null) rect:{{x:109, y:71}, {width:102, height:30}}]
6) UIAButton [name:SementedControllerTagA, 1 of 3 value:1 rect:{{x:5, y:71}, {width:103, height:30}}]
4) UIATableView [name:Empty list value:rows 1 to 2 of 3 rect:{{x:15, y:169}, {width:290, height:247}}]
5) UIAButton [name:XYZ value:(null) rect:{{x:20, y:121}, {width:280, height:46}}]
5) UIATableGroup [name:SomeText value:(null) rect:{{x:15, y:169}, {width:290, height:50}}]
6) UIAStaticText [name:SomeText value:(null) rect:{{x:25, y:197}, {width:270, height:22}}]
5) UIATableCell [name:A value:(null) rect:{{x:15, y:219}, {width:290, height:50}}]
6) UIAElement [name:B value:(null) rect:{{x:15, y:219}, {width:247, height:50}}]
6) UIAButton [name:more info, A value:(null) rect:{{x:262, y:219}, {width:43, height:50}}]
5) UIATableCell [name:X value:(null) rect:{{x:15, y:269}, {width:290, height:50}}]
6) UIAElement [name:Y value:(null) rect:{{x:15, y:269}, {width:247, height:50}}]
6) UIAButton [name:more info, B value:(null) rect:{{x:262, y:269}, {width:43, height:50}}]
4) UIAButton [name:(null) value:(null) rect:{{x:85, y:339}, {width:150, height:32}}]
4) UIAImage [name:(null) value:(null) rect:{{x:0, y:334}, {width:320, height:97}}]
4) UIAStaticText [name:SomeText value:(null) rect:{{x:45, y:377}, {width:189, height:21}}]
4) UIAStaticText [name:SomeText value:(null) rect:{{x:45, y:406}, {width:230, height:21}}]
4) UIAStaticText [name:SomeText value:(null) rect:{{x:234, y:377}, {width:41, height:21}}]
4) UIAStaticText [name:(null) value:(null) rect:{{x:0, y:0}, {width:0, height:21}}]
4) UIAStaticText [name:(null) value:(null) rect:{{x:45, y:377}, {width:230, height:21}}]
4) UIAStaticText [name:(null) value:(null) rect:{{x:45, y:406}, {width:230, height:21}}]
4) UIANavigationBar [name:mainNavigationBar value:(null) rect:{{x:0, y:20}, {width:320, height:44}}]
5) UIAStaticText [name:mainNavigationBar value:(null) rect:{{x:108, y:28}, {width:105, height:27}}]
5) UIAButton [name:Back value:(null) rect:{{x:5, y:27}, {width:49, height:30}}]
4) UIATabBar [name:(null) value:(null) rect:{{x:0, y:431}, {width:320, height:49}}]
5) UIAButton [name:tab1 value:1 rect:{{x:2, y:432}, {width:103, height:48}}]
5) UIAButton [name:tab2 value:(null) rect:{{x:109, y:432}, {width:102, height:48}}]
5) UIAButton [name:tab3 value:(null) rect:{{x:215, y:432}, {width:103, height:48}}]

日志清楚地显示了“返回”按钮,但我没有得到正确的参考。谁能在这里指出我正确的方向?难道我做错了什么?仅供参考,我尝试从frontMostApp()、mainWindow() 和navigationBar() 访问buttons()。都失败了。

【问题讨论】:

    标签: iphone xcode cocoa-touch instruments ios-ui-automation


    【解决方案1】:

    “后退”按钮不是窗口的直接子窗口,而是导航栏。尝试使用以下代码访问它:

       var backButton = UIATarget.localTarget().frontMostApp().mainWindow().navigationBar().buttons()["Back"]; 
    

    【讨论】:

    • 感谢您的回复。请注意,我有两个导航栏,当我尝试使用 navigationBar() 方法访问导航栏时,我得到了不需要的第二个导航栏。
    • 你应该能够通过它的名字进入“真正的”导航栏然后 ...mainWindow().elements()["mainNavigationBar"]
    【解决方案2】:

    您是如何构建视图层次结构的?从日志来看,您在一个 UIWindow 下似乎有两个导航栏:一个未标记,一个标记有 mainNavigationBar

    正如克劳斯指出的那样,您应该能够通过访问主窗口的导航栏来访问后退按钮,但在您的情况下,由于存在第二个导航栏,这可能会失败。

    如果有帮助,以下是我编写的脚本,它逐步遍历 UICatalog 示例应用程序中的每个部分,抓取该部分的屏幕截图,然后返回导航控制器的主级别。在此示例中,我使用leftButton() 访问导航栏上的后退按钮。

    var target = UIATarget.localTarget();
    var application = target.frontMostApp(); 
    var mainWindow = application.mainWindow();
    var tableView = mainWindow.tableViews()[0];
    
    
    UIALogger.logMessage("Number of cells " + tableView.cells().length);
    
    for (var currentCellIndex = 0; currentCellIndex < tableView.cells().length; currentCellIndex++)
    {
        var currentCell = tableView.cells()[currentCellIndex];
        UIALogger.logStart("Testing table option " + currentCell.name());
    
        currentCell.scrollToVisible();  
    
        target.delay(1);
        currentCell.tap();  // Go down a level
        target.delay(1);
    
        UIATarget.localTarget().captureScreenWithName(currentCell.name());
        mainWindow.navigationBar().leftButton().tap(); // Go back
        target.delay(1);
        UIALogger.logPass("Testing table option " + currentCell.name());
    }
    

    【讨论】:

    • 感谢您的回复。您在两个导航栏上的位置,我知道这可能会导致问题。但是,我看到没有办法访问该导航栏。有没有?你的权利,当我访问 mainWindow.navigationBar() 时,我得到了这个带有分段控件的导航栏。我可以通过按名称搜索来选择主导航栏吗?
    【解决方案3】:

    也许它不是一个真正的按钮。我遇到了类似的问题,日志将它作为按钮返回,但它实际上是一个视图。因此,我的解决方案是找到项目(可以是图像),然后获取 rect(x, y),然后在屏幕上使用特定位置的选项卡,例如 target.tapWithOptions({x:5, y:190});希望这会有所帮助,干杯

    【讨论】:

      猜你喜欢
      • 2014-11-29
      • 1970-01-01
      • 2016-04-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-07-05
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多