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

编程入门 行业动态 更新时间:2024-10-28 00:20:11
本文介绍了使用 XCTest 测试 iOS 应用程序时检查 XCUIElement 在屏幕上的位置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

限时送ChatGPT账号..

我正在测试一个 iOS 应用程序,目前我正在使用 isHittable 检查特定 XCUIElement 的存在.

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

我想知道我们是否还可以检查 XCUIElement 在视图上的位置.例如,如果我们在视图的右下角有一个按钮,我们是否可以使用 XCTest 框架检查它是否真的在右下角?

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?

我查看了 XCTest 框架的 Apple 文档,但没有得到任何线索.任何帮助将不胜感激.

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

推荐答案

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.

这篇关于使用 XCTest 测试 iOS 应用程序时检查 XCUIElement 在屏幕上的位置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

更多推荐

[db:关键词]

本文发布于:2023-04-24 10:22:49,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1061439.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:应用程序   位置   测试   屏幕上   XCTest

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!