I am trying to build a function findElementWithLabel() that can recursively walk the entire XCUIElement tree for any given element and return a child element with a specified label or id. Unlike the built in childrenMatchingType() function it will search children more than one level deep (not just direct children). I have built this function as an extension on XCUIElement, here is the sample code:
public extension XCUIElement {
public func findElementWithLabel(label: String) -> XCUIElement? {
return findElementWithName(self, name:label)
}
private func findElementWithName(elementToSearch:XCUIElement?, name: String) -> XCUIElement? {
if let currentElement = elementToSearch {
if currentElement.label == name {
return currentElement
}
} else {
return nil
}
let children = elementToSearch?.childrenMatchingType(.Any)
var loopIndex:UInt = 0
while (loopIndex < children?.count) {
let foundElement = findElementWithName(children?.elementBoundByIndex(loopIndex), name:name)
if let unwrappedFoundElement = foundElement {
return unwrappedFoundElement
}
loopIndex += 1
}
return nil
}
}
The function returns the proper result, but unfortunately the performance is horrible taking almost 10-15 seconds to return. Can any experts on xcode ui automation help me deduce what could be causing this? Originally I thought it was the repeated calls to "XCUIElement.childrenMatchingType(.Any)" however I think I can rule this out. The function on average takes 0.007 seconds to compute, and I invoke it roughly 30 times which means that can't be the culprit. The only other theories I have are:
1) Some delays are being applied somewhere, greatly affecting the run-time. I wrote this function in instruments with the javascript api and invoking UIATarget.localTarget().pushTimeout(0) before the tree traversal (and popTimeout(0) after the traversal) did the trick. This essentially caused my recursive function calls to not delay and wait on any elements... If this is the culprit is there some way I can achieve this global pushTimeout() and popTimeout() functionality with the new API?
2) The huge amounts of xcode console logging with the new ui-automation must be affecting runtime. I know NSLog's are synchronous and slow, so could this be the culprit? If so how can we switch these logs off? How can this even scale going forward, if the built in xcode xctest debug logs affect run-time so immensely. As devs we need these logs, but we also can not have it affect runtime like this.
Aucun commentaire:
Enregistrer un commentaire