使用 UIAutomation 从文本字段中获取值

发布于 2024-12-18 04:03:00 字数 963 浏览 0 评论 0原文

我试图了解如何使用 UIAutomation 来执行自动应用程序测试,因此我创建了一个可以从 JavaScript 界面驱动的温度转换器应用程序。这是 UI:

在此处输入图像描述

顶部文本字段具有“摄氏度”的辅助功能标签,类似地,底部字段具有“华氏度”作为其辅助功能标签。我试图通过此脚本来驱动它:

UIALogger.logStart("Test -40ºC == -40ºF");
var window = UIATarget.localTarget().frontMostApp().mainWindow();
var celsiusField = window.textFields()["Celsius"];
var fahrenheitField = window.textFields()["Fahrenheit"];
var convertButton = window.buttons()["Convert"];

celsiusField.setValue("-40");
convertButton.tap();
var fahrenheitValue = fahrenheitField.value();
if (fahrenheitValue == "-40.0") {
    UIALogger.logPass("-40C == -40F");
} else {
    UIALogger.logFail("-40C == " + fahrenheitValue + "F");
}

这会将摄氏字段中的文本正确设置为“-40”,并且当点击按钮时,应用程序会将华氏温度字段中的文本更新为“-40.0”。但是,fahrenheitValue 的值为“Fahrenheit”(文本字段的名称/标签),因此测试失败。我想知道这是否可能是一个计时问题,因此在点击后延迟一秒,这并没有改变行为。

是否可以从文本字段中获取文本并将其与预期值进行比较?

I'm trying to understand how UIAutomation can be used to perform automatic application tests, so I've created a temperature converter app that can be driven from the JavaScript interface. Here's the UI:

enter image description here

The top text field has an Accessibility label of "Celsius", similarly the bottom field has "Fahrenheit" as its Accessibility label. I'm trying to drive it through this script:

UIALogger.logStart("Test -40ºC == -40ºF");
var window = UIATarget.localTarget().frontMostApp().mainWindow();
var celsiusField = window.textFields()["Celsius"];
var fahrenheitField = window.textFields()["Fahrenheit"];
var convertButton = window.buttons()["Convert"];

celsiusField.setValue("-40");
convertButton.tap();
var fahrenheitValue = fahrenheitField.value();
if (fahrenheitValue == "-40.0") {
    UIALogger.logPass("-40C == -40F");
} else {
    UIALogger.logFail("-40C == " + fahrenheitValue + "F");
}

This correctly sets the text in the Celsius field to "-40", and when the button is tapped, the app updates the text in the Fahrenheit field to "-40.0". However, fahrenheitValue has the value "Fahrenheit" (the name/label of the text field), so the test fails. I wondered whether this was maybe a timing issue, so put in a one-second delay after the tap, which didn't change the behaviour.

Is it possible to get the text out of the text field and compare it with the expected value?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

忘你却要生生世世 2024-12-25 04:03:00

它变得“华氏度”是因为它是辅助功能标签还是因为它是占位符文本?

看起来 value() 可能会返回占位符文本。一种解决方法可能是在用户开始键入时修改占位符值,将其设置为用户输入的文本,然后在删除时将其恢复为华氏度?我假设即使您调用 setValue,它也会像调用 UITextField 委托方法时一样起作用?

Is it getting "Fahrenheit" because that's the accessibility label or because it's the placeholder text?

It looks like value() might return the placeholder text. One workaround could be to modify the placeholder value once the user starts typing to set it to be the text the user entered and then reverting it to Fahrenheit when they delete? I assume that even when you call setValue it acts as it would when the UITextField delegate methods get called?

幽梦紫曦~ 2024-12-25 04:03:00

您可以向视图添加标签、更新它并读出它。

You could add a label to your view, update it and read it out.

∞梦里开花 2024-12-25 04:03:00

尝试连接辅助功能检查器显示 UIAutomation 在您的应用程序中“看到”的内容。由于您在应用程序中设置值的方式,它可能正在读取一些过时的值。

刷新值的一种方法是发送更新事件:

UIAccessibilityPostNotification(UIAccessibilityLayoutChangedNotification, nil);

Try hooking up the Accessibility Inspector to show what UIAutomation "sees" in your app. It might be that it's reading some stale values because of the way you set the values in your app.

One way of refreshing the values is to send an update event:

UIAccessibilityPostNotification(UIAccessibilityLayoutChangedNotification, nil);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文