1

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.

OsamaA
  • 451
  • 1
  • 8
  • 13
  • 1
    shouldn't you not be checking for that element if it doesn't exist then? Sounds like just changing the login of your asserts. – cakes88 Oct 16 '15 at 17:27

2 Answers2

0

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
Community
  • 1
  • 1
Laser Hawk
  • 1,988
  • 2
  • 23
  • 29
0

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

Community
  • 1
  • 1
Bill Chan
  • 3,199
  • 36
  • 32