单击键盘“下一步”;使用 UIAutomation 的 key
我的应用程序中有一个搜索字段,并且我已将此字段的键盘返回键类型设置为 UIReturnKeyNext
。我正在尝试编写一个 UIAutomation 测试,使用以下行单击键盘上的 Next 按钮:
UIATarget.localTarget().frontMostApp().mainWindow().keyboard().keys().firstWithName("next");
此调用失败,因为未找到名为“next”的键。我已经使用以下方法转储了应用程序中的所有元素:
UIATarget.localTarget().frontMostApp().logElementTree();
这表明键盘中确实有一个名为“next”的键,但不知何故,我尝试检索它(如上所示)仍然失败。不过,我可以使用此方法检索其他密钥(例如字母“u”的密钥)。这里有已知问题还是我做错了什么?
我尝试过其他变体,但没有成功:
UIATarget.localTarget().frontMostApp().mainWindow().keyboard().elements()["next"];
这是我的 UIAKeyboard 中元素的屏幕截图:
I have a search field in my app and I have set the return key type of the keyboard for this field to UIReturnKeyNext
. I am attempting to write a UIAutomation test that clicks the Next button on the keyboard using the following line:
UIATarget.localTarget().frontMostApp().mainWindow().keyboard().keys().firstWithName("next");
This call is failing because the key with name 'next' is not being found. I have done a dump of all of the elements in my app using:
UIATarget.localTarget().frontMostApp().logElementTree();
This reveals that there is indeed a key in the keyboard with name 'next', but somehow my attempt to retrieve it as show above still fails. I can however retrieve other keys (like the key for the letter 'u') using this method. Is there a known issue here or am I doing something wrong?
I've tried other variations with no luck:
UIATarget.localTarget().frontMostApp().mainWindow().keyboard().elements()["next"];
Here is a screen capture of the elements in my UIAKeyboard:
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
Jelle 的方法对我有用。但如果有人需要的话,我也找到了另一种方法。
您可以在其中将 XCUIApplication() 创建为每个 UI 测试会话上的单例。这种方法的特点是,您现在可以区分
return
和done
以及其他变体,甚至检查它们是否存在。你可以额外做一些像下面这样的事情:
你可以像这样使用它:
Jelle's approach worked for me. But I also found an alternative way if anybody needed it.
Where you can create XCUIApplication() as a singleton on each UI Test session. The thing about this approach is you can now distinguish between
return
anddone
and other variants and even check for their existence.You can go extra and do something like following:
And you can use it like:
如果您只想单击它,并且您知道键盘将“下一个”作为“返回键”(在您的笔尖中定义),那么您可以使用以下命令:
If you just want to click it, and you know the keyboard has "next" as "Return key" (defined in your nib), then you can use this:
我没有要测试的示例,但由于“下一步”按钮是 UIAButton,而不是 UIAKey,您可以尝试:
如果它不起作用,您也可以尝试
I dont't have an example to test, but as the "Next" button is an UIAButton, and not an UIAKey you could try :
If it doesn't work, you can also try
以下对我有用:
The following works for me:
对我来说,键盘不属于视图层次结构中的 mainWindow() 。当您从顶层 logElementTree() 时,它与 mainWindow() 处于同一级别。所以,你想做的是:
当我试图按键盘上的“搜索”按钮时,这对我有用。
For me, keyboard does not fall under mainWindow() in the View Hierarchy. It is at the same level as mainWindow() when you logElementTree() from top level. So, what you want to do is:
This worked for me when I was trying to press the "Search" button on keyboard.