0

I am pretty new to iOS UITesting. I'm currently writing UITest for my app using XCTest. I was able to test if a UIElement is visible or hidden when a particular button is tapped. But I wanted to test if a certain method is called when a button is tapped. How to do this? Does this come under UI testing?

I also wanted to test the UIElements visible/hidden only when certain sent events are triggered instead of tap. Can we test sent events like Touch Down or Touch Up Inside of the button? Any help is appreciated. Thanks.

Kathiresan Murugan
  • 2,783
  • 3
  • 23
  • 44
Pradeep
  • 1
  • 2

2 Answers2

1

First part (testing if element is visible/hittable) is easy:

let button = app.buttons["yourButton"]
let element = app.cells["yourElement"]

button.tap() //tapping the button, so the element appears

if element.exists == false {
  //fail the test with custom message
}

if element.isHittable == false {
  //fail the test with custom message
}

You can ofc use XCTAsserts for the conditions above, but the conditions above can fail with custom message. Also - if your element is not visible immediately, use .waitForExistence instead of .exists

Second part shouldn't (and cannot) be tested by UI tests - you should have unit tests for checking, if your element sends the correct logs upon interacting with it.

Yes, you could (in theory) use asserts with console output, as suggested in @Βασίλης Δ's. answer, but that is not a right way of doing UI testing.

Václav
  • 990
  • 1
  • 12
  • 28
  • Thanks for the quick reply. I was able to test visible part of the test. I want to test if my code in `IBAction` method of a `UIButton` is being executed only when `Touch Up Inside` sent event is triggered not when `Touch Drag Outside` is triggered. Is there a way to do this. Also how can I check if a method is called when the button is tapped. – Pradeep Apr 25 '19 at 10:35
  • @Pradeep - its possible to test it with unit tests, as I said - check this https://stackoverflow.com/questions/18699524/is-it-possible-to-test-ibaction – Václav Apr 25 '19 at 10:47
0

You can technically test Touch down and Touch up inside events through UI tests, but not in a very clean way.

You can modify your element accessibility identifier by adding _touchdown or _touchupinside at the end of it, in the delegate methods. Then, in your UI test, you can check if your element identifier contains the tags.

cesarmarch
  • 617
  • 4
  • 13