Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Check the position of the XCUIElement on screen while testing iOS Application using XCTest

I am testing an iOS Application and currently I am checking the existence of a particular XCUIElement using isHittable.

I wanted to know if we can also check the position of the XCUIElement on the view. For instance, if we have a button in the bottom right corner of the view, can we check if it is actually in the bottom right corner using XCTest framework?

I had a look at the Apple Documentation for XCTest framework but did not get any clue. Any help will be greatly appreciated.

like image 856
Kushal Jogi Avatar asked Jan 01 '17 22:01

Kushal Jogi


People also ask

What is xcuitest in Xcode?

Xcode comes with the XCUITest framework which allows you to easily develop UI tests that reflect users' interaction with the application. Find out how to kick off with the XCUITest framework to grow your automated UI test suite.

How do I run unit tests in Xcode?

There are couple of ways to run your tests. 1.In Xcode go to Product -> Test (cmd+u). This will run all the tests in the project including unit tests. 2.If you want to run only the UI tests go to Test navigator.

How to record a test in Xcode?

To kick off recording the tests go to your test method and hit the red dot button placed next to the Debug area. Once you press the button, your application should be launched on simulator or device depending which one you have selected in Xcode.

What is xcuielement on iOS?

On iOS, XCUIElement provides gestural interactions, such as tapping, long pressing, swiping, pinching, and rotating. XCUIElement adopts the XCUIElementAttributes protocol, which provides additional properties for querying the current state of a UI element's attributes.


1 Answers

XCUIElement has a property frame which you can use to find the coordinates of the element in question.

let button = XCUIApplication().buttons["someButton"]
let frame = button.frame
let xPosition = frame.origin.x
let yPosition = frame.origin.y

There are other ways of retrieving different points relative to the frame, which is a CGRect, such as midX and midY, depending on how you want to assert the position of the element.

You should be aware that XCTest is a functional UI testing framework, and if you are using it for asserting the position of an element, the position will probably be different per device/simulator which may make your tests brittle.

like image 74
Oletha Avatar answered Sep 30 '22 15:09

Oletha