Test your Dynamics Apps with BBD Automated Test Support for iOS

Enterprise Solutions

If you’ve ever developed an app that requires user authentication, you surely know the pain of entering test@test.com and abc123 into your login text fields every time you want to see how that new shade of red looks on a UI button. Of course, there are workarounds for this such as automatic login methods and pre-filled text fields, but these workarounds can compromise the security of your Application. Fear not; with BlackBerry’s Automated Testing Support Library for the BlackBerry Dynamics SDK, you’ll never have to enter another abc123 password again (unless that’s your computer password, which I would not recommend)!

Get Started Using the Automated Testing Support Library

While the Automated Testing Support Library for BlackBerry Dynamics SDK contains a plethora of powerful testing features, a good starting point for integrating the library with your app tests is automated logins. This project makes use of BlackBerry’s newly released BlackBerry Dynamics SDK Beta; sign up here to gain access to the SDK Beta. Before you begin, make sure that you follow these instructions to install the BlackBerry Dynamics SDK for iOS if you have not done so already. Note to only follow the linked instructions up to Deploy and Test > Entitle App Users part 6; the remaining steps will be automated by the following test. By the end of the linked tutorial, you will have a blank BlackBerry Dynamics application running and setup with your UEM server.

Once you have a blank BlackBerry Dynamics app running on your device, it’s time to add the required libraries to get testing! By default, the AutomatedTestSupport library needs to be added to your project directory before you can start using its features. In Finder, navigate to ~/Library /ApplicationSupport /BlackBerry /Good.platform /iOS /AutomatedTestSupport and drag “Core” over to your Xcode projects under the <project_name>UITests folder. Ensure that “Copy items if needed” is selected, along with “Create groups” and all targets are selected.

Now that the libraries are added, it’s time to dive into some coding. We’ll begin by creating a file that contains your credentials that the UITest will use to automatically populate the Email Address, Access Key and Password fields! Begin by creating a new empty file under <project_name>UITests named “credentials.json”. In the new json file, paste the following code:

{
    "GD_TEST_PROVISION_EMAIL":"",

    "GD_TEST_PROVISION_ACCESS_KEY":"",

    "GD_TEST_PROVISION_PASSWORD":""
}

Each GD_TEST_PROVISION property represents a string that the BlackBerry Automated Testing Library will fill in automatically. Begin by entering your user email, followed by generating a BlackBerry Dynamics Access Key in the UEM console. Paste the newly generated access key in the credentials.json file, making sure to remove any dashes and spaces (the key should be a 15 character long string). Finally, enter a password that you would like to use to log into your Dynamics application. Now it’s time to move to the meat and potatoes of this tutorial: coding the test!

Note: if you are using the newest beta version of the BlackBerry Dynamics SDK 4.0.0.5648, you will need to add a <bundle_identifier>.sc3 property to your URL Schemes in your app’s Info.plist. Since the newest beta version also permits use of Apple’s new face ID feature to sign into Dynamics, you will also need to add a NSFaceIDUsageDescription key to your Info.plist accompanied by a brief descriptor of why your app requires this access.

Automated Testing Library with Objective-C

Begin by including the BBDAutomatedTestSupport header file by pasting the following line in <project_name>UITests.m:

#import “BBDAutomatedTestSupport.h”

Next, delete the launch XCUIApplication call under the setUp method; we are going to move this to a custom test method. The test method we will be using, testProvision, will run two tests: check if the user has successfully activated their BlackBerry Dynamics App, and a simple check if the main UI is shown on the screen. To achieve the latter of the two, head to ViewController.m and paste in the following code to indicate that ViewController is your main UI:

self.view.accessibilityIdentifier = @”MainVC”;

Now it’s time to automate your test. Open <project_name>UITest.m and replace the method named testExample with the following code:

- (void)testProvision {
    
    XCUIApplication *application = [[XCUIApplication alloc] init];
    [application launch];
    
    BBDAutomatedTestSupport *ats = [BBDAutomatedTestSupport sharedInstance];
    
    [self fetchCredentials];
    
    [ats setupBBDAutomatedTestSupportWithApplication:application forTestCase:self];
    
     // test 1: if the user has successfully activated their BlackBerry Dynamics App
    BOOL isActivationSucceed = [ats loginOrProvisionBBDApp];
    XCTAssertTrue(isActivationSucceed, @"Activation Failed!");
    
     // test 2: if main UI is shown on screen
    BOOL mainUIShown = [ats isScreenShown:@"MainVC" timeout:50.f];
    XCTAssertTrue(mainUIShown, @"Main UI is not on screen");
}

- (void)fetchCredentials {
    // get user credentials (email, access key, password) from credentials.json
    // and pass to BlackBerry Dynamics SDK
    NSString *credentialsJSONBundlePath = [[NSBundle bundleForClass:[self class]] pathForResource:@"credentials" ofType:@"json"];
    [[BBDAutomatedTestSupport sharedInstance] fetchCredentialsFromFileAtPath:credentialsJSONBundlePath];
}

The method fetchCredentials looks at the credentials.json file you created earlier and passes the information to the BBDAutomatedTestSupport library to automatically populate the textfields upon login. You are now set to start testing your app!

Automated Testing Library with Swift

If you’re a modern day Swift programmer, there’s one small additional step you’ll have to follow to gain access to the BBDAutomatedTestSupport library: add a bridging header. To do so, press the + button on the bottom left corner of Xcode to add a new file; select “UI Test Class Case”, set the language to Objective-C and select “Okay” when you are prompted to create a bridging header file. This is a little hack I’ve picked up throughout my Xcode ventures; in contrast to manually doing so, this method of creating a bridging header allows Xcode to take care of configuring the project settings to incorporate the file. You can now delete the .m UI Test Class Case file that you just created. In The <project_name>-Bridging-Header.h file, paste the following code:

#import “Core/BBDAutomatedTestSupport.h”

Next, delete the launch XCUIApplication call under the setUp method; we are going to move this to a custom test method. The test method we will be using, testProvision, will run two tests: check if the user has successfully activated their BlackBerry Dynamics App, and a simple test if the main UI is shown on the screen. To achieve the later of the two, head to ViewController.m and paste in the following code to indicate that ViewController is your main UI:

self.view.accessibilityIdentifier = “MainVC”

Now it’s time to automate your test. Open <project_name>UITests.swift and replace the method named testExample with the following code:

func testProvision () {
    let application : XCUIApplication = XCUIApplication()
    application.launch()
        
    let ats : BBDAutomatedTestSupport = BBDAutomatedTestSupport.sharedInstance()
    self.fetchCredientials()
        
    ats.setupBBDAutomatedTestSupport(with: application, for: self)
        
    // test 1: if the user has successfully activated their BlackBerry Dynamics App
    let isActivationSucceed = ats.loginOrProvisionBBDApp()
    XCTAssert(isActivationSucceed, "Activation Failed!")
        
    // test 2: if main UI is shown on screen
    let mainUIShown = ats.isScreenShown("MainVC", timeout: 10)
    XCTAssert(mainUIShown, "Main UI is not on screen")
        
}
    
func fetchCredientials () {
    // get user credentials (email, access key, password) from credentials.json
    // and pass to BlackBerry Dynamics SDK
    let credentialsJSONBundlePath : String = (Bundle(for: type(of: self)).path(forResource: "credentials", ofType: "json"))!
    BBDAutomatedTestSupport.sharedInstance().fetchCredentialsFromFile(atPath: credentialsJSONBundlePath)
}

 

The method fetchCredentials looks at the credentials.json file you created earlier and passes the information to the BBDAutomatedTestSupport library to automatically populate the textfields upon login. You are now set to start testing your app!

Build and Test

You are now set to start testing your application and make use of the powerful features offered in BlackBerry’s Automated Testing Support Library! If this is the first time you are launching your app on your device, be sure to build and run your app to configure it with the BlackBerry UEM Client application; doing this in test mode will stop the test prematurely. Once configured with the UEM Client app, build and test your application and watch as the Automated Testing Support Library automatically populates your credentials!

Moving Forward

Congratulations, you have now successfully built and tested your first iOS app using BlackBerry’s Automated Testing Support Library! After you’re done celebrating this triumph, try implementing the unLockBBDApp method to unlock an app that was previously locked with the UEM console, or the waitForRemoteLock method to test BlackBerry UEM’s remote lock functionality. The test you just built comes standard in all of the Objective-C examples in the BlackBerry Dynamics SDK, so testing those will now be a breeze; you can also refer to the example tests for insight on how to use the framework to speed up your testing workflow. You can also check out this great blog to get started using the testing framework for your Android apps. This is only the start to what you’re now capable of testing with the support library; with this primer you can now take your iOS testing abilities to the next level and code with confidence. Happy testing!

About Connor Holowachuk

Connor Holowachuk is a Enterprise Solutions Development Student at BlackBerry.

Join the conversation

Show comments Hide comments
+ -