Google launches iOS functional UI testing Framework Earlgrey

Source: Internet
Author: User

This article is from my translation of the Infoq Chinese station, the original address is: Http://www.infoq.com/cn/news/2016/02/earlgrey-ios-functional-ui-test


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.

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 UI, network requests, primary dispatch queue, and master 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.

    1. Cmd+click the directory where the source files are applied, and then select New File .... A dialog box will pop up.
    2. Select the unit Test case Class 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.
    3. In the next interface, make sure that the test is associated with the unit test target. For this example, the goal is simpleapptests.
    4. 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.
    5. 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:
    6. @import earlgrey#import 
        
          
           
         @interface myfirstearlgreytest:xctestcase@end@implementation myfirstearlgreytest-(void) Testpresenceofkeywindow {[[Earlgrey Selectelementwithmatcher:grey_keywindow ()]     Assertwithmatcher:grey_sufficientlyvisible ()];} @end
        
          
    7. 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.

Want to try Earlgrey? Earlgrey is currently open source and is based on Apache licensing. Interested readers can take a look at this handy guide and then add Earlgrey to the Xcode project file by Cocoapods or by hand.

Google launches iOS functional UI test framework Earlgrey

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.