Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/swift/18.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Swift XCUITest-找不到匹配的元素_Swift_Xcode_Xcode Ui Testing_Xctestcase - Fatal编程技术网

Swift XCUITest-找不到匹配的元素

Swift XCUITest-找不到匹配的元素,swift,xcode,xcode-ui-testing,xctestcase,Swift,Xcode,Xcode Ui Testing,Xctestcase,在我的一个视图控制器中,我有如下代码: class ViewController: UIViewController { @IBOutlet weak var _tableView: UITableView! private lazy var searchBar: UISearchBar = { let searchBar:UISearchBar = UISearchBar() searchBar.isAccessibilityElement =

在我的一个视图控制器中,我有如下代码:

class ViewController: UIViewController {
    @IBOutlet weak var _tableView: UITableView!

    private lazy var searchBar: UISearchBar = {
        let searchBar:UISearchBar = UISearchBar()
        searchBar.isAccessibilityElement = true
        searchBar.accessibilityLabel = "SearchField"
        searchBar.placeholder = "Search..."
        searchBar.sizeToFit()
        searchBar.isTranslucent = false
        searchBar.autocapitalizationType = .none
        searchBar.returnKeyType = .done
        searchBar.delegate = self

        return searchBar
    }()

    override func viewDidLoad() {
        super.viewDidLoad()

        isAccessibilityElement = true
        accessibilityLabel = "ViewController"

        _tableView.isAccessibilityElement = true
        _tableView.accessibilityLabel = "ItemList"
        _tableView.tableHeaderView = searchBar
    }
}
现在,每当我尝试录制UI测试并选择搜索栏时,都会出现以下错误:

时间戳事件匹配错误:找不到匹配元素

我尝试调试以获取搜索栏和以下代码,但它与任何元素都不匹配,断言失败

    let searchField = app.tables["ItemList"].otherElements.element(boundBy: 0).otherElements["SearchField"]
    XCTAssertTrue(searchField.exists)
你能告诉我我做错了什么吗?如何访问和测试搜索栏? 提前谢谢

  • 内置记录器不好,您应该自己编写测试代码

  • 使用
    print(app.debugDescription)

  • 您的XUIElementQuery比应该的要长一点。尽量缩短它,如
    app.otherElements[“SearchField”]


  • 您可以自下而上使用调试视图层次结构或XCode open developer工具可访问性检查器查找未启用可访问性的视图。

    为什么要向UIViewController提供可访问性属性?这可能会降低应用程序的可访问性。因为我需要测试在特定状态下显示哪个视图控制器。该视图是从代码中添加的,因此在这种情况下,调试视图层次结构不是选项。但好消息是,我使用Accessibility Inspector找到了SearchField。它位于tableView
    otherElements
    中,而不在
    元素(边界条件:0)中。otherElementes
    。谢谢。我不知道代码生成的视图不会出现,这很棘手。我的错。代码中的视图确实出现在调试视图层次结构中。我只是迫不及待地想从混乱的图层中找出答案我再次搜索了该评论。没问题,至少你尝试了值得一试的可访问性检查器