breaktop breaktop - 2 years ago 99
Swift Question

Swift - How do you test whether a label has been updated when a button is tapped

I've go a very simple calculator and I' trying to test whether a label gets updated when a button is tapped.

My test method looks like this:

let app = XCUIApplication()

I can visually see the Label being updated but I'm not sure how to test it.

I think I need to use
API to query label and then assert that the label text has changed. I'm just not sure how to do that.

I'm not sure about the following questions:

  • Do I need to know what the value is for the label to be able to query?

  • Is there a way of querying for the label without knowing what the value is when the application starts?

Answer Source

With UI Testing you might have to think about your problem a little differently. Instead of asserting that something changed, check if the new thing exists.

In practice, this means to check that a label with your expected value appears. Don't check that an existing one changed to the correct state.

So, in your example, you can do the following. This will check that when you tap the "9" button a label with the text "42" appears.

let app = XCUIApplication()
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download