UI Testing & Screen Level UI Testing

On 2 mai 2016 by jb

When comes about testing, first thoughts come to Unit test, to test models and logics. Then about UI, there are a lot of frameworks or tools which can be used, but most of them appears to be complicated to implement.

 

Since Xcode 7, testing UI is very simple, only create a new UI Testing target, then clic on the record button, play with the UI, stop and it’s done!!! Just run your tests, and watch, it’s magic. (Works for iOS, MacOS and TVos)
If you haven’t watched the WWDC2015 about this, you should do !! https://developer.apple.com/videos/play/wwdc2015/406/

 

test

Take a look at this small red circle button, which let you record your scenarii. If a test failed just after creation, because some elements are missing, you may have to check your app accessibility.

 

This is very easy, but all ways to test UI, launch the app from the beginning and you have to navigate to the screen (or feature) you want to test. There is no such tool that let you test each controller independantly. After some researches there is a way to do this with some little tricks. http://stackoverflow.com/questions/34340896/ios-ui-testing-on-an-isolated-view

The trick is to create a new AppDelegate which will route the app to right controller depending on parameters. We need to add also a parameter check in the main.m to route to the new AppDelegate. (For Swift project, you will have to add the main.swift file and remove the @UIApplicationMain)

 

main.swift:
main
UITestingAppDelegate.swift:
appdelegate

ThirdViewControllerUITests.swift:
third
Launch the tests and you will see all viewControllers that your created a test for.
Amazing, you are now able to test all viewControllers independantly, but if you try to use the record feature, you will see that it starts at the beginning of the app.. All of this for nothing ?? Nope, there is another trick, for recording tests, Xcode launch the Run target, then we just have to add some parameter to launch the right viewController. Unfortunatly you will have to do this as many time you have controllers to test, anyway, normally you will spend more time running tests than writing it.. Hope so.

 

Edit scheme of the run target to add the required parameters:
scheme

 

Now, return to your test (the one you set parameters for), then click on record button (small red button), Xcode launches the simulator directly to the right viewController. Now you can record your tests.

 

Of course it was simple examples, without any data given to the view controllers. Depending on yours needs, you will have to add models or view models in the UITestingAppDele to create your controllers. And create constants or use class name than litteral things as parameter to avoid mistakes.

Now have fun testing UI..