gpt4 book ai didi

swift - EarlGrey测试对ViewController的隔离

转载 作者:行者123 更新时间:2023-11-30 12:09:38 25 4
gpt4 key购买 nike

我正在尝试测试是否成功登录后会出现一个新的viewcontroller,但我不太走运,无法理解原因。

测试:

func testLoginButtonPerformsSegueWithValidEmailAndPassword() {
let titleLabel = EarlGrey.select(elementWithMatcher: grey_kindOfClass(MyBillsViewController.self))
let assertion = grey_sufficientlyVisible()
let conditionName = "Wait for title to appear in next MyBillsViewController"

login(email: registeredEmail,
password: password)

let appearedSuccesfully = waitForSuccess(of: assertion,
with: titleLabel,
conditionName: conditionName)

GREYAssertTrue(appearedSuccesfully, reason: "Title Label appeared with correct text")
}


登录助手方法:

func login(email: String, password: String) {
let emailTextField = grey_accessibilityID(TestAccesID.emailTextField)
let passwordTextField = grey_accessibilityID(TestAccesID.passwordTextField)
let loginButton = grey_accessibilityID(TestAccesID.loginButton)

EarlGrey.select(elementWithMatcher: emailTextField)
.perform(grey_tap()).perform(grey_typeText(email))
EarlGrey.select(elementWithMatcher: passwordTextField)
.perform(grey_tap()).perform(grey_typeText(password))
EarlGrey.select(elementWithMatcher: loginButton).perform(grey_tap())
}


GreyCondition Helper方法:

func waitForSuccess(of assertion: GREYMatcher,
with element: GREYElementInteraction,
conditionName: String ) -> Bool {
var success = false

GREYCondition(name: conditionName, block: { () -> Bool in

let errorOrNil = UnsafeMutablePointer<NSError?>.allocate(capacity: 1)
errorOrNil.initialize(to: nil)

element.assert(with: assertion, error: errorOrNil)

success = errorOrNil.pointee == nil
errorOrNil.deinitialize()
errorOrNil.deallocate(capacity: 1)

return success
}).wait(withTimeout: 3.0)

return success
}


尽管成功登录并正确选择正确的视图控制器,但我仍然无法通过测试,但是我不确定自己缺少什么。任何帮助将是巨大的,谢谢。

最佳答案

异步功能测试是已知的挑战。如果您设置断点,我认为问题在于:

GREYCondition(name: conditionName, block: {
//Do stuff here.
}).wait(withTimeout: 3.0)


该块不会立即输入,然后在 return success行(其中成功=假)之后直接进入 wait

要解决此问题,请尝试实现 XCWaitCompletionHandlerXCTestExpectation

func expectation(description: String) -> XCTestExpectation

func waitForExpectations(timeout: TimeInterval, handler: XCWaitCompletionHandler? = nil)

关于swift - EarlGrey测试对ViewController的隔离,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46237660/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com