XCUIElement tap () not working

I have a very simple XCTestCase implementation that checks for a button click and expects an Alert controller to appear. The problem is that the tap() method is not working. Placing a breakpoint in an associated IBAction button I understand that logic is not even called.

 class uitestsampleUITests: XCTestCase { var app: XCUIApplication! override func setUp() { super.setUp() continueAfterFailure = false app = XCUIApplication() app.launch() } func testButton() { let button = app.buttons["Button"] button.tap() expectationForPredicate(NSPredicate(format: "exists == 1"), evaluatedWithObject: button, handler: nil) waitForExpectationsWithTimeout(5.0, handler: nil) } } 

In addition, duplicating the button.tap() command makes a test pass as follows:

  func testButton() { let button = app.buttons["Button"] button.tap() button.tap() expectationForPredicate(NSPredicate(format: "exists == 1"), evaluatedWithObject: button, handler: nil) waitForExpectationsWithTimeout(5.0, handler: nil) } 

I ran into this problem in Xcode 7.3.1 Am I missing something? This is mistake?

+8
ios integration-testing swift xcode-ui-testing
source share
4 answers

So, an Apple engineer answered my error report:

The second possibility is that you encounter a problem that sometimes occurs when the application finishes launching, but the splash screen does not immediately disappear, and events sent to the application are not processed properly.

To try to work around this problem, consider starting your test (sleep (1) should be enough).

So, I did this, and now it works:

 override func setUp() { super.setUp() continueAfterFailure = false app = XCUIApplication() app.launch() sleep(1) } 
+8
source share

For a UIWebView that does not allow, the crane did not work until I executed it using the coordinates:

 extension XCUIElement { func forceTap() { coordinate(withNormalizedOffset: CGVector(dx:0.5, dy:0.5)).tap() } } 

Hope this helps someone

PS Also works for items that are not allowed, such as labels, etc.

+3
source share

I had something like that. For me, the problem was that the elements I was trying to use were sometimes not hittable for some reason.

From the Apple documentation:

Sends a tap event to the point that is being calculated for the element, for httable.

So, if the item is not hittable , the touch action does not do much, which breaks the logic of your test case.

To fix this, before I touch anything, I wait until the corresponding item becomes hittable. Pretty simple.

 #import <XCTest/XCTest.h> @interface XCUIElement (Tap) - (void)tapInTestCase:(XCTestCase *)testCase; @end @implementation XCUIElement (Tap) - (void)tapInTestCase:(XCTestCase *)testCase { // wait until the element is hittable NSPredicate *predicate = [NSPredicate predicateWithFormat:@"hittable == true"]; [testCase expectationForPredicate:predicate evaluatedWithObject:element handler:nil]; [testCase waitForExpectationsWithTimeout:5.0f handler:nil]; // and then tap [self tap]; } @end 
+2
source share

For me, the most reliable way was to add sleep(1) in places where the elements are not listening properly. Using the usleep function with values ​​less than 1000 led to unreliable behavior, such as random errors in valid tests.

0
source share

All Articles