1.How to get iOS device & simulator UDID programmatically(JAVA) 2. Pointing to certain env before testing a iOS App(our company app)

1Q.

When 3/4 AutoQA’s execute testcases at their convienant times by pulling the latest develop bitbucket code, they will obv have diff conf data as below

How to programmtically get the iOS Device & simulator udid (whatever they are executing on) - Java. Check the below code

public class BaseTest {
public static AppiumDriverLocalService service;
public static Properties prop = new Properties();
public static InputStream inputProperties = null;
public static DesiredCapabilities cap = null;
public static URL url = null;
public static IOSDriver driver=null;

public static IOSDriver setUp() {

    try {
        inputProperties = new FileInputStream(System.getProperty("user.dir") + "/device.properties");
        prop.load(inputProperties);
        cap = new DesiredCapabilities();


        for(i=1; i<=5;i++)
        {
            if ((prop.getProperty("udid_'"+i+"'").equals(connected_Device_UDID)) && (prop.getProperty("deviceType'"+i+"'").equals("simulator")))
            {
            cap.setCapability("platformName", prop.getProperty("platformName'"+i+"'"));
            cap.setCapability("deviceName", prop.getProperty("deviceName'"+i+"'"));
            cap.setCapability("automationName", prop.getProperty("automationName'"+i+"'"));
            cap.setCapability("udid", prop.getProperty("udid'"+i+"'"));
            cap.setCapability("bundleId", prop.getProperty("bundleId'"+i+"'"));
            driver = new IOSDriver(url, cap);

            }

        else if ((prop.getProperty("udid_'"+i+"'").equals(connected_Device_UDID)) && (prop.getProperty("deviceType'"+i+"'").equals("realDevice")))
        {
            cap.setCapability("platformName", prop.getProperty("platformName'"+i+"'"));
            cap.setCapability("deviceName", prop.getProperty("deviceName'"+i+"'"));
            cap.setCapability("automationName", prop.getProperty("automationName'"+i+"'"));
            cap.setCapability("udid", prop.getProperty("udid'"+i+"'"));
            cap.setCapability("xcodeOrgId", prop.getProperty("xcodeOrgId'"+i+"'"));
            cap.setCapability("xcodeSigningId", prop.getProperty("xcodeSigningId'"+i+"'"));
            cap.setCapability("bundleId", prop.getProperty("bundleId'"+i+"'"));
            driver = new IOSDriver(url, cap);

        }
        }

    } catch (IOException io) {
        io.printStackTrace();
    }
    return driver;
}

Need to use this command: xcrun xctrace list devices in code & get the simulator / device udid as per the requirment.

Any other better simpler way to do the above stuff?

Q2:


Need to point to certain env before testing a scenario - iOS App(our company app). How this can be done programmatically.

just execute on machine where devices connected:

xcrun xctrace list devices

So there is no way to automate it right?

what you mean “to automate”? I gave you solution.
just execute and you have all data. All devices all it UDID numbers…
example:

$ xcrun xctrace list devices
== Devices ==
M-S3HTD5 (3C337C1D-xxxx)
iPhone QA9 old (15.4.1) (66c57f6ff5xxxxxxx)

== Simulators ==
iPad (9th generation) Simulator (15.5) (18A136DE-906A-4DD3-8829-604217E6683F)
iPad Air (5th generation) Simulator (15.5) (5B178AE0-ABE8-4697-AE60-FCD0F090F3DA)
iPad Pro (11-inch) (3rd generation) Simulator (15.5) (C1251708-36F4-4E72-92F2-1ACCF46D6790)
....

Steps:

  1. programmatically in Java execute this code on machine where devices connected (tons of ways to execute. choose whatever you like e.g. https://stackoverflow.com/questions/15356405/how-to-run-a-command-at-terminal-from-java-program)
  2. get it output
  3. sort whatever needed

PS better use json to store phone data e.g. I use something like:

  "iphone8_3": {
    "platformName": "iOS",
    "platformVersion": "15.4",
    "deviceName": "iPhone QA3",
    "udid": "6f7443f73a9xxxx",
    "appiumPort": 6003
  },

Also:

  • in code better detect if it real device or not and use path to .ipa or .app file
  • use parameters like env, number of users … so on (e.g. it easy to use it with testNG or Junit + maven)

If you have libimobiledevices installed (you should) you may also have an easier way:

idevice_id