1

I'm new to writing Integration Tests with Subliminal. And I want to test if a certain View Controller is shown after the user taps a button. What would the best way to test this?

  • Can I get the top most view controller with Subliminal and find it's title?
  • Can I get the top most view controller and compare its class?
  • Can I the title label of the Navigation Controller?

At the moment I only managed to get the title label of the Navigation Controller by its accessibility label, like this:

SLElement *titleLabel = [SLElement elementWithAccessibilityLabel:@"Welcome"]; SLAssertTrue([[UIAElement(titleLabel) label] isEqualToString:@"Welcome"], @"It should show the Welcome screen");

But then I would just be testing if @"Welcome" == @"Welcome". And a welcome label might appear in the view hierarchy of other view controllers as well.

What would be your approach to test and assert if the View Controller shown after a user's action is indeed the view controller that I am expecting?

Thanks!

1 Answers1

2

To identify an object in your accessibility hierarchy that can't be described (or can't be uniquely described) by a combination of its accessibility label, value, and traits, your best bet is to use [SLElement elementMatching:withDescription:]. The method is used like this:

SLElement *titleLabel = [SLElement elementMatching:^BOOL(NSObject *obj) {
    // Code to test objects (obj parameter) in the accessibility hierarchy,
    // and return YES or NO depending on whether or not obj is the particular
    // object you're looking for.
} withDescription:@"A description of the view for Subliminal to use in logs/error reports"];

What goes in the matching block depends on the structure of your app and what exactly you're trying to identify, but remember that you can put essentially arbitrary code there to evaluate the input accessibility element obj. You could inspect obj.accessibilityLabel and if that label is equal to to @"Welcome" then (verify that obj is a UIView) and walk up the view hierarchy looking for a particular view or class (maybe UINavigationBar) that proves that obj lives inside your navigation controller. Or you could just test the class of obj and look for something that appears only in the particular navigation controller that you're trying to verify has appeared.

Another tool that you can use to approach problems like this is a Subliminal app-hook. You can register objects in your application (either classes or specific instances) to respond to messages from Subliminal tests. So if, for example, your application delegate knows whether or not this view controller was/is shown, you might register the application delegate to respond to an message indicating whether or not the view controller was shown, and in your test code you can use SLAskAppYesNo to find out whether or not the view controller was/is shown.

Jeffrey Wear
  • 1,155
  • 2
  • 12
  • 24
Aaron Golden
  • 7,092
  • 1
  • 25
  • 31