代码之家  ›  专栏  ›  技术社区  ›  Reinhard Männer

UI测试返回的其他元素数量错误

  •  0
  • Reinhard Männer  · 技术社区  · 7 年前

    我有一个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
    但是, mapView 仅显示1针注释视图,以及打印时 在调试器中,我得到

        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 ?

    1 回复  |  直到 7 年前
        1
  •  0
  •   Václav    6 年前

    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秒,等待元素中的第一个元素,您正在计数。