Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PageControl in Xcode UITest

I have an app where a Page Control is used to indicate multiple of screens. When the user taps the page control (for example: to the right the of the current selected page), the scroll view scrolls to the following screen. It works fine.

I wanted to write a UI Test for this scenario. I found out that I cannot tap a specific page control "dot" to trigger that action. What I can check is that the Page Control exists and the current selected page.

Another issue is that even if I am able to do so, how can I check that the scroll view has scrolled already? I can only access the frame of the scroll view, not its bounds.

I have just started using Xcode UITest and it's letting me down. I thought it would be powerful by now (it was introduced by Xcode 7 and it was available before as UI Automation), but it seems it's not yet.

I will revert back to Unit Testing + manual functional testing. If anyone has other thoughts, please share it.

like image 725
Abdalrahman Shatou Avatar asked Jan 29 '23 06:01

Abdalrahman Shatou


1 Answers

Don't give up so soon, Xcode UITest can be frustrating at times but once you figured out what tricks to use, they are quite powerful:

You test your UIPageControl like this:

func testPageIndicator() {
    let app = XCUIApplication()
    app.launch()

    let scrollView = app.scrollViews.element(boundBy: 0)
    let pageIndicator = app.pageIndicators.element(boundBy: 0)

    // test that page indicator is updated when user scrolls to page 2
    scrollView.swipeLeft()
    XCTAssertEqual(pageIndicator.value as? String, "page 2 of 3")

    // test that scrollview scrolls to page 3 when user taps right of the current page in page indicator
    pageIndicator.coordinate(withNormalizedOffset: CGVector(dx: 0.9, dy: 0.2)).tap()
    XCTAssert(app.otherElements["page_3"].waitForExistence(timeout: 2))
    XCTAssertFalse(app.otherElements["page_2"].exists)
    XCTAssertEqual(pageIndicator.value as? String, "page 3 of 3")
}

You can tell the test where to tap on a UIElement by using the coordinate method. You hand a CGVector object to that method that describes where to tap.

For example a CGVector(dx: 0.9, dy: 0.2) tells the test to tap the element at the point that lies at 90% of its width and 20% of its height.

You can use this to tap left of right of the current page in the UIPageIndicator(see above code example)

To test if you are on the correct page in the UIScrollView you can set each pages accessibilityIdentifier to a unique string and then assert that the UIElement with that identifier exists after you tapped the page control.

In my case the pages are 3 UIViews. I set their accessibilityIdentifier to "page_1", "page_2" and "page_3" and then assert that "page_3" exists and "page_2" doesn't.

like image 134
joern Avatar answered Jan 31 '23 22:01

joern