After a period of brewing, Google was pleased to announce the Earlgrey, a pin-to-iOS functional UI testing framework. A variety of Google apps such as YouTube, Google Calendar, Google Photos, Google Translate, Google Play Music, and more have successfully adopted the Earlgrey framework to meet their functional testing needs.
Features of Earlgre
The main features provided by Earlgrey are as follows, which makes the testing of applications easier and more efficient:
-
Powerful built-in synchronization mechanism: Tests automatically wait for events such as animations, network requests, and so on before interacting with the UI. That way, we can write tests more easily (no sleep, no more waiting), and it's easier to maintain (very intuitive, the entire test looks like a series of descriptions). In general, you don't have to think about synchronization because Earlgrey automatically synchronizes the UI, network requests, the main dispatch
queue, and the main nsoperationqueue. To support the need to wait for an event to occur before the next UI interaction occurs, Earlgrey provides synchronization APIs that you can use to control the synchronization behavior of Earlgrey. You can use these APIs to enhance the stability of your tests.
-
Visibility Detection: All interactions occur on elements that the user can see. For example, trying to tap a button behind a picture will cause the test to fail immediately. Earlgrey uses a screen-sensitive comparison (also known as "screenshot diffs") to determine its visibility before interacting with UI elements. In this way, you can determine the UI that the Earlgrey interacts with, and the user can see and interact with it. It is important to note that out-of-process (that is, system-generated) warning views and other modal dialogs that obscure the UI can interfere with this process.
-
Flexible design: Components used to determine element selection, interaction, assertion, and synchronization are designed to be extensible. Tapping and swiping are achieved through application-level touch events, rather than using an element-level event handler. Before each UI interaction, Earlgrey will assert that the elements of the interaction are visible, not just in the view hierarchy. Earlgrey's UI interaction simulates how real users interact with the app UI, helping you find and fix the same bugs that users encounter when they use the app.
Earlgrey is a native iOS UI Automation testing framework that can help you write more clear and concise tests. With the Earlgrey framework, you can use enhanced synchronization features. Earlgrey automatically synchronizes with the UI, network requests, and various queries, and you can manually implement a custom timer if necessary. The Earlgrey synchronization feature ensures that the UI is in a stable state before the action is executed. This greatly enhances test stability and makes testing highly repeatable. Earlgrey works in conjunction with the Xctest framework and integrates into Xcode's Test navigator so you can run tests directly in Xcode or on the command line (using Xcodebuild).
The user documentation for the Earlgrey is located in the Earlgrey/docs directory. To get started, take a look at the Earlgrey features, see their backwards compatibility, and then install/run Earlgrey by testing the target. Once the configuration is complete, review the Earlgrey API and start writing your own tests.
Here's how to install Earlgrey, and how to create and run the first Test.
To use Earlgrey properly, make sure that the application being tested meets the following requirements:
-
The correct splash screen image can appear on all supported devices (see iOS Developer library,launch Files).
-
The main uiapplicationdelegate implements the (UIWindow *) window property (see Uikit Framework Reference,window properties).
Finally, make sure that you start the assertion on the test target (that is, do not set ns_block_assertions). You can add Earlgrey to your Xcode project in two ways: using Cocoapods, or in the form of a frame.
Cocoapods Installation
For Earlgrey, we strongly recommend using Cocoapods for installation, which is the best way we think.
1th Step: Create a Test target.
Earlgrey requires a test target. Because Earlgrey modifies the scheme and build phases of the test target, we recommend that you create a separate test target for adding Earlgrey tests. If you haven't created it yet, you can select the project in the Xcode Project Navigator and click Editor > Add Target ... in the menu.
In the Add Target window, select the iOS Unit testing Bundle:
Because Earlgrey uses schemes, the test target must have a scheme associated with it. It's better if the scheme is shared. If the test target does not have a scheme, then go to manage schemes, click the + button, and select the target from the drop-down menu. Select the shared option, and then set container to the app you want to test.
It's worth noting that if you've created schemes before, you'll need to run it once to have the pod install command select it. If the test target does not contain changes to the scheme and build phase after you run the pod install, run the pod install again.
2nd step: Add Earlgrey and as a framework dependency
Once you've created your test target (say, appfunctionaltests), you need to add Earlgrey as a framework dependency now. To do this, add Earlgrey as a test dependency to podfile. Since Earlgrey must be placed in the application under test, we need to make some changes to the build phases and scheme of the test target. Add the Configure_earlgrey_pods.rb file to the project directory (run the pod Install command here). You need to invoke this script in the Post_install hook, using the project name, the test target name, and the Xcscheme file name.
3rd step: Run the Pod install command
Once the pod install command has been successfully run, open the generated workspace and you will find the installed Earlgrey in Pods/directory.
Create and run a first test
Because Earlgrey is based on Xctest, creating the first Test in Xcode is as simple as creating a new unit test case class. Please do not confuse the unit test case class with the UI Test case class. The UI Test case class uses the new UI testing attribute added to Xctest, which is not compatible with Earlgrey.
-
Cmd+click the directory where the source files are applied, and then select New File .... A dialog box will pop up.
-
Select Unit Test caseclass in the Pop-up dialog box, and then click Next. In the next interface, enter the name of the test case. For this example, we enter Myfirstearlgreytest.
-
In the next interface, make sure that the test is associated with the unit test target. For this example, the goal is simpleapptests.
-
Xcode will create a new test case for us, but we don't use this use case. Let's modify its code to leave only a test method and include the Earlgrey framework.
-
Now add a simple Earlgrey assertion, check if a key window exists, and assert that it will be displayed. Here is the final Test code:
-
This is it! As with other unit tests, the test is displayed in the Test Navigator, so you can run the test by clicking the Run icon or the cmd+clicking test name and then selecting Test "Testpresenceofkeywindow". Because this is a regular unit test, you can break points in your test and application code, or you can use built-in tools seamlessly.
For WHO to use
? It is based on Xctest (Xcode's own) extension, which you can understand as a better use of xctest.
This intrusive framework (which requires the test bundle to be put into the application within the Xcode integration or command line) means that the tool is primarily geared toward developing engineers, and because it is a white-box test, it is primarily for developers to measure their apps.
So if you're an independent developer, it's going to be a great test tool for you.
Problems that can't be solved
Earlgre cannot replace a test based on a real machine. This means that the app's compatibility with real-world devices cannot be exposed by software simulations. If you want to avoid user churn due to hardware problems, you still need to test your app on hundreds of real phones.
Automated real-time testing can help you solve battles quickly when you need to test on hundreds of phones. So, if you're a test engineer who doesn't have the ability to write code, and the company just needs you to do large-scale real-machine testing, the compatibility tests provided by Testbird will be more appropriate for you.
Currently Testbird offers a free compatibility Test experience (Testbird official registered address)
Google launches iOS functional UI testing Framework Earlgrey