Wednesday, October 26, 2016

Running Mobile Web Tests on iOS 10 Device (Simulator) using Appium iOS driver, backed by Apple XCUITest (Xcode 8)


Requirements:
- OS X El Capitan (10.11.6)
- Xcode 8 (with command line tools)
- JDK 1.8
- Node.js v6.5.0 (brew install node)
- appium 1.6.0 (npm install -g appium)
Other appium node_modules (appium-doctor, appium-xcuitest-driver)


As a background and for those familiar with appium, UIAutomation - which was being used by Appium for its previous version of running tests against iOS devices - is already deprecated by Apple.

With version 1.6.0,  Appium has added support to target Apple's new XCUITest framework, which includes support for iOS 10 / Xcode 8.

Here's the github link for appium-xcuitest-driver.

Steps:

- Verify appium installation/setup using the ff command:
$ appium-doctor --ios




- Start appium using the ff command:
$ appium --default-capabilities ios.json

where ios.json:
{
"app":"safari",
"newCommandTimeout":600
}





- Run the following code:

import java.net.MalformedURLException;
import java.net.URL;

import org.apache.log4j.Logger;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.openqa.selenium.remote.RemoteWebDriver;

public class AppiumIOS {
 private final static Logger logger = Logger.getLogger(AppiumIOS.class);
 static WebDriver driver;
 
 public static void main(String[] args) throws MalformedURLException, InterruptedException {
  String hubUrl = "http://127.0.0.1:4723/wd/hub";
  String iOSBrowserName = "Safari";
  String iOSDeviceName = "iPhone Simulator";
  String iOSVersion = "10.0";

  DesiredCapabilities capabilities = new DesiredCapabilities();
  capabilities.setCapability("deviceName", iOSDeviceName);
  capabilities.setCapability("platformName", "iOS");
  capabilities.setCapability("platformVersion", iOSVersion);
  capabilities.setCapability("browserName", iOSBrowserName);
  capabilities.setCapability("automationName", "XCUITest");
  driver = new RemoteWebDriver(new URL(hubUrl), capabilities);
  
  String[] searchTerms = new String[] { 
   "Bernard Lago Blogspot", 
   "Bernard Lago Youtube",
   "Bernard Lago Google Plus" 
  };

  for (int i = 0; i < searchTerms.length; i++) {
   googleSearch(searchTerms[i]);
   Thread.sleep(3000);
   getInfoUsingResultIndex(1);  // 1 for the first link, 2 for 2nd etc...
  }
  
  driver.quit();

 }
 
 public static void googleSearch(String searchString) {
  logger.info("Search term: " + searchString);
  driver.navigate().to("https://www.google.com.ph/#q=" + searchString);
 }

 public static void getInfoUsingResultIndex(int index) {
  String firstLinkLocator = String.format("div.g:nth-child(%s) h3.r a", index);
  String firstCiteLocator = String.format("div.g:nth-child(%s) cite", index);
  WebElement firstLink = driver.findElement(By.cssSelector(firstLinkLocator));
  logger.info("First Link URL: "+ driver.findElement(By.cssSelector(firstCiteLocator)).getText());
  logger.info("First link Page Title: " + firstLink.getText());
 }

}

Here's the screenshot of the actual execution.



That's it. Thanks for visiting!

No comments:

Post a Comment