【问题标题】:How can I perform a "swipe left" action via UI tests如何通过 UI 测试执行“向左滑动”操作
【发布时间】:2016-02-27 18:20:21
【问题描述】:

如何通过 Xcode 中的 UI 测试执行“向左滑动”操作来触发 segue 和更改视图控制器?

记录的代码是

[[[[[[[XCUIApplication alloc] init].otherElements containingType:XCUIElementTypeNavigationBar identifier:@"UIView"] childrenMatchingType:XCUIElementTypeOther].element childrenMatchingType:XCUIElementTypeOther].element childrenMatchingType:XCUIElementTypeOther].element tap];

但它似乎不起作用——据我所知,视图控制器没有更改为新的。

【问题讨论】:

    标签: ios objective-c xcode xcode7 xcode-ui-testing


    【解决方案1】:

    很难准确判断您要滑动哪个视图。以下代码将在标题为“Swipe Me”的标签上执行“向左滑动”手势。定位视图后,应该很容易遵循相同的技术。

    XCUIApplication *app = [XCUIApplication alloc] init];
    [app.labels[@"Swipe Me"] swipeLeft];
    

    Read more about swipeLeft 以及您可以对元素执行的伴随手势。如果您正在寻找更通用的“备忘单”,我也有got you covered

    【讨论】:

    • 是的,它有效,非常感谢。为什么我的测试会多次运行应用程序?我只有存根方法 setUptearDownone 使用以下代码进行 UI 测试 -- XCUIApplication *app = [[XCUIApplication alloc] init]; [app.images[@"swipeleft"] swipeLeft]; XCUIElement *element = app.buttons[@"icon share"]; [self waitForElementToAppear:element withTimeout:5];
    • @Joe Masilotti 我可以更准确地处理向左滑动手势吗?例如向左滑动距离?
    • @RyanChou 您可以下拉到XCUICoordinate API 并从一个元素拖动到另一个元素。见this example on pull-to-refresh
    • @JoeMasilotti 哦,太棒了。非常感谢。我会仔细阅读这个例子。它可以满足我的要求。
    【解决方案2】:

    如果你不能在appswipeLeft

    app.swipeLeft()
    

    尝试在scrollView 上调用它,比如

    app.scrollViews.element(boundBy: 0).swipeLeft()
    

    【讨论】:

      【解决方案3】:

      无需知道 UI 堆栈上当前有哪些 UI 元素可用,这对我有用: app.windows.element(boundBy:0).swipeLeft()

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2017-08-10
        • 1970-01-01
        • 2018-02-05
        • 1970-01-01
        • 1970-01-01
        • 2023-03-07
        • 2023-03-12
        • 1970-01-01
        相关资源
        最近更新 更多