MdaG
MdaG

Reputation: 2730

How do I detect what iOS device my function tests are running on?

I currently have a number of function tests written in javascript using Apple's UIAutomation API. The tests are written for iPhone, but the application also supports the iPad.

To extend my tests to run on an iPad I need to make some adjustments in the code, but first I need to find out what device is running the tests.

How do I detect what device/simulator is running the tests? when I'm running the javascript tests from the Automation tool.

Upvotes: 0

Views: 1064

Answers (3)

swathy valluri
swathy valluri

Reputation: 5287

Here is the code for StrContains method in my Utilities file

String.prototype.strContains = function(value, ignorecase) {
    if (ignorecase) {
        return (this.toLowerCase().indexOf(value.toString().toLowerCase()) != -1);
    }
    else {
        return this.indexOf(value) != -1;
    }
};

Upvotes: 0

swathy valluri
swathy valluri

Reputation: 5287

Follow the documentation provided here, you will get all the information: https://developer.apple.com/library/ios/#documentation/ToolsLanguages/Reference/UIATargetClassReference/UIATargetClass/UIATargetClass.html

//Here is the script I am using to get the device name, os version, bundle id, target etc..

#import "tuneupjs/Utilities.js"
var target = UIATarget.localTarget();

var app_bundle_id = target.frontMostApp().bundleID();
UIALogger.logDebug("App Bundle Id : " + app_bundle_id);

if(app_bundle_id.strContains("ShazamDev"))
    UIALogger.logDebug("Running UIA Scripts for ShazamDev");

var device_name = target.name();
UIALogger.logDebug("Phone Name : " + target.name());

var device_model = target.model();
UIALogger.logDebug("Device Model: " + device_model);

//UIALogger.logDebug("System Name: " + target.systemName());

var ios_version = target.systemVersion();
UIALogger.logDebug("IOS Version: " + ios_version);

Upvotes: 1

MdaG
MdaG

Reputation: 2730

UIATarget.localTarget().model() holds the information about which device the tests are running on.

I have discovered Alex Vollmer's tuneup_js library. It allows for device independent code to some extent as least.

e.g.)

test("my test", function(target, app) {
  assertWindow({
    "navigationBar~iphone": {
      leftButton: { name: "Back" },
      rightButton: { name: "Done" }
    },
    "navigationBar~ipad": {
      leftButton: null,
      rightButton: { name: "Cancel" }
    },
  }); 
});

edit

Found the following in tuneup_js:

   /**
    * A convenience method for detecting that you're running on an iPad
    */
    isDeviceiPad: function() {
      return this.model().match(/^iPad/) !== null;
    },

    /**
     * A convenience method for detecting that you're running on an
     * iPhone or iPod touch
     */
    isDeviceiPhone: function() {
      return this.model().match(/^iPhone/) !== null;
    }

With these I'll be able to write device specific code.

Upvotes: 1

Related Questions