Skip to content

Instantly share code, notes, and snippets.

@devnano
Last active February 22, 2023 08:01
Show Gist options
  • Save devnano/6d234298da013886c32e5e4adf2152e1 to your computer and use it in GitHub Desktop.
Save devnano/6d234298da013886c32e5e4adf2152e1 to your computer and use it in GitHub Desktop.
iOS Today Widget – Smoke Test: verify that it is added and loaded properly from Today View.
//
// WeeklyCalendarUITests.swift
// WorkRosterAppUITests
//
// Created by Mariano Heredia on 06/05/2020.
import XCTest
class TestAddTodayWidget: XCTestCase {
override func setUp() {
// Put setup code here. This method is called before the invocation of each test method in the class.
// In UI tests it is usually best to stop immediately when a failure occurs.
continueAfterFailure = false
}
override func tearDown() {
// Put teardown code here. This method is called after the invocation of each test method in the class.
}
// This is working for iOS 13 and 12. 11 and 10 might need further tweaks.
func testAddTodayWidget() {
let app = XCUIApplication()
let springboard = XCUIApplication(bundleIdentifier: "com.apple.springboard")
var maxSwipeToTodayScreenAttempts = 5
var editButton: XCUIElement
if #available(iOS 13, *) {
editButton = springboard.buttons["Edit"]
} else {
// handle edit button query differently for iOS 12
editButton = springboard.scrollViews.otherElements.scrollViews["WGMajorListViewControllerView"].otherElements.buttons["Edit"]
}
// Insert <your app name here> is the expected button in the widget edition table
let insertRosta = springboard.tables.buttons["Insert Rosta"]
// Your own app element that you wanna check is there when the widget is added
let targetWidgetUIElement = springboard.buttons["Next month"]
let existsPredicate = NSPredicate(format: "exists == 1")
// 0. Launch the app
app.launch()
// 1. Leave the app and go to the springboad screen
XCUIDevice.shared.press(XCUIDevice.Button.home)
// 2. Head to today screen
// Not sure how many springboard screens the today screen is
// Try swipping n times to get there – edit button should exist
while(!editButton.exists && maxSwipeToTodayScreenAttempts > 0) {
springboard.swipeRight()
maxSwipeToTodayScreenAttempts = maxSwipeToTodayScreenAttempts - 1
}
// 3. Add our extension if it's not already there
// 3.1 Tap the edit widgets button
editButton.tap()
// 3.2 if the button to add our widget is there, just tap it.
// otherwhise we assume is already added.
if(insertRosta.exists) {
insertRosta.tap()
}
// 3.3. Finish the widgets edition
springboard.buttons["Done"].tap()
// 4. Wait for our target UI piece to be shown
self.expectation(for: existsPredicate, evaluatedWith: targetWidgetUIElement)
waitForExpectations(timeout: 5)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment