我有一个UI测试,用于检查注释视图是否显示在MKMapView
上。
由于注释视图以otherElements
的形式访问,因此我使用以下代码:
let actualPinsShown = shopEasyApp.otherElements.matching(identifier: kLocationPin)
let actualNrOfPinsShown = actualPinsShown.count
XCTAssert(actualNrOfPinsShown == 1, "\(actualNrOfPinsShown) pins shown instead of 1")
通常这可以正常工作,但有时XCTAssert
会失败,因为actualNrOfPinsShown
为0。
但是,mapView
仅显示1个引脚注释视图,当我在调试器中打印actualNrOfPinsShown
时,我得到
t = 28.97s Get number of matches for: Elements matching predicate '"LocationPin" IN identifiers'
t = 466.00s Snapshot accessibility hierarchy for app with pid 87746
t = 466.37s Find: Descendants matching type Other
t = 466.37s Find: Elements matching predicate '"LocationPin" IN identifiers'
t = 466.37s Snapshot accessibility hierarchy for app with pid 87746
t = 466.37s Find: Descendants matching type Other
t = 466.37s Find: Elements matching predicate '"LocationPin" IN identifiers'
t = 466.38s Snapshot accessibility hierarchy for app with pid 87746
t = 466.38s Find: Descendants matching type Other
t = 466.38s Snapshot accessibility hierarchy for app with pid 87746
Printing description of actualPinsShown:
Find: Target Application 0x6040002c5be0
Output: {
Application, 0x608000393660, pid: 87746, {{0.0, 0.0}, {375.0, 812.0}}, label: 'Shop Easy!'
}
↪︎Find: Descendants matching type Other
Output: {
Other, 0x600000392070, traits: 8589934592, {{0.0, 0.0}, {375.0, 812.0}}
Other, 0x600000391780, traits: 8589934592, {{0.0, 0.0}, {375.0, 812.0}}
Other, 0x600000392d70, traits: 8589934592, {{0.0, 0.0}, {375.0, 812.0}}
Other, 0x600000391370, traits: 8589934592, {{0.0, 812.0}, {375.0, 233.0}}
Other, 0x600000391510, {{0.0, 0.0}, {375.0, 44.0}}
Other, 0x6000003815f0, traits: 8590000128, {{125.7, 55.7}, {124.0, 20.3}}, label: 'Grapefruit juice'
Other, 0x6000003926f0, traits: 8589934592, {{0.0, 0.0}, {375.0, 812.0}}
Other, 0x6000003911d0, {{0.0, 0.0}, {375.0, 44.0}}
Other, 0x600000390b50, {{8.7, 24.7}, {81.3, 13.3}}
Other, 0x600000393180, traits: 8589934592, {{0.0, 0.0}, {375.0, 812.0}}
Other, 0x6000003912a0, {{14.7, 0.0}, {170.7, 28.3}}
Other, 0x6000003909b0, {{190.0, 0.0}, {170.3, 28.3}}
Other, 0x600000390f60, {{14.7, 10.3}, {66.7, 18.0}}
Other, 0x600000391030, {{21.7, 8.7}, {54.7, 21.3}}
Other, 0x600000390cf0, {{293.7, 10.3}, {66.7, 18.0}}
Other, 0x600000390c20, {{293.7, 10.3}, {66.7, 18.0}}
Other, 0x6000003923b0, traits: 8589934592, {{16.0, 60.0}, {343.0, 710.0}}
Other, 0x600000390e90, traits: 288230376160100352, {{293.7, 17.7}, {17.0, 10.7}}, label: 'No signal'
Other, 0x600000390dc0, traits: 288230376160100352, {{315.7, 17.3}, {15.3, 11.0}}, label: '3 of 3 Wi-Fi bars', value: SSID
Other, 0x600000390a80, traits: 288230376160100864, {{336.0, 17.3}, {24.3, 11.3}}, label: '100% battery power', value: Not charging
Other, 0x6000003922e0, traits: 8590000128, {{170.0, 11.7}, {35.3, 20.3}}, label: 'Title'
Other, 0x60000038d680, traits: 8589934592, {{16.0, 60.0}, {343.0, 710.0}}
Other, 0x600000391920, traits: 8589934592, {{16.0, 60.0}, {343.0, 710.0}}
Other, 0x600000392140, traits: 8589934592, {{77.1, 401.1}, {30.0, 30.0}}, label: 'Apfel'
Other, 0x600000391e00, traits: 8589934592, {{294.8, 409.0}, {30.0, 30.0}}, label: 'Feuerwehrmuseum Berlin'
Other, 0x600000391fa0, traits: 8589934592, {{288.5, 459.8}, {30.0, 30.0}}, label: 'cyber-Wear Heidelberg'
Other, 0x600000391ed0, traits: 8589934592, {{175.7, 531.8}, {30.0, 30.0}}, label: 'G-f-C Ges. für Catering'
Other, 0x600000392480, traits: 8589934592, {{182.5, 558.9}, {30.0, 30.0}}, label: 'KM Autowäsche GmbH & Co'
Other, 0x600000391b90, traits: 8589934592, {{316.8, 619.1}, {30.0, 30.0}}, label: 'Bischof, Harald Fahrzeuglackierer'
Other, 0x600000391c60, traits: 8589934592, {{242.9, 658.3}, {30.0, 30.0}}, label: 'Squash-In'
Other, 0x600000391850, traits: 8589934592, {{278.6, 694.3}, {30.0, 30.0}}, label: 'Edmund Böhler'
Other, 0x600000391ac0, traits: 70377334112256, {{179.3, 419.3}, {32.0, 39.0}}, identifier: 'LocationPin', label: 'AED118DF-488F-4616-BD7D-E29361EBA90C'
}
↪︎Find: Elements matching predicate '"LocationPin" IN identifiers'
Output: {
Other, 0x600000391ac0, traits: 70377334112256, {{179.3, 419.3}, {32.0, 39.0}}, identifier: 'LocationPin', label: 'AED118DF-488F-4616-BD7D-E29361EBA90C'
}
因此,它确实找到了1个引脚注释视图。
我的问题是:
actualNrOfPinsShown == 0
怎么会发生?
答案 0 :(得分:0)
这是由于XCTest
库和UI测试不完美而引起的。
在某些情况下,某些元素对于用户而言是可见的,对于调试器而言是可见的,但是在呈现期间的很短时间内对于测试而言是不可见的。这就是测试失败的原因。
您可以通过以下方式改进测试:
let actualPinsShown = shopEasyApp.otherElements.matching(identifier: kLocationPin)
if actualPinsShown.element(boundBy: 0).waitForExistence(5) {
let actualNrOfPinsShown = actualPinsShown.count
XCTAssert(actualNrOfPinsShown == 1, "\(actualNrOfPinsShown) pins shown instead of 1")
} else {
//you can add test fail here, since no elements are visible
}
上面的代码将等待5秒,您正在计数的元素中的第一个元素。