/** Run before each test * */
  @Before
  public void setUp() throws Exception {
    DesiredCapabilities capabilities = new DesiredCapabilities();
    capabilities.setCapability("appium-version", "1.1.0");
    capabilities.setCapability("platformVersion", "7.1");
    capabilities.setCapability("platformName", "ios");
    capabilities.setCapability("deviceName", "iPhone Simulator");

    // Set job name on Sauce Labs
    capabilities.setCapability("name", "Java iOS tutorial " + date);
    String userDir = System.getProperty("user.dir");
    String localApp = "UICatalog6.1.app.zip";
    if (runOnSauce) {
      String user = auth.getUsername();
      String key = auth.getAccessKey();

      // Upload app to Sauce Labs
      SauceREST rest = new SauceREST(user, key);

      rest.uploadFile(new File(userDir, localApp), localApp);

      capabilities.setCapability("app", "sauce-storage:" + localApp);
      URL sauceURL = new URL("http://" + user + ":" + key + "@ondemand.saucelabs.com:80/wd/hub");
      driver = new AppiumDriver(sauceURL, capabilities);
    } else {
      String appPath = Paths.get(userDir, localApp).toAbsolutePath().toString();
      capabilities.setCapability("app", appPath);
      driver = new AppiumDriver(new URL("http://127.0.0.1:4723/wd/hub"), capabilities);
    }

    sessionId = driver.getSessionId().toString();

    driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
    Helpers.init(driver);
  }
  /**
   * Invokes the Sauce REST API to retrieve the details for the jobs the user has access to.
   * Iterates over the jobs and attempts to find the job that has a 'build' field matching the build
   * key/number.
   *
   * @param username
   * @param accessKey
   * @throws Exception
   */
  private ArrayList<JobInformation> retrieveJobIdsFromSauce(String username, String accessKey)
      throws InvalidKeyException, NoSuchAlgorithmException, UnsupportedEncodingException {
    ArrayList<JobInformation> jobInformations = new ArrayList<JobInformation>();

    String buildName =
        PlanKeys.getPlanResultKey(resultsSummary.getPlanKey(), getResultsSummary().getBuildNumber())
            .getKey();
    SauceREST sauceREST = new SauceREST(username, accessKey);
    // invoke Sauce Rest API to find plan results with those values
    String jsonResponse = sauceREST.getBuildFullJobs(buildName);
    logger.info("REST response " + jsonResponse);
    try {
      JSONObject jsonObject = new JSONObject(jsonResponse);
      JSONArray jobs = jsonObject.getJSONArray("jobs");
      for (int i = 0; i < jobs.length(); i++) {
        JSONObject jobData = jobs.getJSONObject(i);
        String jobId = jobData.getString("id");
        if (jobId != null) {
          logger.info("Adding jobInformation for " + jobId);
          JobInformation information =
              new JobInformation(jobId, calcHMAC(username, accessKey, jobId));
          information.populateFromJson(jobData);
          jobInformations.add(information);
        } else {
          logger.warn("Unable to find jobId in jsonData");
        }
      }
    } catch (JSONException e) {
      logger.error("Unable to process json returned by saucelabs", e);
    }
    return jobInformations;
  }
  /**
   * To run before each test. Here we want to define the CAPABILITIES of the target device, the APK
   * of the application to use, etc.
   */
  @Before
  public void setUp() throws Exception {

    /*
     * Step 1 : The capabilities  we want to have
     */
    final DesiredCapabilities CAPABILITIES = new DesiredCapabilities();
    CAPABILITIES.setCapability("appium-version", "1.1.0");
    CAPABILITIES.setCapability("platformName", "Android");
    CAPABILITIES.setCapability("deviceName", "Android");
    CAPABILITIES.setCapability("platformVersion", "4.3");

    // Set the job name on Sauce Labs if needed
    CAPABILITIES.setCapability("name", "Java Android Calculator " + date);
    String userDir = System.getProperty("user.dir");

    /*
     * Step 2 : Defines the APK to use, i.e. the name of the file to push on the device
     */
    final String LOCAL_APP = "app_calculator.apk";

    /*
     * Step 3 : The plateform of tests : cloud with Sauce or local ?
     */
    final URL SERVER_ADDRESS;
    // Upload app to Sauce Labs if run on Sauce
    if (runOnSauce) {
      String user = auth.getUsername();
      String key = auth.getAccessKey();
      SauceREST rest = new SauceREST(user, key);
      rest.uploadFile(new File(userDir, LOCAL_APP), LOCAL_APP);
      CAPABILITIES.setCapability("app", "sauce-storage:" + LOCAL_APP);
      SERVER_ADDRESS = new URL("http://" + user + ":" + key + "@ondemand.saucelabs.com:80/wd/hub");
      driver = new AndroidDriver(SERVER_ADDRESS, CAPABILITIES);
      // Starts tests on a local platform
    } else {
      String appPath = Paths.get(userDir, LOCAL_APP).toAbsolutePath().toString();
      CAPABILITIES.setCapability("app", appPath);
      SERVER_ADDRESS = new URL("http://127.0.0.1:4723/wd/hub");
      driver = new AndroidDriver(SERVER_ADDRESS, CAPABILITIES);
    }

    sessionId = driver.getSessionId().toString();

    driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
    Helpers.init(driver, SERVER_ADDRESS);
  }