TableViewやcollectionViewで特定の条件が揃ったセルをタップした後に見た目や中身などが確実に変わっているか、確かめたいときがあるかと思います(無いかもしれない)。
でも、タップした事によって値が変更されたセルをMatcher等で探すのは一苦労かと思います。
そんな時に僕がどうするかを簡単に共有したいと思います。
解決策1
インスタンスを保持する
以下が例です。
例
import XCTest
import EarlGrey
@testable import QiitaPresents
class QiitaPresentsTests: XCTestCase {
override func setUp() {
super.setUp()
// Put setup code here. This method is called before the invocation of each test method in the class.
let success = GREYCondition(name: "setup") { () -> Bool in
var error: NSError? = nil
EarlGrey.select(elementWithMatcher: grey_kindOfClass(UIViewController.self))
.atIndex(0)
.assert(grey_sufficientlyVisible(), error: &error)
return error != nil
}.wait(withTimeout: 15)
GREYAssertTrue(success, reason: "failed setup")
}
override func tearDown() {
// Put teardown code here. This method is called after the invocation of each test method in the class.
super.tearDown()
}
func testExample() {
// This is an example of a functional test case.
// Use XCTAssert and related functions to verify your tests produce the correct results.
var targetCell: HogeCell!
let matcher = grey_allOf([grey_sufficientlyVisible(),
grey_kindOfClass(UITableViewCell.self),
self.grey_customCellMatcher()])
EarlGrey.select(elementWithMatcher: matcher)
.inRoot(grey_kindOfClass(UITableViewCell.self))
.atIndex(0)
.perform(self.grey_getTagetCell { (cell) in
targetCell = cell
})
EarlGrey.select(elementWithMatcher: grey_equalTo(cell))
.assert(grey_sufficientlyVisible())
}
private func grey_customCellMatcher() -> GREYMatcher {
return GREYElementMatcherBlock.matcher(matchesBlock: { (ele) -> Bool in
guard let cell = ele as? HogeCell else { return }
return cell.hoge
}, descriptionBlock: { (description) in
_ = description?.appendText("grey customCellMatcher")
})
}
private func grey_getTagetCell(completion: ((UITableViewCell) -> Void)?) -> GREYActionBlock {
return GREYActionBlock.action(withName: "grey getTargetCell") { (ele, err) -> Bool in
guard let _ = err else { return false }
guard let cell = ele as? HogeCell else { return false }
completion?(cell)
return true
}
}
}
解決策2
タップする前のセルにaccessibilityIdentifier
を付与する。
以下が例です。
例
import XCTest
import EarlGrey
@testable import QiitaPresents
class QiitaPresentsTests: XCTestCase {
override func setUp() {
super.setUp()
// Put setup code here. This method is called before the invocation of each test method in the class.
let success = GREYCondition(name: "setup") { () -> Bool in
var error: NSError? = nil
EarlGrey.select(elementWithMatcher: grey_kindOfClass(UIViewController.self))
.atIndex(0)
.assert(grey_sufficientlyVisible(), error: &error)
return error != nil
}.wait(withTimeout: 15)
GREYAssertTrue(success, reason: "failed setup")
}
override func tearDown() {
// Put teardown code here. This method is called after the invocation of each test method in the class.
super.tearDown()
}
func testExample() {
// This is an example of a functional test case.
// Use XCTAssert and related functions to verify your tests produce the correct results.
let matcher = grey_allOf([grey_sufficientlyVisible(),
grey_kindOfClass(UITableViewCell.self),
self.grey_customCellMatcher()])
EarlGrey.select(elementWithMatcher: matcher)
.inRoot(grey_kindOfClass(UITableViewCell.self))
.atIndex(0)
.perform(grey_addAccessibilityID())
EarlGrey.select(elementWithMatcher: grey_accessibilityID("targetCell"))
.assert(確認したい事)
}
private func grey_customCellMatcher() -> GREYMatcher {
return GREYElementMatcherBlock.matcher(matchesBlock: { (ele) -> Bool in
guard let cell = ele as? HogeCell else { return }
return cell.hoge
}, descriptionBlock: { (description) in
_ = description?.appendText("grey customCellMatcher")
})
}
private func grey_addAccessibilityID() -> GREYActionBlock {
return GREYActionBlock.action(withName: "grey addAccessibilityID") { (ele, err) -> Bool in
guard let _ = err else { return false }
guard let cell = ele as? UITableViewCell else { return false }
cell.accessibilityIdentifier = "targetCell"
return true
}
}
}
解決策3
一番簡単な方法ですが、そもそも一番最初のセルを対象(.adIndex(0)
)になんとかする(テストデータを用意する)
注意
解決策1,2はセルの再利用時におかしな挙動になりそうな予感がします。