By regina_fallangi


2018-10-09 10:48:21 8 Comments

I am currently trying to figure out when an activity indicator is on or off. I realised that, when on, its value is 1, 0 when off.

I am trying to check if that value is 1 or 0 like this:

let app = XCUIApplication()
let predicate = NSPredicate(format: "value == 0")
let expectation = expectation(
        for: predicate, 
        evaluatedWith: app.activityIndicators["activityId"],
        handler: nil)

if XCTWaiter().wait(for:[expectation], timeout: 20) == .completed {
    print ("Activity indicator is off")
} else {
    print ("Activity indicator is on")
}

The current problem is that the timeout is always reached. Whenever I set a breakpoint at the if and type this in the console:

po app.activityIndicators["activityId"].value

The result is:

Optional<Any>
- some : 0

However, after I let the test run again, the timeout is reached and the else branch is taken.

Can anyone see what I am doing wrong while accessing the value of the element in the expectation?

Thanks for your help in advance.

1 comments

@Aleksander 2018-10-11 14:35:45

Assuming you've both launched your app and set the accessibility identifier of your activity indicator correctly (which it seems you've done), the reason your code doesn't work is that value is a string, not an integer.

Replacing your predicate with the following will make your code work:

let predicate = NSPredicate(format: "value == \"0\"")

Related Questions

Sponsored Content

12 Answered Questions

[SOLVED] swift for loop: for index, element in array?

  • 2014-06-04 03:19:21
  • metaphy
  • 308401 View
  • 630 Score
  • 12 Answer
  • Tags:   arrays swift

0 Answered Questions

xcode UITesting - call method from UITest?

  • 2018-06-27 07:01:24
  • Steffen L.
  • 16 View
  • 0 Score
  • 0 Answer
  • Tags:   xcode uitest

14 Answered Questions

[SOLVED] How to check if an element is in an array

  • 2014-06-08 00:52:11
  • jaredsmith
  • 280242 View
  • 396 Score
  • 14 Answer
  • Tags:   arrays swift

0 Answered Questions

1 Answered Questions

[SOLVED] UITest XCUIElementQuery missing elements

1 Answered Questions

XCode UITest will not give NSTextField keyboard focus

1 Answered Questions

[SOLVED] Get UISwitch state/value in UITests

2 Answered Questions

[SOLVED] Property value prints but when assigned to label it shows nil

2 Answered Questions

[SOLVED] Swift if let evaluates successfully on Optional(nil)

Sponsored Content