How can I check for a UIView using XCUIElementsQuery?

It’s hard to find the right documentation for Apple’s UI Testing Platform. I saw many examples where buttons and labels with specific names can be found, but how can I search for generic UIViews?

For example, suppose I set a view with accessibilityLabel == "Some View", how can I find a view with this name during test execution?

I tried (unsuccessfully) the following code:

XCUIElement *pvc = [[app childrenMatchingType:XCUIElementTypeAny] elementMatchingPredicate:[NSPredicate predicateWithFormat:@"accessibilityLabel == 'Places View'"]]; NSPredicate *exists = [NSPredicate predicateWithFormat:@"exists == true"]; [self expectationForPredicate:exists evaluatedWithObject:pvc handler:nil]; [self waitForExpectationsWithTimeout:10 handler:nil]; NSLog(@"%@", [[app childrenMatchingType:XCUIElementTypeAny] elementMatchingPredicate:[NSPredicate predicateWithFormat:@"accessibilityLabel == 'Places View Controller'"]]); 
+5
source share
2 answers

The problem that I see with the above code is that you are specifically trying to find a representation in the children of the application, not the descendants of the application. Children are strictly subordinate to the submission, and the search will not consider sub-sub-representations, etc. Other than that, it looks normal.

Try:

 XCUIElement *pvc = [[app descendantsMatchingType:XCUIElementTypeAny] elementMatchingPredicate:[NSPredicate predicateWithFormat:@"accessibilityLabel == 'Places View'"]]; 

In addition, the default value for UIView is to not participate in accessibility at all, so in addition to setting the label (and by the way, the code snippet in your question about setting the label performs a comparison instead of an assignment), you should also make sure that you have enabled accessibility:

 view.isAccessibilityElement = YES view.accessibilityLabel = @"AccessibilityLabel" 
+5
source

A request from another answer works, but I found that you can also find views just by using:

 app.otherElements["MenuViewController.menuView"].swipeLeft() 

Where in my case MenuViewController.menuView is a UIView with a set of accessibilityIdentifier .

+2
source

Source: https://habr.com/ru/post/1238180/


All Articles