3

I need the detailed information about coordinate(withNormalizedOffset:) method.

The information provided in the Apple's docs are very minimal.

This method is all about picking a specific point in the window. But how can I define dx & dy points for a specific point?

Consider the four corners of the window. Top Left, Top Right, Bottom Left, Bottom Right. Can someone provide details on this?

  • What is mean by normalizedOffset?
  • What is dx & dy and their range?
Confused
  • 3,846
  • 7
  • 45
  • 72

2 Answers2

9

Range of dx & dy is 0 to 1 allowing float values. Normalized Offset is the x and y coordinates w.r.to the width and height of the object respectively.

Say we have an object of width 245 and height 432, and we need coordinate (10, 34) on it. The normalized offset is (10 / 245, 34 / 432) i.e. (0.040816327, 0.078703704) where the actual offset is (10, 34).

  • They should've just said this in the documentation. The current official description is not helpful at all. – AlanSTACK Feb 03 '23 at 05:35
0

The example given in the answer of Prakash Saravanan is correct. However, the range of dx and dy is not limited from 0 to 1. The docs say

The coordinate’s screen point is computed by adding normalizedOffset multiplied by the size of the element’s frame to the origin of the element’s frame.

Assume that you want to drag in a UI test an XCUIElement element to a different position on the screen. You could use e.g.

let start  = element.coordinate(withNormalizedOffset: CGVector(dx: 0.5, dy: 0.5))
let finish = element.coordinate(withNormalizedOffset: CGVector(dx: 0.5, dy: 10.0))
start.press(forDuration: 1.0, thenDragTo: finish)  

start is then the center of the element, and this center is then dragged after a long press of 1 sec duration to finish, where finish is vertically far outside of the element.

Reinhard Männer
  • 14,022
  • 5
  • 54
  • 116