Skip to content

Commit aaf7cec

Browse files
Marek Cirkosfacebook-github-bot
authored andcommitted
Remove isVisible processing from xpath
Summary: WDA still is having issues on requesting element visibility, which can lead to massive timeouts. Till we solve visibility check issues it is better to remove it from xpath queries. Reviewed By: lawrencelomax Differential Revision: D4403853 fbshipit-source-id: dff9a476e47d3d8d294d9fcf2966eed281843978
1 parent 69e638b commit aaf7cec

File tree

5 files changed

+35
-41
lines changed

5 files changed

+35
-41
lines changed

WebDriverAgentLib/Utilities/FBXPath.m

Lines changed: 18 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ @implementation FBXPath
2727

2828
+ (void)throwException:(NSString *)name forQuery:(NSString *)xpathQuery __attribute__((noreturn))
2929
{
30-
NSString * reason = [NSString stringWithFormat:@"Cannot evaluate results for XPath expression \"%@\"", xpathQuery];
30+
NSString *reason = [NSString stringWithFormat:@"Cannot evaluate results for XPath expression \"%@\"", xpathQuery];
3131
@throw [NSException exceptionWithName:name reason:reason userInfo:@{}];
3232
}
3333

@@ -46,13 +46,13 @@ + (nullable NSString *)xmlStringWithSnapshot:(XCElementSnapshot *)root
4646
xmlDocDumpFormatMemory(doc, &xmlbuff, &buffersize, 1);
4747
xmlFreeTextWriter(writer);
4848
xmlFreeDoc(doc);
49-
return [NSString stringWithCString:(const char*)xmlbuff encoding:NSUTF8StringEncoding];
49+
return [NSString stringWithCString:(const char *)xmlbuff encoding:NSUTF8StringEncoding];
5050
}
5151

5252
+ (NSArray<XCElementSnapshot *> *)findMatchesIn:(XCElementSnapshot *)root xpathQuery:(NSString *)xpathQuery
5353
{
5454
xmlDocPtr doc;
55-
55+
5656
xmlTextWriterPtr writer = xmlNewTextWriterDoc(&doc, 0);
5757
if (NULL == writer) {
5858
[FBLogger logFmt:@"Failed to invoke libxml2>xmlNewTextWriterDoc for XPath query \"%@\"", xpathQuery];
@@ -67,15 +67,15 @@ + (nullable NSString *)xmlStringWithSnapshot:(XCElementSnapshot *)root
6767
[FBXPath throwException:XCElementSnapshotXPathQueryEvaluationException forQuery:xpathQuery];
6868
return nil;
6969
}
70-
70+
7171
xmlXPathObjectPtr queryResult = [FBXPath evaluate:xpathQuery document:doc];
7272
if (NULL == queryResult) {
7373
xmlFreeTextWriter(writer);
7474
xmlFreeDoc(doc);
7575
[FBXPath throwException:XCElementSnapshotInvalidXPathException forQuery:xpathQuery];
7676
return nil;
7777
}
78-
78+
7979
NSArray *matchingSnapshots = [FBXPath collectMatchingSnapshots:queryResult->nodesetval elementStore:elementStore];
8080
xmlXPathFreeObject(queryResult);
8181
xmlFreeTextWriter(writer);
@@ -101,7 +101,7 @@ + (NSArray *)collectMatchingSnapshots:(xmlNodeSetPtr)nodeSet elementStore:(NSMut
101101
[FBLogger log:@"Failed to invoke libxml2>xmlGetProp"];
102102
return nil;
103103
}
104-
XCElementSnapshot *element = [elementStore objectForKey:(id)[NSString stringWithCString:(const char*)attrValue encoding:NSUTF8StringEncoding]];
104+
XCElementSnapshot *element = [elementStore objectForKey:(id)[NSString stringWithCString:(const char *)attrValue encoding:NSUTF8StringEncoding]];
105105
if (element) {
106106
[matchingSnapshots addObject:element];
107107
}
@@ -133,22 +133,22 @@ + (int)getSnapshotAsXML:(XCElementSnapshot *)root writer:(xmlTextWriterPtr)write
133133
return 0;
134134
}
135135

136-
+ (xmlChar *) xmlCharPtrForInput:(const char *)input
136+
+ (xmlChar *)xmlCharPtrForInput:(const char *)input
137137
{
138138
if (0 == input) {
139139
return NULL;
140140
}
141-
141+
142142
xmlCharEncodingHandlerPtr handler = xmlFindCharEncodingHandler(_UTF8Encoding);
143143
if (!handler) {
144144
[FBLogger log:@"Failed to invoke libxml2>xmlFindCharEncodingHandler"];
145145
return NULL;
146146
}
147-
147+
148148
int size = (int) strlen(input) + 1;
149149
int outputSize = size * 2 - 1;
150150
xmlChar *output = (unsigned char *) xmlMalloc((size_t) outputSize);
151-
151+
152152
if (0 != output) {
153153
int temp = size - 1;
154154
int ret = handler->input(output, &outputSize, (const xmlChar *) input, &temp);
@@ -160,7 +160,7 @@ + (xmlChar *) xmlCharPtrForInput:(const char *)input
160160
output[outputSize] = 0;
161161
}
162162
}
163-
163+
164164
return output;
165165
}
166166

@@ -171,7 +171,7 @@ + (xmlXPathObjectPtr)evaluate:(NSString *)xpathQuery document:(xmlDocPtr)doc
171171
[FBLogger logFmt:@"Failed to invoke libxml2>xmlXPathNewContext for XPath query \"%@\"", xpathQuery];
172172
return NULL;
173173
}
174-
174+
175175
xmlXPathObjectPtr xpathObj = xmlXPathEvalExpression([FBXPath xmlCharPtrForInput:[xpathQuery cStringUsingEncoding:NSUTF8StringEncoding]], xpathCtx);
176176
if (NULL == xpathObj) {
177177
xmlXPathFreeContext(xpathCtx);
@@ -223,11 +223,6 @@ + (int)recordElementAttributes:(xmlTextWriterPtr)writer forElement:(XCElementSna
223223
return rc;
224224
}
225225
}
226-
rc = xmlTextWriterWriteAttribute(writer, BAD_CAST "visible", element.wdVisible ? BAD_CAST "true" : BAD_CAST "false");
227-
if (rc < 0) {
228-
[FBLogger logFmt:@"Failed to invoke libxml2>xmlTextWriterWriteAttribute. Error code: %d", rc];
229-
return rc;
230-
}
231226
rc = xmlTextWriterWriteAttribute(writer, BAD_CAST "enabled", element.wdEnabled ? BAD_CAST "true" : BAD_CAST "false");
232227
if (rc < 0) {
233228
[FBLogger logFmt:@"Failed to invoke libxml2>xmlTextWriterWriteAttribute. Error code: %d", rc];
@@ -241,7 +236,7 @@ + (int)recordElementAttributes:(xmlTextWriterPtr)writer forElement:(XCElementSna
241236
return rc;
242237
}
243238
}
244-
239+
245240
if (nil != indexPath) {
246241
rc = xmlTextWriterWriteAttribute(writer, [FBXPath xmlCharPtrForInput:[kXMLIndexPathKey cStringUsingEncoding:NSUTF8StringEncoding]],
247242
[FBXPath xmlCharPtrForInput:[indexPath cStringUsingEncoding:NSUTF8StringEncoding]]);
@@ -250,25 +245,25 @@ + (int)recordElementAttributes:(xmlTextWriterPtr)writer forElement:(XCElementSna
250245
return rc;
251246
}
252247
}
253-
248+
254249
return 0;
255250
}
256251

257252
+ (int)generateXMLPresentation:(XCElementSnapshot *)root indexPath:(nullable NSString *)indexPath elementStore:(nullable NSMutableDictionary *)elementStore writer:(xmlTextWriterPtr)writer
258253
{
259254
NSAssert((indexPath == nil && elementStore == nil) || (indexPath != nil && elementStore != nil), @"Either both or none of indexPath and elementStore arguments should be equal to nil");
260-
255+
261256
int rc = xmlTextWriterStartElement(writer, [FBXPath xmlCharPtrForInput:[root.wdType cStringUsingEncoding:NSUTF8StringEncoding]]);
262257
if (rc < 0) {
263258
[FBLogger logFmt:@"Failed to invoke libxml2>xmlTextWriterStartElement. Error code: %d", rc];
264259
return rc;
265260
}
266-
261+
267262
rc = [FBXPath recordElementAttributes:writer forElement:root indexPath:indexPath];
268263
if (rc < 0) {
269264
return rc;
270265
}
271-
266+
272267
NSArray *children = root.children;
273268
for (NSUInteger i = 0; i < [children count]; i++) {
274269
XCElementSnapshot *childSnapshot = children[i];
@@ -281,7 +276,7 @@ + (int)generateXMLPresentation:(XCElementSnapshot *)root indexPath:(nullable NSS
281276
return rc;
282277
}
283278
}
284-
279+
285280
rc = xmlTextWriterEndElement(writer);
286281
if (rc < 0) {
287282
[FBLogger logFmt:@"Failed to invoke libxml2>xmlTextWriterEndElement. Error code: %d", rc];
@@ -291,4 +286,3 @@ + (int)generateXMLPresentation:(XCElementSnapshot *)root indexPath:(nullable NSS
291286
}
292287

293288
@end
294-

WebDriverAgentTests/IntegrationTests/FBXPathIntegrationTests.m

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -35,18 +35,18 @@ - (void)testSingleDescendantXMLRepresentation
3535
NSString *expectedType = @"XCUIElementTypeButton";
3636
XCUIElement *matchingElement = [[self.testedView fb_descendantsMatchingXPathQuery:[NSString stringWithFormat:@"//%@", expectedType] shouldReturnAfterFirstMatch:YES] firstObject];
3737
XCElementSnapshot *matchingSnapshot = matchingElement.fb_lastSnapshot;
38-
38+
3939
NSString *xmlStr = [FBXPath xmlStringWithSnapshot:matchingSnapshot];
4040
XCTAssertNotNil(xmlStr);
41-
42-
NSString *expectedXml = [NSString stringWithFormat:@"<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<%@ type=\"%@\" name=\"%@\" label=\"%@\" visible=\"%@\" enabled=\"%@\" x=\"%@\" y=\"%@\" width=\"%@\" height=\"%@\"/>\n", expectedType, expectedType, matchingSnapshot.wdName, matchingSnapshot.wdLabel, matchingSnapshot.wdVisible ? @"true" : @"false", matchingSnapshot.wdEnabled ? @"true" : @"false", [matchingSnapshot.wdRect[@"x"] stringValue], [matchingSnapshot.wdRect[@"y"] stringValue], [matchingSnapshot.wdRect[@"width"] stringValue], [matchingSnapshot.wdRect[@"height"] stringValue]];
41+
42+
NSString *expectedXml = [NSString stringWithFormat:@"<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<%@ type=\"%@\" name=\"%@\" label=\"%@\" enabled=\"%@\" x=\"%@\" y=\"%@\" width=\"%@\" height=\"%@\"/>\n", expectedType, expectedType, matchingSnapshot.wdName, matchingSnapshot.wdLabel, matchingSnapshot.wdEnabled ? @"true" : @"false", [matchingSnapshot.wdRect[@"x"] stringValue], [matchingSnapshot.wdRect[@"y"] stringValue], [matchingSnapshot.wdRect[@"width"] stringValue], [matchingSnapshot.wdRect[@"height"] stringValue]];
4343
XCTAssertTrue([xmlStr isEqualToString: expectedXml]);
4444
}
4545

4646
- (void)testFindMatchesInElement
4747
{
4848
NSArray *matchingSnapshots = [FBXPath findMatchesIn:self.testedView.fb_lastSnapshot xpathQuery:@"//XCUIElementTypeButton"];
49-
49+
5050
XCTAssertEqual([matchingSnapshots count], 4);
5151
for (id<FBElement> element in matchingSnapshots) {
5252
XCTAssertTrue([element.wdType isEqualToString:@"XCUIElementTypeButton"]);

WebDriverAgentTests/IntegrationTests/XCUIElementFBFindTests.m

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,7 @@ - (void)testFirstDescendantWithWrongXPathQuery
132132
NSException, XCElementSnapshotInvalidXPathException);
133133
}
134134

135-
- (void)testVisibleDescendantWithXPathQuery
135+
- (void)disabled_testVisibleDescendantWithXPathQuery
136136
{
137137
NSArray<XCUIElement *> *matchingSnapshots = [self.testedView fb_descendantsMatchingXPathQuery:@"//XCUIElementTypeButton[@name='Alerts' and @enabled='true' and @visible='true']" shouldReturnAfterFirstMatch:NO];
138138
XCTAssertEqual(matchingSnapshots.count, 1);
@@ -142,7 +142,7 @@ - (void)testVisibleDescendantWithXPathQuery
142142
XCTAssertEqualObjects(matchingSnapshots.lastObject.label, @"Alerts");
143143
}
144144

145-
- (void)testInvisibleDescendantWithXPathQuery
145+
- (void)disabled_testInvisibleDescendantWithXPathQuery
146146
{
147147
[self goToAttributesPage];
148148
NSArray<XCUIElement *> *matchingSnapshots = [self.testedApplication fb_descendantsMatchingXPathQuery:@"//XCUIElementTypePageIndicator[@visible='true']" shouldReturnAfterFirstMatch:NO];

WebDriverAgentTests/UnitTests/Doubles/XCUIElementDouble.m

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -22,15 +22,15 @@ - (id)init
2222
self.wdFrame = CGRectMake(0, 0, 0, 0);
2323
self.wdName = @"testName";
2424
self.wdLabel = @"testLabel";
25-
self.wdValue = @"кирилиця";
25+
self.wdValue = @"magicValue";
2626
self.wdVisible = YES;
2727
self.wdAccessible = YES;
2828
self.wdEnabled = YES;
2929
self.children = @[];
30-
self.wdRect = @{@"x": @(0),
31-
@"y": @(0),
32-
@"width": @(0),
33-
@"height": @(0),
30+
self.wdRect = @{@"x": @0,
31+
@"y": @0,
32+
@"width": @0,
33+
@"height": @0,
3434
};
3535
self.wdAccessibilityContainer = NO;
3636
self.elementType = XCUIElementTypeOther;

WebDriverAgentTests/UnitTests/FBXPathTests.m

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ @implementation FBXPathTests
2121
- (void)testInternalSnapshotXPathPresentation
2222
{
2323
xmlDocPtr doc;
24-
24+
2525
xmlTextWriterPtr writer = xmlNewTextWriterDoc(&doc, 0);
2626
NSMutableDictionary *elementStore = [NSMutableDictionary dictionary];
2727
XCUIElementDouble *root = [XCUIElementDouble new];
@@ -36,16 +36,16 @@ - (void)testInternalSnapshotXPathPresentation
3636

3737
XCTAssertEqual(rc, 0);
3838

39-
NSString *resultXml = [NSString stringWithCString:(const char*)xmlbuff encoding:NSUTF8StringEncoding];
40-
NSString *expectedXml = @"<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<XCUIElementTypeOther type=\"XCUIElementTypeOther\" value=\"кирилиця\" name=\"testName\" label=\"testLabel\" visible=\"true\" enabled=\"true\" x=\"0\" y=\"0\" width=\"0\" height=\"0\" private_indexPath=\"top\"/>\n";
39+
NSString *resultXml = [NSString stringWithCString:(const char *)xmlbuff encoding:NSUTF8StringEncoding];
40+
NSString *expectedXml = @"<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<XCUIElementTypeOther type=\"XCUIElementTypeOther\" value=\"magicValue\" name=\"testName\" label=\"testLabel\" enabled=\"true\" x=\"0\" y=\"0\" width=\"0\" height=\"0\" private_indexPath=\"top\"/>\n";
4141
XCTAssertTrue([resultXml isEqualToString: expectedXml]);
4242
XCTAssertEqual(1, [elementStore count]);
4343
}
4444

4545
- (void)testSnapshotXPathResultsMatching
4646
{
4747
xmlDocPtr doc;
48-
48+
4949
xmlTextWriterPtr writer = xmlNewTextWriterDoc(&doc, 0);
5050
NSMutableDictionary *elementStore = [NSMutableDictionary dictionary];
5151
XCUIElementDouble *root = [XCUIElementDouble new];
@@ -55,14 +55,14 @@ - (void)testSnapshotXPathResultsMatching
5555
xmlFreeDoc(doc);
5656
XCTAssertEqual(rc, 0);
5757
}
58-
58+
5959
xmlXPathObjectPtr queryResult = [FBXPath evaluate:@"//XCUIElementTypeOther" document:doc];
6060
if (NULL == queryResult) {
6161
xmlFreeTextWriter(writer);
6262
xmlFreeDoc(doc);
6363
XCTAssertNotEqual(NULL, queryResult);
6464
}
65-
65+
6666
NSArray *matchingSnapshots = [FBXPath collectMatchingSnapshots:queryResult->nodesetval elementStore:elementStore];
6767
xmlXPathFreeObject(queryResult);
6868
xmlFreeTextWriter(writer);

0 commit comments

Comments
 (0)