Testing mobile web applications on iOS using Appium

Appium drives automation using a native automation framework and provides an API based on Selenium's WebDriver JSON wire protocol. To automate an iOS application, it uses the UI Automation feature offered as part of Apple Instruments.

Appium works as an HTTP server and receives the commands from test scripts over the JSON wire protocol. Appium sends these commands to Apple Instruments so that these can be executed on the app that is launched in a simulator or real device. While doing so, Appium translates the JSON commands into UI Automation JavaScript commands that are understood by the Instruments. The Instruments take care of launching and closing the app in the simulator or device. This process is shown in the following diagram:

Testing mobile web applications on iOS using Appium

When a command is executed against the app on the simulator or device, the target app sends the response back to the Instruments, which then send it back to Appium in the JavaScript response format. Appium translates the UI Automation JavaScript responses into Selenium WebDriver JSON wire protocol responses and sends them back to the test script.

Getting ready

Before we start writing tests with Appium, we need to add the Appium client library to the project. This example uses Maven for dependency management, so we will add the following dependency for the Appium Java Client library to the POM.xml file:

<dependency>
    <groupId>io.appium</groupId>
    <artifactId>java-client</artifactId>
    <version>3.1.0</version>
    <scope>test</scope>
  </dependency>

How to do it...

Now we have Appium running, so let's create a test that will check the BMI Calculator application on the iPhone Safari browser. Create a new test MobileBmiCalculator with the following code:

package com.secookbook.examples.chapter14.ios;

import java.net.URL;

import io.appium.java_client.ios.IOSDriver;

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.By;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.junit.*;

import static org.junit.Assert.*;

public class MobileBmiCalculatorTest {

  private WebDriver driver;

  @Before
  public void setUp() throws Exception {
    // Set the desired capabilities for iOS- iPhone 6
    DesiredCapabilities caps = new DesiredCapabilities();
    caps.setCapability("platformName", "iOS");
    caps.setCapability("platformVersion", "8.4");
    caps.setCapability("deviceName", "iPhone 6");
    caps.setCapability("browserName", "safari");

    // Create an instance of IOSDriver for testing on iOS platform
    // connect to the local Appium server running on a different machine
    // We will use WebElement type for testing the Web application
    driver = new IOSDriver<WebElement>(new URL("http://192.168.0.101:4723/wd/hub"), caps);

    // Open the BMI Calculator Mobile Application
    driver.get("http://cookbook.seleniumacademy.com/mobilebmicalculator.html");
  }

  @Test
  public void testMobileBmiCalculator() throws Exception {
    // Get the height field and set the value
    WebElement height = driver.findElement(By.name("heightCMS"));
    height.sendKeys("181");

    // Get the weight field and set the value
    WebElement weight = driver.findElement(By.name("weightKg"));
    weight.sendKeys("80");
    // Click on Calculate button
    WebElement calculateButton = driver.findElement(By.id("Calculate"));
    calculateButton.click();

    // Check the Bmi Result
    WebElement bmi = driver.findElement(By.name("bmi"));
    assertEquals("24.4", bmi.getAttribute("value"));

    // Check the Category Result
    WebElement bmi_category = driver.findElement(By.name("bmi_category"));
    assertEquals("Normal", bmi_category.getAttribute("value"));
  }

  @After
  public void tearDown() throws Exception {
    // Close the browser
    driver.quit();
  }
}

How it works...

The Appium Java Client library provides the IOSDriver class that supports executed tests on the iOS platform to run the tests with Appium. However, for Appium to use the desired platform, we need to pass a set of desired capabilities, as shown in the following code:

DesiredCapabilities caps = new DesiredCapabilities();
caps.setCapability("platformName", "iOS");
caps.setCapability("platformVersion", "8.4");
caps.setCapability("deviceName", "iPhone 6");
caps.setCapability("browserName", "safari");

The platformName capability is used by Appium to decide on which platform the test script should get executed. In this example, we used the iPhone 6 Simulator. To run tests on an iPad, we can specify the iPad Simulator.

When running tests on a real device, we need to specify the value of iPhone or iPad for the device capability. Appium will pick the device that is connected to the Mac via USB.

The last desired capability that we used is browserName, which is used by Appium to launch the Safari browser.

Finally, we need to connect to Appium server using IOSDriver and program the desired capabilities that we need. This is done by creating an instance of IOSDriver, as shown in the following code:

driver = new IOSDriver<WebElement>(new URL(
  "http://192.168.0.101:4723/wd/hub"), caps);

The rest of the test uses Selenium API to interact with the mobile web version of the application. Run the test as normal. You will see that Appium will establish a session with the test's script and launch the iPhone Simulator with the Safari app. Appium will execute all the test steps by running commands on the Safari app in the simulator window.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.144.90.182