Generating Boilerplate code for iOS

Once we have the preceding Appium set up does and it runs successfully to deploy the app on the iOS simulator, we can use the boiler plate code to implement the first step which is about launching the app. The precedingly written scenario in case of android is contextual to the app; let's write a unique scenario for the iOS app to understand the concepts under a new feature file and name it Sample_ios.feature:

Following is the snippet which you can copy after creating a new feature file for iOS features:

Feature: Hello World

Scenario: Computing sum of two number
As a user When I add two number 22 and 33
I should see the sum 55

When I launch iOS app
And I choose to enter "22" and "33"
When I tap on Compute Sum
Then I should see the result "55"

So the preceding scenario is performing in following steps:

  1. Launch the app.
  2. Enter two numbers 22 and 33 in the specified text-box.
  3. Tap on Compute Sum.
  4. Verify result as 55.

Let's implement the first step which is launching the iOS app. Remember we have the Appium session running and the app is already launched on the simulator. Click on the Record button on the Appium inspector screen. This will generate the boiler plate code as shown:

So if we notice the important desired capabilities, they are:

  1. platformName - iOS
  2. platformVersion - 8.4
  3. deviceName - iPhone 6
  4. app - Actual app path

Let's implement the first step When I launch iOS app ; we need to copy the preceding code generated as boiler plate and make some minor tweaks as shown:

@When("^I launch iOS app$")
public void iLaunchIOSApp() throws Throwable {
DesiredCapabilities capabilities = new DesiredCapabilities();
capabilities.setCapability("appium-version", "1.0");
capabilities.setCapability("platformName", "iOS");
capabilities.setCapability("platformVersion", "8.4");
capabilities.setCapability("deviceName", "iPhone 6");
capabilities.setCapability("app",
"/Users/nishant/Development/HelloAppium/app/TestApp.app");
AppiumDriver wd = new AppiumDriver(new
URL("http://0.0.0.0:4723/wd/hub"), capabilities);
wd.manage().timeouts().implicitlyWait(60, TimeUnit.SECONDS);
}

This gives us the driver instance wd which can be used for all the test purpose. Let's learn how to run the preceding steps.

So, the steps to run the feature file are these:

  1. Start the terminal and type in appium --session-override.
  2. Navigate to IntelliJ and open the feature file Sample_ios.feature.
  3. Right click on the feature file and choose the run feature option:

This will launch the iOS simulator and run the test which is basically going to deploy the app and launch the app. The test would fail because other steps are yet to be implemented.

We need to perform the above demonstrated refactoring to extract the AppiumDriver instance and make it a class variable. Once it is done, let's automate other steps to complete the scenario automation. Next step to get automated is :

 And I choose to enter "22" and "33"

We need to figure out the locators of the text box. Launch the appium GUI app, previously made iOS settings would persist. Click on Start, this will start the appium server. We need to wait till the appium server is started and the console log is shown as below:

And then click on the Appium Inspector icon which would start the iOS simulator and deploy the app on the same. This would deploy the app, and launch the appium inspector window. Click on the first text box field on the right of the appium inspector window, it will load the UI hierarchy and the attributes in the left pane, as shown in image below:

We can use the property name for identifying the first text box by using the method findElementByAccessibilityId() and pass the property name as the parameter.

Step implementation for the above mentioned step would be:

@And("^I choose to enter "([^"]*)" and "([^"]*)"$")
public void iChooseToEnterAnd(String num1, String num2) throws Throwable {
wd.findElementByAccessibilityId("TextField1").sendKeys(num1);
wd.findElementByAccessibilityId("TextField2").sendKeys(num2);
}

In the above code, we are trying to find a field using it's accessibility identifier and then pass a value in that field. Similarly we can implement the other two steps using accessibility identifier :

When I tap on Compute Sum
Then I should see the result "55"

Below is how the iOSPageSteps.Java file would look like after the implementation:

public class iOSPageSteps {

private AppiumDriver appiumDriver;

@When("^I launch iOS app$")
public void iLaunchIOSApp() throws Throwable {
DesiredCapabilities capabilities = new DesiredCapabilities();
capabilities.setCapability("appium-version", "1.0");
capabilities.setCapability("platformName", "iOS");
capabilities.setCapability("platformVersion", "8.4");
capabilities.setCapability("deviceName", "iPhone 6");
capabilities.setCapability("app",
"/Users/nishant/Development/HelloAppium/app/TestApp.app");
appiumDriver = new AppiumDriver(new URL
("http://0.0.0.0:4723/wd/hub"), capabilities);
appiumDriver.manage().timeouts()
.implicitlyWait(60, TimeUnit.SECONDS);
}

@And("^I choose to enter "([^"]*)" and "([^"]*)"$")
public void iChooseToEnterAnd(String num1, String num2) throws Throwable {
appiumDriver.findElementByAccessibilityId("TextField1").sendKeys(num1);
appiumDriver.findElementByAccessibilityId("TextField2").sendKeys(num2);
}

@When("^I tap on Compute Sum$")
public void iTapOnComputeSum() throws Throwable {
appiumDriver.findElementByAccessibilityId("ComputeSumButton").click();
}

@Then("^I should see the result "([^"]*)"$")
public void iShouldSeeTheResult(String result) throws Throwable {
Assert.assertEquals(result,
appiumDriver.findElementByAccessibilityId("Answer").getText());
}

}

This completes the automation of the scenario for iOS app. In the upcoming chapters, we can perform the general refactoring on the above generated code for iOS app. However, some of the code which are android specific will not work for iOS app.

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

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