Is there any way to check element exists in XCTest other than .exists function?

4.4k Views Asked by At

Currently, I am working in XCTest iOS framework and using .exists function to check the element presence. Want to know if there is any other way to check the presence of element on UI as .exists is getting problem. Tests get successful on the first run but when it runs second time, it gets failed because script clicks the element which is not exist on the UI might be because elements loaded first time in app remains hidden but exists.

Please let me know any function which checks the current screen elements presence.

2

There are 2 best solutions below

0
Laser Hawk On

I too am looking for a way to wait until an element appears instead of using the sleep method. sometimes UI elements take longer to load, but that doesn't mean it won't appear, eventually. At which point you can check use the .exists

There is a XCUIApplication Class Reference, not hosted by apple, but its in OBJ-C: http://masilotti.com/xctest-documentation/index.html

UPDATE: I think I found a solution from this link Delay/Wait in a test case of Xcode UI testing

Using the NSPredicate class worked for me.

    let welcomePage = app.staticTexts["Landing Page"]
    let existsPredicate = NSPredicate(format: "exists == 1")

    expectationForPredicate(existsPredicate, evaluatedWithObject: LandingPageLoad, handler: nil)
    waitForExpectationsWithTimeout(5, handler: nil)
  • First create a constant that of a value or object you're waiting for
  • Create another constant using the NSPredicate class to compare a value of an object
  • Then use the expectationForPredicate method along with vars
  • And lastly give the handler a timeout upper limit
0
Bill Chan On

I refer to this answer and this XCUIElementQuery function can handle the case the element is not created:

 open func matching(identifier: String) -> XCUIElementQuery

ref: Testing if an element is visible with XCode 7 UITest