My First Experience with UI Automation in MAC OS

Im surprised to know about MAC having default Automation in their OS. When i start to explore more about it, i started to develop a small iOS app which continued with Automation, here we go…..

To create a sample app,

Step 1: Open Xcode in mac from Spotlight Search at right top most corner in MAC.

Step 2: In Xcode click File -> New > Project> Single View Application.

Step 3 : Click Next and assign a name for Project (Eg. Sample).

Step 4: Click next, the project opens in Xcode, now at your left you will be having the folders, Click the Storyboard which shows your Iphone 6s view controller.

Step 5: Your designing starts here, we gonna place two buttons here, In your right bottom you have “Filters, search for buttons and drag them to the view controller,

Name those buttons as “Button 1” and “Button 2”.

Step 6: Click the “Show Assistant editor button “ at the right top most corner in Xcode.

Step 7: Now the assistant window opens up at the right side of the view controller, Now hold the first button in the view controller and drag it to the “@interface ViewController ()” in the assistant editor window, now a pop up appears, fill up the details as like in the below image,

Repeat the same for the Button2 also, once done you will have the below code ,

@interface ViewController ()

- (IBAction)Button1:(id)sender;

- (IBAction)Button2:(id)sender;

@end

Step 8: Now click the “Viewcontroller.h” from the folders at the left side of the window,

Step 9: Copy the below code to the “- (IBAction)Button1:(id)sender {}”, here we define the actions for buttons,

“UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@”Button 1" message:@”Pressed Button 1" delegate:nil cancelButtonTitle:@”ok” otherButtonTitles:nil];

[alert show];

Step 10: Do the same for Button 2 too,

Step 11 : Done with your sample app, now run the app by clicking the Run icon at the left top in Xcode window,

Now the Iphone 6s simulator opens with build succeeded message.

Here we can start our UI Automation mac,

We gonna do UI automation for the sample app we have created above,

Step 1: Product> Profile in Xcode.

Step 2: It ask for “Choose profiling template “ Search for automation and choose automation ,

Step 3: Script window opens, in that window paste the below given code,

var target = UIATarget.localTarget(); // It opens your app.

var app = target.frontMostApp(); // It runs your app in simulator(Not headless).

var window = app.mainWindow(); // It opens the main page of the app.

target.deactivateAppForDuration(1); // It kills the app for 2 seconds (Just for test).

window.buttons()[“Button 1”].tap(); // It Taps the first button.

target.deactivateAppForDuration(2); // Again it kills the app.

You can also create a new script, on the right had side you “Add” button by clicking it you can create a new script,

Step 4: Click the “Run” icon to run the script, and you can view the logs in “Editor Logs”.

This blog is to give you a basic idea about UI Automation in MAC and to share with you all about its easy scripting. You can find more about this in below links,

Useful links:

http://www.guru99.com/ios-test-program-uiautomation-framework.html

https://developer.apple.com/library/tvos/documentation/DeveloperTools/Conceptual/InstrumentsUserGuide/UIAutomation.html

Thanks for going through my blog and i hope you have gained something about UI Automation in MAC.