App & Browser Testing Made Easy

Give your users a seamless experience by testing on 3000+ real devices and browsers. Don't compromise with emulators and simulators

Get Started free
Home Guide How to scroll to element in XCUITest

How to scroll to element in XCUITest

By Suparna K, Community Contributor -

The need for mobile automation has risen drastically over the last few decades due to an increase in the rise of mobile users globally. 

Android & iOS being the global leaders in the mobile market, overrules the heavy need for quality delivery of seamless and top-performing mobile apps on the market. It not only provides a great user-friendly experience to users but also, reaches the market before its competitors for many top companies. 

While there are many automation tools in the market, nothing beats Apple’s very own XCTest framework. XCUITest is one of the most promising, efficient, reliable, and fastest automation tools in the iOS world. 

Let’s go ahead and explore how to scroll to elements in XCUITest, in order to test the iOS apps.

What does scroll to an element mean?

Mobile users, often find swipe down with their fingers to read/access the sections that are not visible in the current displayed area of the screen. The swiping gesture to reach an element across the length of the page as seen below is called Scrolling to an element.

Scroll down in App Store

Scroll down in App Store

Scrolling down using XCUITest

You can scroll or swipe down in an app without any constraints as well as conditions. It can be a generic scroll/swipe as well as a scroll with a condition. 

Let us go through an example where we just scroll down generally in an app:

Code to Scroll down Once using XCUITest 

func test_ScrollDownInApp() {

     appStore.swipeUp()  

  }

Running XCUITest Test on Real iOS Device

Here’s how running an XCUITest on a Real iOS device would look like.

Scroll Down OnceScroll Down Once

Locate Elements using BrowserStack App Live – Inspect Beta

Before scrolling or swiping down an app, it is a good practice to locate elements in the app and pinpoint the destination of the scroll. 

Debugging apps for locating elements before scrolling is important. One easiest way to inspect iOS app elements easily is to locate elements using BrowserStack App Live – Inspect Beta. BrowserStack App Live is a real device cloud where you can access various Android and iOS devices and run tests on them.

Locate elements in the app

Try BrowserStack App Live for Free

How to Scroll down to Element using XCUITest 

In the below piece of code, we would notice that we are scrolling till an element is found without any set limit with any certain number of scrolls. 

Approach 1 – Generic Scroll/Swipe down 

func test_FootBall_AppTap_In_AppStore() {

        

        appStore.launch()

        

        let gameRequired = "eFootball™ 2022, Authentic football game!"

        let footballLabel = appStore.buttons[gameRequired]

        

        while !(footballLabel.exists) {

            appStore.swipeUp()

        }




        XCTAssertTrue(footballLabel.isHittable)

  }

Running XCUITest with Generic Scroll on Real iOS Device

Scroll Down till Element is foundScroll Down till Element is found

Drawback of Generic Scroll

If the element is not found, there is a possibility of the swipe to enter a loop until the test fails, leading to unwanted wastage of time and execution. Hence, we need a better approach, where the number of scrolls needs to be defined to handle such unforeseen situations. 

Approach 2 – Pre-defined Scroll/Swipe down

Here, the number of scrolls is between 0 & 10 depending upon where it finds the element sooner. So if the element is found in the 5th scroll, it will scroll only 5 times. If an element is not found within 10 scrolls, then the test fails.

 func testScrollMaxTime() {

        

        let gameRequired = "WWE Mayhem, No1. WWE Arcade Action Game"

        

        appStore.launch()

        appStore.tabBars.buttons["Games"].tap()

                

        let lastCell = appStore

            .scrollViews

            .cells[gameRequired]

            .firstMatch




        let maxScrolls = 10

        var count = 0

        while lastCell.isHittable == false && count < maxScrolls {

            appStore.swipeUp()

            count += 1

        }

        

        let buttonLabel = appStore

            .buttons[gameRequired]

            .firstMatch

            .label

        

        XCTAssertEqual(gameRequired, buttonLabel)

    }

Running XCUITest with Approach 2 on Real iOS Device

Scroll Down with Defined ConditionScroll Down with Defined Condition

It is always recommended to write test scripts with handled scenarios for errors and exceptions. Bear in mind to write tests that not only pass but also fail effectively, in case of an error/issue. 

Conclusion

For accurate test results, it is always recommended to run tests on real devices, so that the real user conditions can be taken into account. Moreover, since different devices have different screen sizes, hence in the case of pre-defined scroll the number of scrolls might be altered based on the device mode. Hence, try testing on real devices to get accurate results and deliver a consistent user experience.

BrowserStack App Live, allows QAs to test their mobile apps on real devices instead of emulators or simulators under real-world scenarios. While BrowserStack App Automate, allows QAs to run Automated tests on XCUITests for better performance. Test results can be viewed on App Automate Dashboard once the test execution is completed. By clicking on individual tests will give you a detailed report for each test including steps, text logs, Appium logs, execution video logs, and other details for better debugging of failed tests.

Try BrowserStack App Automate

Tags
Manual Testing Mobile App Testing

Featured Articles

Appium vs XCUITest : Key Differences

Getting Started with XCUITest : UI Automation Framework on iOS

App & Browser Testing Made Easy

Seamlessly test across 20,000+ real devices with BrowserStack