Git Product home page Git Product logo

ui-testing-cheat-sheet's Introduction

UI Testing Cheat Sheet

This repository is complementary code for my post, UI Testing Cheat Sheet and Examples. The post goes into more detail with example images for most examples.

The included project highlights working code with a simple Test Host. This was last updated for Swift 5 on Xcode 11.4.1.

Contents

Basic Functionality

Testing if an element exists

XCTAssert(app.staticTexts["Welcome"].exists)

Testing if text with an ellipsis exists

A full text match will find an element even if the displayed text has an ellipsis due to truncation.

let longNameCell = app.staticTexts["Adolph Blaine Charles David Earl Frederick Gerald Hubert Irvin John Kenneth Lloyd Martin Nero Oliver Paul Quincy Randolph Sherman Thomas Uncas Victor William Xerxes Yancy Wolfeschlegelsteinhausenbergerdorff, Senior"]
XCTAssert(longNameCell.exists) // displayed text is "Adolph Blaine Charles David Earl Freder..."

Waiting for an element to appear

"Waiting" is now built into XCTest.

let goLabel = app.staticTexts["Go!"]
XCTAssertFalse(goLabel.exists)

app.buttons["Ready, set..."].tap()
XCTAssert(goLabel.waitForExistence(timeout: 5))

Interacting with System Controls

Tapping buttons

Identify buttons by their accessibility label.

app.buttons["Add"].tap()

Typing text

First make sure the text field has focus by tapping on it.

let textField = app.textFields["Username"]
textField.tap()
textField.typeText("joemasilotti")

Dismissing alerts

app.alerts["Alert Title"].buttons["Button Title"].tap()

Dismissing action sheets

app.sheets["Sheet Title"].buttons["Button Title"].tap()

Handling system alerts

Present a location services authorization dialog to the user and dismiss it with the following code.

Before presenting the alert add a UI Interruption Handler. When this fires, dismiss with the "Allow" button.

addUIInterruptionMonitor(withDescription: "Location Services") { (alert) -> Bool in
  alert.buttons["Allow"].tap()
  return true
}

app.buttons["Request Location"].tap()
app.tap() // need to interact with the app again for the handler to fire

Sliding sliders

This will slide the value of the slider to 70%.

app.sliders.element.adjust(toNormalizedSliderPosition: 0.7)

Interacting with pickers

A picker with one wheel:

app.pickerWheels.element.adjust(toPickerWheelValue: "Picker Wheel Item Title")

A picker with multiple wheels. Make sure to set the accessibility delegate so the framework can identify the different wheels.

let firstPredicate = NSPredicate(format: "label BEGINSWITH 'First Picker'")
let firstPicker = app.pickerWheels.element(matching: firstPredicate)
firstPicker.adjust(toPickerWheelValue: "first value")

let secondPredicate = NSPredicate(format: "label BEGINSWITH 'Second Picker'")
let secondPicker = app.pickerWheels.element(matching: secondPredicate)
secondPicker.adjust(toPickerWheelValue: "second value")

Tapping links in web views

app.links["Tweet this"].tap()

Interactions

Verifying the current controller's title

XCTAssert(app.navigationBars["Details"].exists)

Reordering table cells

If you have a UITableViewCell with default style and set the text to "Title", the reorder control's accessibility label becomes "Reorder Title".

Using this we can drag one reorder control to another, essentially reordering the cells.

let topButton = app.buttons["Reorder Top Cell"]
let bottomButton = app.buttons["Reorder Bottom Cell"]
bottomButton.press(forDuration: 0.5, thenDragTo: topButton)

XCTAssertLessThanOrEqual(bottomButton.frame.maxY, topButton.frame.minY)

Pull to refresh

Create a XCUICoordinate from the first cell in your table and another one with a dy of six. Then drag the first coordinate to the second.

let firstCell = app.staticTexts["Adrienne"]
let start = firstCell.coordinate(withNormalizedOffset: (CGVectorMake(0, 0))
let finish = firstCell.coordinate(withNormalizedOffset: (CGVectorMake(0, 10))
start.press(forDuration: 0, thenDragTo: finish)

Pushing and popping view controllers

Test if a view controller was pushed onto the navigation stack.

app.buttons["More Info"].tap()
XCTAssert(app.navigationBars["Volleyball?"].exists)

Pop a view controller by tapping the back button in the navigation bar and assert that the title in the navigation bar has changed.

app.navigationBars.buttons.elementBoundByIndex(0).tap()
XCTAssert(app.navigationBars["Volley"].exists)

ui-testing-cheat-sheet's People

Contributors

dasdom avatar glennposadas avatar joemasilotti avatar krausefx avatar leopic avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

ui-testing-cheat-sheet's Issues

testRefreshControl doesn't seems to work

It doesn't seems like the testRefreshControl is working. The alert isn't getting dismissed, and the table is not returning back to the start position after pulling down.

Forcing system dialogs to appear on every run

In func testDismissingASystemAlert() {...} there is code sample on how to write test for case when system dialog appears (Location Services permission dialog). This code works fine only if run once for the first time on particular device. Reruns are not triggering system dialog to appear (which is understandable - this is how system works).
But this means I can only have one test to see what happens with my app (in this test it is tapping Allow button). There are at least two more cases (besides tapping Allow button) which can be tested: first one is, of course, tapping Don't Allow button; second one is pressing Home button.
Is it possible to rewrite this test so that system dialog appears every time a test is being run?

Localization

I'm working on using Fastlane:Snapshot, and this projects is a great guide. But I am wondering how to target alerts in different languages when the text or button text isn't the same. IE,
app.alerts["You won!"].buttons["Awesome!"].tap()
or
addUIInterruptionMonitorWithDescription("Location Services") { (alert) -> Bool in alert.buttons["Allow"].tap() return true }
Especially considering the system alert buttons might not be the same translation even if I have NSLocaliazedString...

Simulate a Double Press on Home Button

I'm trying to simulate double pressing the home button to launch the so-called app switcher. However, it looks like - (void)pressButton:(XCUIDeviceButton)button; is not synchronous and calling

[[XCUIDevice sharedDevice] pressButton:XCUIDeviceButtonHome];

would launch the home screen any way regardless of how quick the subsequent press follows. Any idea or tips on how to accomplish this?

Question - Do you have a version for Xcode 8 and Swift 3

Hi Joe,

Thank you for your great UI Testing Cheat Sheet. I am trying to converting it to Xcode 8 but failed with many error in syntax.

Do you have a version of this app for Xcode 8 and Swift 3 ready? If yes, could you share it.

Thanks again,
Dat

Wrong argument label for one line of sample code

The is a small coding error:

expectation(for: exists, evaluatedWithObject: nextGame, handler: nil)

This create a 'Argument labels do not match any available overloads' error. The correction is as follows:

expectation(for: exists, evaluatedWith: nextGame, handler: nil)

testElementExists() fails unexpectedly

I have run the tests provided within the project.

The fix is trivial, the element being checked is a staticText, but should actually be the navigationBar.

How to test specific view controller

It seems all of the tests you write must start from the beginning, I want to write a class for testing specific view controller, without any action on another screen, Is there any solution?

Webview Test Elements

Hello! I think it's not the best place to ask this question but I was trying to figure out the issue.
I was testing (XCUITest) a Webview (Load an Angular Page). I ran the test with this code line:

let propertiesButton = app.webViews.otherElements.element(boundBy: 3).firstMatch
if propertiesButton.exists {
propertiesButton.tap()
}

Properties Window opened fine only in Simulator iPhone 11 - 13.1. If I run the same test with another Simulator like iPhone 11 Pro - 13.1 or 13.3 the tap() doesn't work.

Also this line: app.swipeUp() when the Properties Window is opened, only works in Simulator iPhone 11 - 13.1

I'm not sure if the issue will be from how the web view render the Angular page (CSS).
Does anybody have or had the same issue?

Finding Search Bar embedded inside a navigation bar in iOS 11.

This repo has been exceptionally helpful. Thanks for making this.

I've been running into a pickle especially while testing search on VCs in iOS 11 where the search bar is embedded inside a navigation bar. Neither app.searchFields or any child element under app.navigationBar reveal the relevant XCUIElement.

Is there a workaround for this?

Question - Do you run all of the tests at one time or run them individually

Thanks so much for the information you provide on UI Testing and XCode 7. I have found it very useful in helping me learn how to write UI Tests.

I have seen several notes that tests should be stand alone. I downloaded this Volley project and if I run the tests individually - one at a time, they all work. But If I try to run all of them starting at the top, only one or two of them will pass.

So would it be better to run the tests one at a time in general?

Thanks

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.