Search code examples
iosxcodeios-ui-automationxcode-instruments

Read the value of a UILabel in Automation template of Instruments in Xcode


I have written the following script to tap on a button on UI. Each press increments the integer value of a UILabel. I wish to read the value of the UILabel in my script. How can I achieve this? I am new to this Automation tool in Instruments.

var target = UIATarget.localTarget();
var appName = target.frontMostApp().mainWindow().name;
target.setDeviceOrientation(UIA_DEVICE_ORIENTATION_PORTRAIT);
target.frontMostApp().mainWindow().buttons()["Increment"].tap();

Solution

  • First make sure you assign accessibility identifier to that UILabel.

    You can do that via code:

    self.myLabel.accessibilityIdentifier = @"uniqueId";
    

    Or via storyBoard:

    enter image description here

    To get label value, use the following line in your UIAutomation script:

    target.frontMostApp().mainWindow().staticTexts()["uniqueId"].value();