【问题标题】:Testing UIImagePicker测试 UIImagePicker
【发布时间】:2016-02-09 17:18:04
【问题描述】:

我正在尝试测试 UiImagePicker 我已使用 Xcode 上的记录功能来完成大部分测试,但它无法捕获用于确认我要选择的图片的元素。另一个选项“取消”也会发生同样的事情,我记得有某种方法可以获取视图上所有元素的列表或类似的效果。所以我的问题是如何在视图中获取对 ImagePicker 对象中选项的引用。

我正在为 iOS9 构建并运行 Xcode7.2

我当前的测试看起来像这样 ` 功能测试菜单(){ loginInApp(app) //让你通过登录

    app.buttons["LogoButton"].tap() //entry point for menuView

    XCTAssert(app.buttons["BR"].exists)
    let brButton = app.buttons["BR"]
    brButton.tap()

    let photosFromSheet = app.sheets["Where would you like to get photos from?"]
    XCTAssert(photosFromSheet.exists)
    photosFromSheet.staticTexts["Where would you like to get photos from?"].tap()
     XCTAssert(photosFromSheet.collectionViews.buttons["Chose from Library"].exists && photosFromSheet.buttons["Cancel"].exists)
    photosFromSheet.collectionViews.buttons["Chose from Library"].tap()
     XCTAssert(app.tables/*@START_MENU_TOKEN@*/.buttons["Moments"]/*[[".cells[\"Moments\"].buttons[\"Moments\"]",".buttons[\"Moments\"]"],[[[-1,1],[-1,0]]],[0]]@END_MENU_TOKEN@*/.exists)
    app.tables/*@START_MENU_TOKEN@*/.buttons["Moments"]/*[[".cells[\"Moments\"].buttons[\"Moments\"]",".buttons[\"Moments\"]"],[[[-1,1],[-1,0]]],[0]]@END_MENU_TOKEN@*/.tap()
    XCTAssert(app.collectionViews.cells["Photo, Landscape, March 12, 2011, 4:17 PM"].exists)
    app.collectionViews.cells["Photo, Landscape, March 12, 2011, 4:17 PM"].tap()

XCTAssert(app.childrenMatchingType(.Window).elementBoundByIndex(0).childrenMatchingType(.Other).elementBoundByIndex(2).childrenMatchingType(.Other).element.exists)

// 这就是事情变得模棱两可的地方,并且在测试运行时实际上并没有选择任何东西。

app.childrenMatchingType(.Window).elementBoundByIndex(0).childrenMatchingType(.Other).elementBoundByIndex(2).childrenMatchingType(.Other).element.tap()


    brButton.tap()
    photosFromSheet.buttons["Cancel"].tap()
    app.buttons["LogoButton"].tap()

`

【问题讨论】:

  • 在不明确的行之前放置一个断点并在控制台中运行po app.debugDescription,它将打印 UI 的可访问性层次结构。应该有助于弄清楚测试主机看到了什么。
  • 那里也没有透露。
  • 这解决了我的问题,但不适用于 6S+ stackoverflow.com/questions/33422681/…

标签: swift testing automation ios9


【解决方案1】:

UIImagePicker 显然不是hittable 所以这是解决方法

如果您在文档中看到 tap(),它会说

/*!
 * Sends a tap event to a hittable point computed for the element.
 */
- (void)tap;


/*Sends a tap event to a hittable/unhittable element.*/
extension XCUIElement {
    func forceTapElement() {
        if self.hittable {
            self.tap()
        }
        else {
            let coordinate: XCUICoordinate = self.coordinateWithNormalizedOffset(CGVectorMake(0.0, 0.0))
            coordinate.tap()
        }
    }
}

现在你可以调用为

button.forceTapElement()

这在 6S+ 上不起作用。希望这将很快得到解决。

【讨论】:

    【解决方案2】:
    /*Sends a tap event to a hittable/unhittable element.*/
        extension XCUIElement {
            func forceTapElement() {
                if self.isHittable {
                    self.tap()
                }
                else {
                    let coordinate: XCUICoordinate = self.coordinate(withNormalizedOffset: CGVector(dx: 0.0, dy: 0.0))
                    coordinate.tap()
                }
            }
        }
    

    Swift 4 版本。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-04-20
      • 2016-08-09
      • 2014-08-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多