简体   繁体   English

如何在XCTest中快速测试是否加载了Webview且没有staticTexts

[英]How to test Webview is loaded or not without staticTexts in XCTest in swift

first time I try to write unit test case. 我第一次尝试编写单元测试用例。 Here am blocking with the following scenario. 这里阻止了以下情况。

When I tap a button, it navigates to a next screen and webview will load. 当我点击一个按钮时,它将导航到下一个屏幕,并且将加载Webview。 I need to wait for webview loading. 我需要等待webview加载。

I tried to wait till the staticTexts is visible by below code, 我试图等到下面的代码可以看到staticTexts,

let rewards = self.app.staticTexts[“Rewards”]
let exists = NSPredicate(format: “exists == 1”)
expectationForPredicate(exists, evaluatedWithObject: rewards, handler: nil)
waitForExpectationsWithTimeout(10, handler: nil)
XCTAssert(rewards.exists)
XCTAssert(app.staticTexts[“Rewards Overview”].exists)

But In my scenario, when I tap the button it will navigate to next screen and webview will starts to load. 但是在我的情况下,当我点击按钮时,它将导航到下一个屏幕,并且Webview将开始加载。 But the webview content is always dynamic one. 但是,webview内容始终是动态的。 So I need to wait till the webviewDidFinishLoad. 所以我需要等到webviewDidFinishLoad。

You can use waitForExistence(timeout:) to return a boolean if an element enters existence within the given timeout - I'd recommend using this for your use case. 如果元素在给定的超时时间内进入存在状态,则可以使用waitForExistence(timeout:)返回一个布尔值-我建议在您的用例中使用它。

https://developer.apple.com/documentation/xctest/xcuielement/2879412-waitforexistence https://developer.apple.com/documentation/xctest/xcuielement/2879412-waitforexistence

I would recommend writing your own function for waiting, since with waitForExistence(timeout:) , you can only wait for existence, but it ignores other states of element( .isHittable , .isEnabled etc) or simply wait for any other boolean. 我建议编写自己的等待函数,因为使用waitForExistence(timeout:)只能等待存在,但是它会忽略element( .isHittable.isEnabled等)的其他状态,或者只是等待任何其他布尔值。

Plus waitForExistence(timeout:) is painfully slow, if you have a lot of tests with a lot of waits (we stopped using it after we wrote our own waiting). 加上waitForExistence(timeout:)非常慢,如果您有很多等待很多的测试(我们编写了自己的等待后就停止使用它)。 It is caused by the system method waiting for additional 1-2s before returning true, if the object exists. 这是由系统方法(如果存在)在返回true之前等待另外的1-2s引起的。

This might help you, however its not Swift, its ObjC: https://pspdfkit.com/blog/2016/running-ui-tests-with-ludicrous-speed/ 这可能会帮助您,但不是Swift的ObjC: https ://pspdfkit.com/blog/2016/running-ui-tests-with-ludicrous-speed/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM