且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

使用 XCTest 测试 iOS 应用程序时检查 XCUIElement 在屏幕上的位置

更新时间:2022-06-22 02:47:28

XCUIElement 有一个属性 frame 可用于查找相关元素的坐标.

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

还有其他方法可以检索相对于框架的不同点,这是一个 CGRect,比如midXmidY,取决于你想如何断言元素的位置.

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.

您应该知道 XCTest 是一个功能性 UI 测试框架,如果您使用它来断言元素的位置,则每个设备/模拟器的位置可能会有所不同,这可能会导致你的测试很脆弱.

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.