From 7e5b64c693082b26628d481a1b8e39232474b0f3 Mon Sep 17 00:00:00 2001 From: Aditya Atul Tirodkar Date: Sun, 26 Mar 2017 22:57:42 -0700 Subject: [PATCH] Update documents and added ifaq.md as per the comments --- docs/faq.md | 48 ++--- docs/ifaq.md | 13 ++ docs/install-and-run.md | 386 ++++++++++++++++++++-------------------- 3 files changed, 228 insertions(+), 219 deletions(-) create mode 100644 docs/ifaq.md diff --git a/docs/faq.md b/docs/faq.md index b72bdd70a..251bfb57d 100644 --- a/docs/faq.md +++ b/docs/faq.md @@ -9,6 +9,13 @@ to the [Scheme](./install-and-run.md#scheme-changes) and [Build Phases](./install-and-run.md#build-phase-changes) after running `pod install`, please re-run `pod install` again. +**I see lots of “XXX is implemented in both YYY and ZZZ. One of the two will be used. Which one is +undefined.” in the logs** + +This usually means that EarlGrey is being linked to more than once. Ensure that only the **Test Target** +depends on *EarlGrey.framework* and EarlGrey.framework is embedded in the app under test (i.e. *$TEST_HOST*) from the +test target's built products via a Copy File(s) Build Phase. + **How does EarlGrey compare to Xcode’s UI Testing?** EarlGrey is more of a [gray-box testing](https://en.wikipedia.org/wiki/Gray_box_testing) solution @@ -31,35 +38,9 @@ containers, regardless of the amount of scrolling required. the video frame?** For your tests to have the application properly scaled, make sure the app under test has correct -launch screen images present for all supported devices (see +launch screen images present for all supported devices (see [iOS Developer Library, Launch Files](https://developer.apple.com/library/ios/documentation/UserExperience/Conceptual/MobileHIG/LaunchImages.html)). -**Why does EarlGrey need to modify the test's scheme and add a Copy Files Build Phase?** - -EarlGrey synchronizes by keeping track of the app's internal state. It is essential that EarlGrey -therefore be embedded into the app. Since we do not want users to have EarlGrey directly link to -the app under test or create separate test rigs, we perform the embedding ourselves by adding a -Copy Files Build Phase that copies the *EarlGrey.framework* linked to the test target to the app -under test, as specified by the *$TEST_HOST* variable. - -Also, EarlGrey needs to be loaded before the app to ensure that we do not miss any states that -should have been tracked, along with giving EarlGrey fine-grained control of the test's execution. -For this purpose, we add a *DYLD_INSERT_LIBRARIES* environment variable in the test's scheme. - -**I get a crash with “Could not swizzle …”** - -This means that EarlGrey is trying to swizzle a method that it has swizzled before. It is a result -of EarlGrey being linked to more than once. Ensure that only the **Test Target** -depends on *EarlGrey.framework* and EarlGrey.framework is embedded in the app under test (i.e. *$TEST_HOST*) from the -test target's build phase. - -**I see lots of “XXX is implemented in both YYY and ZZZ. One of the two will be used. Which one is -undefined.” in the logs** - -This usually means that EarlGrey is being linked to more than once. Ensure that only the **Test Target** -depends on *EarlGrey.framework* and EarlGrey.framework is embedded in the app under test (i.e. *$TEST_HOST*) from the -test target's built products via a Copy File(s) Build Phase. - **Is there a way to return a specific element?** No, but there is a better alternative. Use [GREYActionBlock](../EarlGrey/Action/GREYActionBlock.h) @@ -103,11 +84,11 @@ open class Element { * GREYAssertTrue(element.text != "", reason: "get text failed") */ public func grey_getText(_ elementCopy: Element) -> GREYActionBlock { - return GREYActionBlock.action(withName: "get text", - constraints: grey_respondsToSelector(#selector(getter: UILabel.text))) { element, + return GREYActionBlock.action(withName: "get text", + constraints: grey_respondsToSelector(#selector(getter: UILabel.text))) { element, errorOrNil -> Bool in let elementObject = element as? NSObject - let text = elementObject?.perform(#selector(getter: UILabel.text), + let text = elementObject?.perform(#selector(getter: UILabel.text), with: nil)?.takeRetainedValue() as? String elementCopy.text = text ?? "" return true @@ -115,6 +96,13 @@ public func grey_getText(_ elementCopy: Element) -> GREYActionBlock { } ``` +**I get a crash with “Could not swizzle …”** + +This means that EarlGrey is trying to swizzle a method that it has swizzled before. It is a result +of EarlGrey being linked to more than once. Ensure that only the **Test Target** +depends on *EarlGrey.framework* and EarlGrey.framework is embedded in the app under test (i.e. *$TEST_HOST*) from the +test target's build phase. + **How do I check whether an element exists in the UI hierarchy?** If you are unsure whether the element exists in the UI hierarchy, pass an `NSError` to the diff --git a/docs/ifaq.md b/docs/ifaq.md new file mode 100644 index 000000000..559ba229d --- /dev/null +++ b/docs/ifaq.md @@ -0,0 +1,13 @@ +#IFAQ - Infrequently Asked Questions + +**Why does EarlGrey need to modify the test's scheme and add a Copy Files Build Phase?** + +EarlGrey synchronizes by keeping track of the app's internal state. It is essential that EarlGrey +therefore be embedded into the app. Since we do not want users to have EarlGrey directly link to +the app under test or create separate test rigs, we perform the embedding ourselves by adding a +Copy Files Build Phase that copies the *EarlGrey.framework* linked to the test target to the app +under test, as specified by the *$TEST_HOST* variable. + +Also, EarlGrey needs to be loaded before the app to ensure that we do not miss any states that +should have been tracked, along with giving EarlGrey fine-grained control of the test's execution. +For this purpose, we add a *DYLD_INSERT_LIBRARIES* environment variable in the test's scheme. diff --git a/docs/install-and-run.md b/docs/install-and-run.md index 6cfb3ee2c..5d41b3e6c 100644 --- a/docs/install-and-run.md +++ b/docs/install-and-run.md @@ -1,129 +1,134 @@ # Install and run This document shows you how to install EarlGrey and then how to set up and run your first -test. +test.M ## Install EarlGrey -You can add EarlGrey to Xcode projects in three ways: using CocoaPods, Carthage or manually through the Xcode Project. +You can add EarlGrey to Xcode projects in three ways: using [CocoaPods](#cocoapods-installation), [Carthage](#carthage-installation) or [Manually](#github-installation) through the Xcode Project. For EarlGrey, we highly recommend [CocoaPods](http://cocoapods.org/pods/EarlGrey) as the best way to get started. ### CocoaPods installation -#### Step 1: Set up a Unit target +#### Step 1: Set up a Unit Testing target -1. To create a new Unit Test (XCTest) target, select your project in the Xcode Project Navigator, -and then click **Editor → Add Target...** from the menu. -2. In the **Add Target** window, select **iOS** → **Test** → **iOS Unit Testing Bundle**: + 1. To create a new Unit Test (XCTest) target, select your + project in the Xcode Project Navigator, + and then click **Editor → Add Target...** from the menu. + 2. In the **Add Target** window, select **iOS** → **Test** + → **iOS Unit Testing Bundle**: - + -Click **Next** → **"Add a Test Target Name"** → **Finish**. + Click **Next** → **"Add a Test Target Name"** → **Finish**. -3. The test target must have a Scheme associated with it. To add one, go to **Product** → -**Scheme** → **Manage Schemes**, press the plus **(+)** sign, and then select the target from -the dropdown menu. Ensure the **Container** is set to the app under test. + 3. The test target must have a Scheme associated with it. + To add one, go to **Product** → + **Scheme** → **Manage Schemes**, press the plus **(+)** sign, and then select the target from + the dropdown menu. Ensure the **Container** is set to the app under test. - + -And click on **Close**. + And click on **Close**. #### Step 2: Add EarlGrey as a framework dependency -1. In the test target's section in your `Podfile`, add EarlGrey as a dependency. + 1. Install the EarlGrey gem by doing + `gem install earlgrey`. -```ruby -target TEST_TARGET do - project PROJECT_NAME + 2. In the test target's section in your `Podfile`, add + EarlGrey as a dependency. - use_frameworks! # Required for Swift Test Targets only - inherit! :search_paths # Required for not double-linking libraries in the app and test targets. - pod 'EarlGrey' -end -``` + ```ruby + target TEST_TARGET do + project PROJECT_NAME -2. Use the `configure_for_earlgrey` method from the [EarlGrey gem](https://rubygems.org/gems/earlgrey) -in your Podfile's `post_install` section as in below. + use_frameworks! # Required for Swift Test Targets only + inherit! :search_paths # Required for not double-linking libraries in the app and test targets. + pod 'EarlGrey' + end + ``` -```ruby -post_install do |installer| - require 'earlgrey' # Imports the EarlGrey gem for usage in your Podfile - configure_for_earlgrey(installer, PROJECT_NAME, TEST_TARGET, SCHEME_FILE) # EarlGrey Gem Call -end -``` + 3. Call the `configure_for_earlgrey` method from the + [EarlGrey gem](https://rubygems.org/gems/earlgrey) + in your Podfile's `post_install` section for each EarlGrey testing target and scheme. -3. Your complete Podfile when using EarlGrey with the latest CocoaPods should look like: + ```ruby + post_install do |installer| + require 'earlgrey' # Imports the EarlGrey gem for usage in your Podfile + configure_for_earlgrey(installer, PROJECT_NAME, TEST_TARGET, SCHEME_FILE) # EarlGrey Gem Call + end + ``` + The EarlGrey gem also copies the `EarlGrey.swift` file to your project directory if you're passing the `{swift: true}` parameter to the args for the configure_for_earlgrey() method. -```ruby -PROJECT_NAME = 'SampleApp' -TEST_TARGET = 'SampleAppTests' -SCHEME_FILE = 'SampleAppTests.xcscheme' + 4. Your complete Podfile when using EarlGrey with the latest + CocoaPods should look like: -target TEST_TARGET do - project PROJECT_NAME + ```ruby + PROJECT_NAME = 'SampleApp' + TEST_TARGET = 'SampleAppTests' + SCHEME_FILE = 'SampleAppTests.xcscheme' - use_frameworks! # Required for Swift Test Targets only - inherit! :search_paths # Required for not double-linking libraries in the app and test targets. - pod 'EarlGrey' -end + target TEST_TARGET do + project PROJECT_NAME -post_install do |installer| - require 'earlgrey' # Imports the EarlGrey gem for usage in your Podfile - configure_for_earlgrey(installer, PROJECT_NAME, TEST_TARGET, SCHEME_FILE) # EarlGrey Gem Call -end -``` + use_frameworks! # Required for Swift Test Targets only + inherit! :search_paths # Required for not double-linking libraries in the app and test targets. + pod 'EarlGrey' + end -4. Install the EarlGrey gem by doing `gem install earlgrey` and then do the `pod install`. + post_install do |installer| + require 'earlgrey' # Imports the EarlGrey gem for usage in your Podfile + configure_for_earlgrey(installer, PROJECT_NAME, TEST_TARGET, SCHEME_FILE) # EarlGrey Gem Call + end + ``` -Notes: -* Call the `configure_for_earlgrey` method for each target / Scheme. -* For compatibility between different versions, please see [this doc](https://github.com/google/EarlGrey/tree/master/docs/versions.md). -To download a particular version of the gem, use `gem install earlgrey -v x.y.z`. + Once finished, do a `pod install`. For compatibility between different versions, please see [this doc](https://github.com/google/EarlGrey/tree/master/docs/versions.md). To download a particular version of the gem, use `gem install earlgrey -v x.y.z`. -#### Step 3: Run the pod install command +#### Step 3: Open the workspace and verify that you see EarlGrey. -After you successfully run the `pod install` command, open the generated workspace and find EarlGrey -installed in the `Pods/` directory. The generated `Pods/` project should look similar to: + After you successfully run the `pod install` command, open the generated workspace and find EarlGrey + installed in the `Pods/` directory. The generated `Pods/` project should look similar to: - + ### Carthage Installation #### Step 1: Set up a test target for Carthage -See [Step 1 from the `CocoaPods installation`](#step-1:-set-up-a-unit-target) detailed above. + See [Step 1 from the `CocoaPods installation`](#step-1:-set-up-a-unit-target) detailed above. #### Step 2: Configure carthage -1. Install Carthage + 1. Install Carthage -`brew update; brew install carthage` + `brew update; brew install carthage` -2. Specify the version of EarlGrey to use in Cartfile.private. + 2. Specify the version of EarlGrey to use in Cartfile.private. -Note that you can also use "master" instead of a release tag. + Note that you can also use "master" instead of a release tag. -`echo 'github "google/EarlGrey" "1.2.0"' >> Cartfile.private` + `echo 'github "google/EarlGrey" "1.2.0"' >> Cartfile.private` -3. Update to latest EarlGrey revision and create Cartfile.resolved. + 3. Update to latest EarlGrey revision and create Cartfile.resolved. -`carthage update EarlGrey --platform ios` + `carthage update EarlGrey --platform ios` #### Step 3: Use the EarlGrey gem -1. Install the EarlGrey gem. + 1. Install the EarlGrey gem. -`gem install earlgrey` + `gem install earlgrey` -2. Use the gem to install EarlGrey into the testing target. + 2. Use the gem to install EarlGrey into the testing target. -`earlgrey install -t EarlGreyExampleSwiftTests` + `earlgrey install -t EarlGreyExampleSwiftTests` -Now you're ready to start testing with EarlGrey! -If you need more control, review the available installation options in the `earlgrey gem`: + Now you're ready to start testing with EarlGrey! + If you need more control, review the available installation options in the `earlgrey gem`: -`earlgrey help install` + `earlgrey help install` ### GitHub Installation @@ -131,90 +136,104 @@ In cases where CocoaPods is not compatible with your project, you can add EarlGr #### Step 1: Generate EarlGrey.framework -1. Download the source for EarlGrey and its dependencies. - * Download EarlGrey's source code from the [Latest Release](https://github.com/google/EarlGrey/releases/latest) + 1. Download the source for EarlGrey. + 1. Download EarlGrey's source code from the + [Latest Release](https://github.com/google/EarlGrey/releases/tag/1.8.0). - * Unzip and go to the `EarlGrey-x.y.z/EarlGrey` directory that contains **EarlGrey.xcodeproj**. + 2. Unzip and go to the `EarlGrey-x.y.z/EarlGrey` directory + that contains **EarlGrey.xcodeproj**. - * Open the **EarlGrey.xcodeproj** file and build the EarlGrey scheme. + 3. Open the **EarlGrey.xcodeproj** file. -Your EarlGrey folder structure should now look like this: + 4. Build the EarlGrey scheme. - + **Note:** As part of the initial build step, a script [**setup-earlgrey.sh**](https://github.com/google/EarlGrey/tree/master/Scripts/setup-earlgrey.sh) + will be run to download all the required dependencies. Without it, you might find dependencies + like `fishhook` and `OCHamcrest` shown as missing in the folder structure. Ensure that you are online for this step. -And your EarlGrey Project should look like this: + Your EarlGrey folder structure should now look like this: - + -2. As part of the initial build step, a script [**setup-earlgrey.sh**](https://github.com/google/EarlGrey/tree/master/Scripts/setup-earlgrey.sh) - will be run to download all the required dependencies. Without it, you might find dependencies - like `fishhook` and `OCHamcrest` shown as missing in the folder structure. + And your EarlGrey Project should look like this: + + #### Step 2: Add EarlGrey as a dependency of the project which contains your app under test -1. Close **EarlGrey.xcodeproj** so that it is no longer open in any Xcode window. Once closed, drag **EarlGrey.xcodeproj** from its directory into your App’s project or workspace in Xcode. To verify this, you should find `EarlGrey` in the list of targets of your app in Xcode: + 1. Close **EarlGrey.xcodeproj** so that it is no longer + open in any Xcode window. Once closed, drag **EarlGrey.xcodeproj** from its directory into your App’s project or workspace in Xcode. To verify this, you should find `EarlGrey` in the list of targets of your app in Xcode: - + -2. Add **EarlGrey.framework** as a dependency of your project’s Test Target: + 2. Add **EarlGrey.framework** as a dependency of your + project’s Test Target: -``` -**Project** → **Test Target** → **Build Phases** → **Link Binary With Libraries** → **+ (Add Sign)** → **EarlGrey.framework** -``` + ``` + **Project** → **Test Target** → **Build Phases** → **Link Binary With Libraries** → **+ (Add Sign)** → **EarlGrey.framework** + ``` -3. Add EarlGrey as a Target Dependency to the Test Target: + 3. Add EarlGrey as a Target Dependency to the Test Target: -``` -**Project** → **Test Target** → **Build Phases** → **Target Dependencies** → **+ (Add Sign)** → **EarlGrey** -``` + ``` + **Project** → **Test Target** → **Build Phases** → **Target Dependencies** → **+ (Add Sign)** → **EarlGrey** + ``` -The Test Target’s Build Phases should now look similar to this: + The Test Target’s Build Phases should now look similar to this: - + -4. Turn off Bitcode as it is not supported by EarlGrey (yet) by setting **Enable Bitcode** to **NO** in the Build Settings of the Test Target. + For Swift Users, download the correct + [`EarlGrey.swift`](https://github.com/google/EarlGrey/tree/master/gem/lib/earlgrey/files/) + file for your Swift version and include it in to your test bundle. EarlGrey + currently supports Swift 3.0 and 2.3. + 4. Turn off Bitcode as it is not supported by EarlGrey + (yet) by setting **Enable Bitcode** to **NO** in the Build Settings of the Test Target. -5. You must add environment variables in the Test Target's Scheme to inject the EarlGrey framework. To do so, go to **The Test Target → Edit Scheme → Test Action** and then deselect **Use the Run action's arguments and environment variables**. Add the following details in the `Environment Variables`: + 5. You must add environment variables in the Test Target's + Scheme to inject the EarlGrey framework. To do so, go to **The Test Target → Edit Scheme → Test Action** and then deselect **Use the Run action's arguments and environment variables**. Add the following details in the `Environment Variables`: -Key: `DYLD_INSERT_LIBRARIES` -Value:`@executable_path/EarlGrey.framework/EarlGrey` + Key: `DYLD_INSERT_LIBRARIES` + Value:`@executable_path/EarlGrey.framework/EarlGrey` -Make sure the `Expand Variables Based On` value points to the app under test. The Scheme should now look like this: + Make sure the `Expand Variables Based On` value points to the app under test. The Scheme should now look like this: - + -#### Step 3: Attempt to build the app under test +#### Step 3: Build the app under test -In Xcode, attempt to build the app under test. It should build without any errors. After EarlGrey is built, see the [Final Test Configuration](#final-test-configuration) section for additional customizations that you may need to get your tests to run. + In Xcode, build the app under test. It should build without any errors. After EarlGrey is built, see the [Final Test Configuration](#final-test-configuration) section for additional customizations. #### Step 4: Final Test Configuration -The EarlGrey tests are hosted from the application being tested. Make sure the test target is setup -correctly to launch the app under test: - -1. Under the **General** tab: - * **Host Application** is set to the app under test. + The EarlGrey tests are hosted from the application being tested. Make sure the test target is setup correctly to launch the app under test: -2. Under the **Build Settings** tab: - * **Test Host** points to your application, for example: - *$(BUILT_PRODUCTS_DIR)/.app/* where - ** must be replaced by the name of the app under test. - * **Bundle Loader** is set to *$(TEST_HOST)*. - * **Wrapper Extension** is set to *xctest*. -3. Add a **Copy Files** Build Phase to the Test Target to copy the EarlGrey framework to your app under test. -To do this, choose **Project → Test Target → Build Phases → + (Add Sign) → New Copy Files Phase**, and then -add the following details in the **Copy Files** phase: + 1. Under the **General** tab: + * **Host Application** is set to the app under test. -Destination: `Absolute Path` -Path: `$(TEST_HOST)/..` -Copy files only when installing: Deselect -Name: `Path to EarlGrey.Framework` with `Code Sign on Copy` selected. + 2. Under the **Build Settings** tab: + * **Test Host** points to your application, for example: + *$(BUILT_PRODUCTS_DIR)/.app/* where + ** must be replaced by the name of the app under test. + * **Bundle Loader** is set to *$(TEST_HOST)*. + * **Wrapper Extension** is set to *xctest*. + 3. Add a **Copy Files** Build Phase to the Test Target to + copy the EarlGrey framework to your app under test. + To do this, choose **Project → Test Target → Build Phases → + (Add Sign) → New Copy Files Phase**, and then + add the following details in the **Copy Files** phase: + ``` + Destination: `Absolute Path` + Path: `$(TEST_HOST)/..` + Copy files only when installing: Deselect + Name: `Path to EarlGrey.Framework` with `Code Sign on Copy` selected. + ``` + The Build Phases should now include: -The Build Phases should now include: + - + After the app under test is set up, you can use the Xcode **Test Navigator** to add new test classes and run them selectively, or together. -After the app under test is set up, you can use the Xcode **Test Navigator** to add new test classes and run them selectively, or together. + :sparkles:Hooray!!! You can now use EarlGrey in your tests!:sparkles: ## Set Up and run your first test @@ -223,91 +242,80 @@ Because EarlGrey is based on XCTest, creating your first test in Xcode is as eas Class**. **UI Test Case Class** uses the new UI Testing feature added to XCTest and isn’t yet compatible with EarlGrey. -1. **Ctrl+Click** the folder for your app's source files, and then select **New file...**. The -following dialog will appear: - - - -2. Select **Unit Test Case Class**, and then click **Next**. On the following screen, type the name -of your test case. For this example, let’s leave it as *MyFirstEarlGreyTest*: - - - -3. On the next screen, make sure that the test is associated with the Unit Test target. In this -case, our target is *SimpleAppTests*: - - - -4. Xcode will create a new test case for us but we won’t need much of it. Let’s change the code to -leave just a single test method and include the EarlGrey framework, like this: + 1. **Ctrl+Click** the folder for your app's source files, + and then select **New file...**. The + following dialog will appear: -```objc -@import EarlGrey; -#import + -@interface MyFirstEarlGreyTest : XCTestCase -@end + 2. Select **Unit Test Case Class**, and then click + **Next**. On the following screen, type the name + of your test case. For this example, let’s leave it as *MyFirstEarlGreyTest*: -@implementation MyFirstEarlGreyTest + -- (void)testExample { -// Your test actions and assertions will go here. -} + 3. On the next screen, make sure that the test is + associated with the Unit Test target. In this + case, our target is *SimpleAppTests*: -@end -``` + -5. Now let’s add a simple EarlGrey assertion that checks for the presence of a key window and -asserts that it is displayed. Here’s what the resulting test would look like: + 4. Xcode will create a new test case for us but we won’t + need much of it. Let’s change the code to + leave just a single test method and include the EarlGrey framework, like this: -```objc -- (void)testPresenceOfKeyWindow { -[[EarlGrey selectElementWithMatcher:grey_keyWindow()] -assertWithMatcher:grey_sufficientlyVisible()]; -} + For Swift Tests: -``` + ```swift + import EarlGrey -6. And that’s it! As with any other unit test, this test will show up in the test navigator, so you -can run it by just clicking on the **run** icon or by Ctrl+clicking the test name and then selecting -**Test "testPresenceOfKeyWindow"**. Because this is a regular unit test, you can place breakpoints in -your test and in your application code and use the built-in tools seamlessly. + class MyFirstEarlGreyTest: XCTestCase { -### Swift + func testExample() { + // Your test actions and assertions will go here. + } + } + ``` + For Objective-C Tests: -To use EarlGrey with Swift, include the `EarlGrey.swift` file in your Swift Test Project. The file -contains functions that implement C preprocessor macros that aren't available in Swift. There are -two ways to do so: + ```objc + @import EarlGrey; -#### Using the [earlgrey gem](https://rubygems.org/gems/earlgrey): -* We recommend uisng the EarlGrey gem to use EarlGrey with Swift. + @interface MyFirstEarlGreyTest : XCTestCase + @end -The EarlGrey gem also copies the `EarlGrey.swift` file to your project directory if you're -passing the `{swift: true}` parameter to the args for the `configure_for_earlgrey()` method as -in the below snippet. This is compiled into the tests along with EarlGrey. + @implementation MyFirstEarlGreyTest -```ruby -configure_for_earlgrey(installer, PROJECT_NAME, test_target, test_target + '.xcscheme', {swift: true}) -``` + - (void)testExample { + // Your test actions and assertions will go here. + } -EarlGrey currently supports Swift 3.0 and 2.3. While 3.0 is the default, you can also use 2.3 -by adding `{swift: true, swift_version: 2.3}`. + @end + ``` -* Manual installation: + 5. Now let’s add a simple EarlGrey assertion that checks + for the presence of a key window and + asserts that it is displayed. Here’s what the resulting test would look like: -In this case you need to download the correct -[`EarlGrey.swift`](https://github.com/google/EarlGrey/tree/master/gem/lib/earlgrey/files/) -file for your Swift version and include it in to your test bundle. + For Swift Tests: -Here is an example of a very basic EarlGrey test written in Swift: + ```swift + func testExample() { + EarlGrey.select(elementWithMatcher: grey_keyWindow()) + .assert(grey_sufficientlyVisible()) + } + ``` + For Objective-C Tests: -```swift -import EarlGrey // This adds EarlGrey.swift and the EarlGrey framework. + ```objc + - (void)testPresenceOfKeyWindow { + [[EarlGrey selectElementWithMatcher:grey_keyWindow()] + assertWithMatcher:grey_sufficientlyVisible()]; + } -// Select the button with Accessibility ID "clickMe" and assert it's visible. -EarlGrey.select(elementWithMatcher: grey_accessibilityID("ClickMe")) -.assert(grey_sufficientlyVisible()) -``` + ``` -> For compatibility with Swift, we use `grey_allOfMatchers()` and `grey_anyOfMatchers()` instead -of `grey_allOf()` and `grey_anyOf()` respectively. +6. And that’s it! As with any other unit test, this test will show up in the test navigator, so you +can run it by just clicking on the **run** icon or by Ctrl+clicking the test name and then selecting +**Test "testPresenceOfKeyWindow"**. Because this is a regular unit test, you can place breakpoints in +your test and in your application code and use the built-in tools seamlessly.