自 Xcode 7 以来,我们有一个很好的用于UI测试的API。 我对此很满意。唯一的问题与速度有关。
一开始普通的UI测试用例(大约15个操作)大约 25秒。然后我完全嘲笑网络。现在需要 20秒。考虑到时间只取决于动画和发射时间(1秒甚至更短),我想,必须有办法加快速度。
答案 0 :(得分:23)
尝试运行UI测试时设置此属性:
UIApplication.shared.keyWindow?.layer.speed = 100
以下是我如何设置它:
func application(_ application: UIApplication,
didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
if ProcessInfo.processInfo.arguments.contains("UITests") {
UIApplication.shared.keyWindow?.layer.speed = 100
}
}
在我的UI测试中:
class MyAppUITests: XCTestCase {
// MARK: - SetUp / TearDown
override func setUp() {
super.setUp()
let app = XCUIApplication()
app.launchArguments = ["UITests"]
app.launch()
}
}
此blog post中有一些方便的提示。
答案 1 :(得分:8)
另一种可能性是根本禁用动画:
[UIView setAnimationsEnabled:NO];
斯威夫特3:
UIView.setAnimationsEnabled(false)
答案 2 :(得分:4)
关注@Mark回答, Swift 3 版本:
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
if ProcessInfo.processInfo.arguments.contains("UITests") {
UIApplication.shared.keyWindow?.layer.speed = 200
}
}
关于你的测试文件:
override func setUp() {
super.setUp()
// Put setup code here. This method is called before the invocation of each test method in the class.
let app = XCUIApplication()
app.launchArguments = ["UITests"]
app.launch()
答案 3 :(得分:2)
在didFinishLaunch中添加
[UIApplication sharedApplication].keyWindow.layer.speed = 2;
默认值为1,使其速度加倍2。
答案 4 :(得分:1)
我想在快照测试期间禁用所有动画。我可以通过如下禁用Core Animation和UIView动画来实现这一目标。
请注意,因为我的应用在启动时使用的故事板UIApplication.shared.keyWindow
为零,所以我直接引用window
属性来访问UIWindow。
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
if ProcessInfo.processInfo.arguments.contains("SnapshotTests") {
// Disable Core Animations
window?.layer.speed = 0
// Disable UIView animations
UIView.setAnimationsEnabled(false)
}
return true
}
答案 5 :(得分:1)
并行运行它们。
如果只有一台构建机器,则可以使用Bluepill:https://github.com/linkedin/bluepill
如果您有多台计算机,则可以使用Emcee:https://github.com/avito-tech/Emcee(它也适用于单台计算机设置)
我们有50个小时的UI测试,Emcee允许我们在1个小时内运行它们。有一些技巧可以使UI测试更快,但是如果不并行运行它们,那将毫无意义。例如。您不能让25秒的测试在0.5秒内运行。
技巧:
XCUIApplication(
privateWithPath: nil,
bundleID: "your.bundle.id"
)
注意:每次启动XCUI测试运行程序时,您至少应使用XCUIApplication().launch()
启动应用程序以安装该应用程序。这需要使用私有API。
let someDataFromApi = getSomeData() // start request asynchronously and immediately return
launchApp() // this can be few seconds
useInTest(someDataFromApi) // access to data will wait request to finish
您可以使代码看起来像没有异步的东西,对于QA来说会更容易。我们想实现这一点,但是我们没有实现,所以这只是一个主意。
切换到EarlGrey并进行持续几秒钟的测试(但它们不会是黑匣子)。
如果您具有深层链接,则通过深层链接打开屏幕。
使用很多私有API和其他技巧。例如:您可以调用一些私有API,而不是通过UI进入“设置”应用,然后重置隐私设置。
请勿长时间睡眠。使用轮询。
加快动画播放速度。不要禁用它们!
将一些标志从UI测试传递到应用,以跳过初始警报/教程/弹出窗口。可以节省很多时间。确保至少进行一项测试,以检查这些警报是否正常工作。
答案 6 :(得分:0)
我将UITests时间减少了30%,请执行所有步骤:
运行应用程序时,添加参数:
let app = XCUIApplication()
override func setUp() {
super.setUp()
continueAfterFailure = false
app.launchArguments += ["--Reset"]
app.launch()
}
现在,在您的AppDelegate中添加:
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
setStateForUITesting()
}
static var isUITestingEnabled: Bool {
get {
return ProcessInfo.processInfo.arguments.contains("--Reset")
}
}
private func setStateForUITesting() {
if AppDelegate.isUITestingEnabled {
// If you need reset your app to clear state
UserDefaults.standard.removePersistentDomain(forName: Bundle.main.bundleIdentifier!)
// To speed up your tests
UIApplication.shared.keyWindow?.layer.speed = 2
UIView.setAnimationsEnabled(false)
}
}
在代码中,要验证是否处于测试模式,可以使用:
if AppDelegate.isUITestingEnabled {
print("Test Mode")
}
此外,在元素加载过程中,我可以创建此扩展名以使用wait
:
import XCTest
extension XCUIElement {
func tap(wait: Int, test: XCTestCase) {
if !isHittable {
test.expectation(for: NSPredicate(format: "hittable == true"), evaluatedWith: self, handler: nil);
test.waitForExpectations(timeout: TimeInterval(wait), handler: nil)
}
tap()
}
}
像这样使用:
app.buttons["start"].tap(wait: 20, test: self)
答案 7 :(得分:0)
请注意,keyWindow
从 iOS 13 起已弃用。具体来说,
AppDelegate 中的 UIApplication.sharedApplication.keyWindow
和 self.window.keyWindow
都返回 nil。 This answer 说循环遍历 UIApplication.shared.windows
并找到 isKeyWindow
为真的那个,但在我使用 ios14 的测试中,即使是我唯一的窗口也返回假。最后,设置 self.window.layer.speed
对我有用。