EarlGrey – IOS UI Testing Framework
EarlGrey – IOS UI Testing Framework
With today’s UI testing framework, most of the UI tests are slow and complex to write and Synchronization of UI is the major issue.
EarlGrey
EarlGrey is a native iOS UI test automation framework that enables you to write clear and concise tests, released by Google.
Features
- Synchronization:
- EarlGrey’s synchronization features help to ensure that the UI is in a steady state before actions and assertions are performed. This greatly increases test stability.
- EarlGrey automatically synchronizes with the UI, network requests, main Dispatch Queue, main NSOperationQueue, Animations, Gestures, Network, View Controller Appearance / Disappearance, Keyboard interaction, Scrolling, Apps main run loop and Web View.
- There is no need to add manual waits for resource idealisation. An example where manual waits need to be added is KIF testing framework.
- EarlGrey works in conjunction with the XCTest framework and integrates with Xcode Test Navigator so you can run tests directly from Xcode or the command line (using xcodebuild command).
- Visibility Checks:
- Before every UI interaction, EarlGrey asserts that the elements which are being interacted with are actually visible and not just present in the view hierarchy.
- User-like Interactions:
- Swipes, scrolls and taps are performed just as a user interacts with the app.
EarlGrey’s Interaction API
Interaction API is used for Selecting any UI element and performing action or assertion on it.
Identifying UI element
EarlGrey.selectElement(with: grey_accessibilityID("button"))
Performing action on UI element
EarlGrey.selectElement(with: grey_accessibilityID("button")) .perform(grey_tap())
Asserting UI element
EarlGrey.selectElement(with: grey_accessibilityID("button")) .assert(grey_sufficientlyVisible())
Performing action on off screen UI element
EarlGrey.selectElement(with: grey_accessibilityID("cell")) .usingSearch( grey_scrollInDirection(.down, 50), onElementWith: grey_accessibilityID("tableView") )
EarlGrey’s Synchronization API
This API is used to control EarlGrey’s synchronization
Waits
GREYCondition(name: "Wait for view") { () -> Bool in var error: NSError? EarlGrey.selectElement(with: grey_accessibilityID("view")) .assert(grey_sufficientlyVisible(), error: &error) return error == nil }.wait(withTimeout: 20, pollInterval: 5)
Note: There is no need to add waits for UI Elements, as EarlGrey itself synchronizes the app resources. But in some cases if you need to wait, you can use EarlGrey’s GREYCondition.
Disabling synchronization
GREYConfiguration.sharedInstance().setValue(false, forConfigKey: kGREYConfigKeySynchronizationEnabled)
Which can be applicable where you need to perform some action on UI elements but a video is continuously streaming and the app is not getting ideal. By default, the interaction timeout is 30 seconds. If the UI element is not found due to background animation within 30 seconds, then a timeout exception occurs.
Skipping network call
Some network calls synchronization can be skipped by blacklisting network URL as:
GREYConfiguration.sharedInstance().setValue("www.test.api.com", forConfigKey: kGREYConfigKeyURLBlacklistRegex)
EarlGrey’s Other Top Level API
Change device orientation
EarlGrey.rotateDeviceTo(orientation: .landscapeLeft, errorOrNil: nil)
Dismiss keyboard
EarlGreyImpl.invoked(fromFile: #file, lineNumber: #line).dismissKeyboardWithError(nil)