Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

Full Stack BDD in Xcode With Page Object Model: Part II

DZone's Guide to

Full Stack BDD in Xcode With Page Object Model: Part II

Here is Part II, outlining how to set up 'XCUI POM Test Bundle' target, the 'Acceptance Test' target, and more!

· Mobile Zone
Free Resource

Download this comprehensive Mobile Testing Reference Guide to help prioritize which mobile devices and OSs to test against, brought to you in partnership with Sauce Labs.

Did you miss out on Part I? Don't wait, read it now!

Set Up ‘XCUI POM Test Bundle’ Target

  • From Xcode, create a new app(Or use existing app) and select File —> New —-> Target
  • Now Select ‘XCFit’ for iOS app and Click on ‘XCUI POM Test Bundle’
  • Once Clicked on the target e.g ‘XCUI POM Test Bundle’ Xcode will create UI testing target with properly structured Xcode Groups and required files. You can then make physical directories on Disk Or change the folder/group structure as per your need.
  • You don’t have to so any setting to run those demo XCUI tests. Just CMD+U and You are good to go!

image

What’s in the XCUI POM Template ?

  • YOUR_CUCUMBERISH_TARGETPageObjectTests.swift
    • This file is at the base of the target. It’s exactly same file that Apple’s XCUI Test generate at first. You may use it for recording the tests or simply delete it if not needed.
  • Screens
    • This is groups where we can abstract logic of every screen in the app. Example file are ‘HomeScreen.swift’ and ‘BaseScreen.swift’ This assumes that your apps is made up of multiple screens. You can write individual screen class which extend BaseScreen. All the functionality related to screen can be included in this class.
  • Sections
    • This group has ‘HomeElements.swift’ class which stores all the locators of HomeScreen in enums. Sections are defined to store locators of the screen in the form of enums. Here are some samples we can store Images, Buttons and Static Texts of the HomeScreens. We can access those enums from anywhere from the Test methods or page objects.
  • Protocols
    • Swift is protocol oriented language so feel to start with Protocol per screen. This group has ‘HomeProtocol.swift’ file where there is a way to implement protocol oriented testing. Here is an example of sample protocol for the home screen. We can implement this protocol in the XCTest method as needed or we can write some extensions to support Testing.
  • Tests
    • This group has all the test for our app. Currently demo template has two tests ‘HomeScreenTest.swift’ and ‘ProtocolOrientedTest.swift’. This example shows how XCUI test can be implemented in the Object Oriented way and Protocol oriented way.
  • TestBase
    • Testbase is a group where we can abstract all set up, tear down and common stuff in the base class. Every screen then uses this class as base. You can add more stuff as needed e.g Fixtures, Launch Arguments.

FitNesse for iOS: Acceptance/Contract Tests

FitNesse is a fully integrated standalone wiki and acceptance testing framework for BDD Style testing. As of now, we have seen Cucumber and Page Object pattern test frameworks. If you really wanted to get more information about FitNesse for iOS, please follow documentation on OCSlim project. XCFit adopted the framework as dependency to make it full stack BDD. We will cover basic setup as part of this document.

Setup ‘Acceptance Test’ Target template

  • From Xcode, create a new app(Or use existing app) and select File —> New —-> Target
  • Now Select ‘XCFit’ for iOS app and Click on ‘iOS Acceptance Tests ‘
  • Once Clicked on the target e.g ‘OS Acceptance Tests’ Xcode will create new target with all required files and groups for Acceptance testing
  • Select ‘Acceptance Test’ Scheme from Xcode and try to build
  • The build will fail as we need to fix some Swift3 related issue as well as we need to add XCFit/OCSlimProject Pod to the to the podfile.

** Watch it so far**

image

  1. To Fix Swift Issue : Just Click on ‘Edit-> Convert-> To Current Swift Syntax
  2. To Fix Pod issue : Add ‘XCFit’ for AcceptanceTests target
target 'AcceptanceTests' do      
  pod 'OCSlimProject'
end
$ pod install

You can install Pod by running ‘pod install’

Now, You should be able to build ‘Acceptance Tests” target.

You should also note that the script ‘Launch FitNesse’ has been created in the base of the project. Launch the fitness by executing that script from command line:

$ sh Launch Fitnesse

The browser will pop up with example test. You should be able to execute that suite and see then running and passing.

image

If you get any errors at this stage, please confirm that you have Java as well as ios-sim node package installed.

So, congratulations again. You have just executed FitNesse test fro browser which is talking to your app.

Setting up Fitnesse Acceptance Target with XCTest

You can also setup Fitnesse Acceptance Tests but you need to use Cocoapods for this target.

You can find detailed blog post on DZone.

Add Acceptance and Acceptance Unit Test Target to Project

We have all the predefined targets for FitNesse. Just add “Acceptance Tests” target from the template and “AcceptanceUnitTests” target from the bundle. You will need “FitNesse Suite page name” to create this target but just put “OCSlimProjectExamplePage” there for now . Add your ‘AcceptanceTests’ target as a ‘Target Dependancy’ of this new target in Build Phases. This ensures that it the latest code has been built prior to the tests being run.

Add Pod Dependencies

We need to create a “Podfile” at the root of the project with the following content.

target 'AcceptanceUnitTests' do     
  pod 'OCSlimProjectTestBundleSupport'
end
$ pod install

Now, we can run ‘pod install’ at this stage and close the current Xcode session and open project workspace.

Build Acceptance Tests Target

At this stage, we should be able to build the “Acceptance Tests” target. If you are using Xcode8, you might see some warning related to the Swift3 Syntax. Just Click on ‘Edit-> Convert-> To Current Swift Syntax

Now you should be able to build an “Acceptance Tests” target. Once, build is successful, you should see “LaunchFitnesse” script is generated in the root of the project. We can launch and execute the fitness test as shown below.

Test AcceptanceUnitTests Target

Now if you select “AcceptanceUnitTarget” and press CMD+U.

image

Now we can see that FitNesse tests are running as shown above. We can add this to main scheme to make sure we are running it after the unit tests to follow proper development workflow. We can build and run it as our normal unit tests.

Continuous Integration + Fastlane

Now that , we have seen how to run Cucumberish, XCUIPOM, FitNesse acceptance tests from Xcode but it’s a good idea to run it with Fastlane. We can also take control of version of Cocoapods and Fastlane by using Bundler. Let’s create a Gemfile at the root of the project with the following gem:

source "https://rubygems.org" 
gem 'cocoapods'
gem 'fastlane'

Let’s also create directory “fastlane” and make “Fastfile” with following content:

fastlane_version "1.104.0"  
default_platform :ios   
platform :ios do   
before_all do     
   system "rm -rf ../test_reports/"     
   system "bundle install"     
   system "pod install"     
    system "bundle exec fastlane add_plugin trainer"   
end     
desc "Runs all the XCUI POM, Cucumberish tests"
  lane :xcfit_ui_test do
   scan(
   scheme: "XCFit2Demo",
   destination: 'platform=iOS Simulator,name=iPhone 7 Plus,OS=10.0',
   output_directory: "test_reports/",
   output_types: "html",
   fail_build: false
   )
  end

  desc "Runs Fitnesse Tests"
  lane :fitnesse do
   scan(
   scheme: "AcceptanceUnitTests",
   destination: 'platform=iOS Simulator,name=iPhone 7 Plus,OS=10.0',
   output_directory: "test_reports/",
   output_types: "html",
   fail_build: false
   )
  end
end

After running “bundle install” we should be able to run those test from command line like this:

$ bundle execfastlane xcfit_ui_test

?

image

Once that's done, we can have clear HTML reports generated.

XCFit: Swift Package Manager

XCFit will be having full on support for XCUI Test helpers so that we can use Apple’s XCUI Test Framework painless to use. There is sample swift package on Github to test XCFit Full Documentation and API implementation still in progress. You can grab it like this

import PackageDescription

let package = Package(
    name: "XCFit",
    dependencies: [
  .Package(url: "https://github.com/Shashikant86/XCFit.git", majorVersion: 2),
]
)

Quick Demo With Example App

You can clone the existing repo which has a demo app we can run Unit, FitNesse and Cucumberish tests as XCTest.

$ git clone https://github.com/Shashikant86/XCFit
$ cd XCFit/XCFit2Demo
$ open XCFit2Demo.xcworkspace

Run Unit, FitNesse and Cucumberish test with Xcode. “cmd + U”. We can execute it using Fastlane

 $ bundle install
 $ bundle exec fastlane xcfit_ui_test

Conclusion

With XCFit 2.o, we can get started with BDD within a minute using fully automated Xcode Templates using very popular Page Object Pattern. XCFit enables Full stack BDD in iOS.

Analysts agree that a mix of emulators/simulators and real devices are necessary to optimize your mobile app testing - learn more in this white paper, brought to you in partnership with Sauce Labs.

Topics:
xcode ,bdd ,ios ,swift ,mobile

Published at DZone with permission of Shashikant Jagtap, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}