Showing posts with label iOS. Show all posts
Showing posts with label iOS. Show all posts

Thursday, 30 July 2020

Xcode UI tests: saveScreenshot(...) extension function

Taking a screenshot as part of an Xcode UI test and adding the screenshot to the test's output is a five-step shuffle as follows:

let screenshot = XCUIApplication().screenshot()
let attachment = XCTAttachment(screenshot: screenshot)
attachment.lifetime = .keepAlways
attachment.name = "SomeName"
add(attachment)

Wouldn't it be great if this could be reduced to a single line call as follows:

XCUIApplication().saveScreenshot(to: self, named: "SomeName")

To make this possible, all you need to do is add the following extension function to your UI testing bundle:

extension XCUIScreenshotProviding {
    
    func saveScreenshot(to activity: XCTActivity, named name: String) {
        let attachment = XCTAttachment(screenshot: screenshot())
        attachment.lifetime = .keepAlways
        attachment.name = name
        activity.add(attachment)
    }
}
You can find this extension function and others like it in the XCTestExtensions repo.

Saturday, 25 July 2020

Xcode UI tests: waitForNonExistence(...) extension function

The XCUIElement.waitForExistence(timeout:) function is great for waiting on an element to appear whilst an animation or asynchronous operation completes. However, there's equally a need at times to wait for an element to disappear and, sadly, XCUIElement does not offer such a function. Good news though! It turns out that it's not difficult at all to compose an extension function on XCUIElement which does exactly this, as follows:

extension XCUIElement {

    /**
     * Waits the specified amount of time for the element’s `exists` property to become `false`.
     *
     * - Parameter timeout: The amount of time to wait.
     * - Returns: `false` if the timeout expires without the element coming out of existence.
     */
    func waitForNonExistence(timeout: TimeInterval) -> Bool {

        let timeStart = Date().timeIntervalSince1970

        while (Date().timeIntervalSince1970 <= (timeStart + timeout)) {
            if !exists { return true }
        }
 
        return false
    }

You can find this extension function and others like it in the XCTestExtensions repo.

Friday, 11 October 2019

iOS: Natural text alignment doesn't mean what you think it means

So it turns out that natural text alignment in iOS doesn't mean "left-align left-to-right text and right-align right-to-left text" as it does in Android. For example, if I put English text in a UILabel I'd expect it to be left-aligned and if I put Arabic text in a UILabel I'd expect it to be right-aligned. But no. This is not how it works in iOS. Instead natural text alignment in iOS means "left-align text if the device's language is set to a left-to-right language and right-align text if the device's language is set to a right-to-left language". So, for example, if I set the device's language to English then the text in the UILabel will be left-aligned regardless of its content and if I set the device's language to Arabic then the text in the UILabel will be right-aligned regardless of its content.

For a super simple iOS application which demonstrates this see here.

Friday, 9 August 2019

Quran SDK (library) for iOS

I mentioned a good while previously that I'd extracted code out from my Hifdh Tracker and Hifdh Tester Android apps into a Quran SDK for Android. Well I've just started porting these apps to iOS and as a first step I've ported the Quran SDK for Android to its iOS equivalent. The Android and iOS SDKs are essentially Kotlin and Swift (respectively) wrappers around the Quran database which make it super easy to get data out of the database. You can find the repository for the SDKs here. The README in the repository explains how to incorporate and make use of the SDKs in your own Android and iOS projects.