【问题标题】:XCTest - How to query for substring in navbar titleXCTest - 如何在导航栏标题中查询子字符串
【发布时间】:2016-10-12 06:20:16
【问题描述】:

我希望能够在 UI 测试中验证子字符串是否出现在导航栏中。

例如,如果导航栏标题是“Rent Properties”,那么我可以这样匹配:

XCTAssert(XCUIApplication().staticTexts["Rent Properties"].exists)

但是,这有两个问题:

  • 如果文本不在导航栏中,它仍然会匹配
  • 它进行完全匹配,而我希望能够匹配诸如“Rent”之类的子字符串

如何做到这一点?

【问题讨论】:

    标签: ios swift xcode xcode-ui-testing


    【解决方案1】:

    为了匹配子串Rent,可以使用下面的代码:

    XCUIApplication().staticTexts.matchingPredicate(NSPredicate(format: "label CONTAINS 'Rent'")).elementBoundByIndex(0)
    //it may contains one or more element with substring Rent.
    //you have to find out which element index you want in debug mode using p print() options.
    

    对于第一个选项,元素显示或不显示肯定会有区别。您必须在调试模式下使用 po 或 p 打印选项找出它。

    例如,可能存在计数不同或元素不可命中等......

    您可以尝试使用:

    let app = XCUIApplication()
    XCTAssert(app.staticTexts["Rent Properties"].exists)
    
    or 
    let app = XCUIApplication()
    app.staticTexts["Rent Properties"].hittable
    
    or
    let app = XCUIApplication()
    app.staticTexts["Rent Properties"].enabled
    
    or 
    
    app.staticTexts.matchingIdentifier("Rent Properties").count
    //take count while showing the text and take the count while not showing the text
    

    【讨论】:

      【解决方案2】:

      试试这些。

      1.从元素中获取静态文本,而不是从应用中获取。

        Eg:`XCUIApplication().navigationBars["Rent Properties"].staticTexts["Rent Properties"]`
      
      1. 使用elementMatchingPredicateexpectationForPredicate 来匹配元素。

      有用链接:http://masilotti.com/ui-testing-cheat-sheet/

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多