views:

66

answers:

2

I have a simple function that should check if the view is at the home interface, and if not, bring it to the home:

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!");
    }
}

Now, i can't get past the error "Could not find 'Back' button!". It's up there, on the left corner, staring right at me, and taunting me!

The UIATarget.localTarget().frontMostApp().logElementTree() gives me following tree:

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}}]

The log clearly shows the "Back" button, yet i don't get a proper reference for it. Can anyone point me in the right direction here? Am i doing something wrong? FYI, I've tried to access the buttons() from frontMostApp(), mainWindow() and navigationBar(). All failed.

+2  A: 

The "Back" button is not a direct child of the window, but instead the navigation bar. Try accessing it with the following code:

   var backButton = UIATarget.localTarget().frontMostApp().mainWindow().navigationBar().buttons()["Back"]; 
Claus Broch
Thanks for the reply. Note that I have two navigation bars, and i get the unwanted second navigation bar when i try to access the navigation bar with navigationBar() method.
Mustafa
You should be able to get to the "real" navigation bar by its name then ...mainWindow().elements()["mainNavigationBar"]
Claus Broch
Worked. Super. Thanks.
Mustafa
+1  A: 

How are you constructing the view hierarchy? From the log, it looks like you have two navigation bars under the one UIWindow: one unlabeled, and one labeled with mainNavigationBar.

As Claus points out, you should be able to get at the back button by accessing the main window's navigation bar, but that might be failing in your case because of the presence of that second navigation bar.

In case it helps, the following is a script that I wrote which steps through every section in the UICatalog sample application, grabs a screenshot of that section, then returns to the main level of the navigation controller. In this example, I use leftButton() to access the back button on the navigation bar.

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());
}
Brad Larson
Thanks for your reply. Your spot on about two navigation bars, and i know this might be causing the problem. But, i see that there is no way to access that navigation bar. Is there? Your right, when i access mainWindow.navigationBar(), i get this navigation bar with segmented control in it. Can i pick the main navigation bar via searching by it's name?
Mustafa
@Claus Broch's solution worked for me.
Mustafa