如何在 XCode7 上对系统按钮进行自动化 UI 测试? [英] How to do automated UI testing for system button on XCode7?

查看:22
本文介绍了如何在 XCode7 上对系统按钮进行自动化 UI 测试?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的故事板只有一个 UI,它有一个导航栏和一个 UIBarButtonItem 系统项:添加.它还有另一个信息 UIButton.用英语进行 UI 测试时,一切正常,没有任何问题.但是,如果您将语言切换到另一种语言,则总是失败.这是测试代码片段:

My Storyboard has only one UI, and it has navigation bar with one UIBarButtonItem with System Item: Add. It also has another info UIButton. When doing UI testing in English everything works good without any problem. But if you switch the language to another one, it is always failed. Here is the testing code snippet:

app.navigationBars["My Product"].buttons["Add"].tap()
app.buttons["More Info"].tap()

根据错误日志,Xcode 找到了另一种语言.这里是:

According to the error log, Xcode find it in another language. Here it is:

断言失败:UI 测试失败 - 未找到更多信息"按钮的匹配项

Assertion Failure: UI Testing Failure - No matches found for "More Info" Button

查询输入是

Button 0x7fa4ca65a1d0: traits: 8724152321, {{8.0, 31.5}, {21.0, 21.0}}, label: '戻る',
Button 0x7fa4ca657700: traits: 8589934593, {{330.5, 26.0}, {40.0, 30.0}}, label: '追加',
Button 0x7fa4ca4658d0: traits: 8589934593, {{312.0, 605.0}, {22.0, 22.0}}, label: '詳細情報'

但是,我没有为它创建任何本地化字符串,因为它只是 iOS 系统创建的图像.有谁知道如何修理它?非常感谢.

However, I haven't created any localized string for it since it's just an image created by iOS system. Does anyone know how to fix it? Thanks so much.

推荐答案

选项 1:设置默认语言

为 UI 测试创建一个新方案并设置默认应用程序语言.这会将应用程序锁定到一个本地化文件中,以便您可以针对该语言编写所有测试.

Option 1: Set a Default Language

Create a new scheme for UI Testing and set the default Application Language. This will lock the app into one localized file so you can write all of your tests for that language.

从 Product -> Scheme -> Manage Schemes 或 ⌘⇧, 中设置选项.然后选择选项选项卡并设置语言.

Set the option from Product -> Scheme -> Manage Schemes or ⌘⇧,. Then select the Options tab and set the language.

优点:简单的一次性更改.

缺点:不能用于创建带有 snapshot 的本地化屏幕截图(通过 UI 测试运行您的应用并在此过程中生成 App Store 屏幕截图的工具.

Cons: Cannot be used to create localized screenshots with snapshot (a tool that runs your app via UI Testing and generates App Store screenshots along the way).

使用 accessibilityIdentifier 代替通过显示的文本或值访问项目.这是由 UI 测试框架读取的,但从未向用户显示或读取(即使打开了可访问性).在旧的 UIAutomation 文档中,Apple 提到将其用于开发人员功能,这看起来像是一个很好的用例.

Instead of accessing items via their displayed text or value, use accessibilityIdentifier. This is read by the UI Testing framework but never shown or read to users (even with accessibility turned on). In the old UIAutomation docs Apple mentions using this for developer functionality, which this seams like a good use case.

然后您可以像往常一样使用本地化版本继续设置 accessibilityLabelaccessibilityValue.

You can then continue to set accessibilityLabel and accessibilityValue like normal, with the localized versions.

优点:可用于更通用的解决方案,例如自动截屏.

Pros: Can be used for more generic solutions, such as taking automated screenshots.

缺点:可能需要做更多工作来更改您需要未本地化"进行测试的每个标签.

Cons: Might require more work changing each label you need "unlocalized" for testing.

这篇关于如何在 XCode7 上对系统按钮进行自动化 UI 测试?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆