forked from appium/WebDriverAgent
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFBXPathTests.m
More file actions
153 lines (135 loc) · 7.8 KB
/
FBXPathTests.m
File metadata and controls
153 lines (135 loc) · 7.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
/**
* Copyright (c) 2015-present, Facebook, Inc.
* All rights reserved.
*
* This source code is licensed under the BSD-style license found in the
* LICENSE file in the root directory of this source tree. An additional grant
* of patent rights can be found in the PATENTS file in the same directory.
*/
#import <XCTest/XCTest.h>
#import "FBMacros.h"
#import "FBXPath.h"
#import "FBXPath-Private.h"
#import "XCUIElementDouble.h"
#import "XCElementSnapshotDouble.h"
#import "FBXCElementSnapshotWrapper+Helpers.h"
@interface FBXPathTests : XCTestCase
@end
@implementation FBXPathTests
- (NSString *)xmlStringWithElement:(id<FBXCElementSnapshot>)snapshot
xpathQuery:(nullable NSString *)query
excludingAttributes:(nullable NSArray<NSString *> *)excludedAttributes
{
xmlDocPtr doc;
xmlTextWriterPtr writer = xmlNewTextWriterDoc(&doc, 0);
NSMutableDictionary *elementStore = [NSMutableDictionary dictionary];
int buffersize;
xmlChar *xmlbuff = NULL;
int rc = xmlTextWriterStartDocument(writer, NULL, "UTF-8", NULL);
if (rc >= 0) {
rc = [FBXPath xmlRepresentationWithRootElement:snapshot
writer:writer
elementStore:elementStore
query:query
excludingAttributes:excludedAttributes];
if (rc >= 0) {
rc = xmlTextWriterEndDocument(writer);
}
}
if (rc >= 0) {
xmlDocDumpFormatMemory(doc, &xmlbuff, &buffersize, 1);
}
xmlFreeTextWriter(writer);
xmlFreeDoc(doc);
XCTAssertTrue(rc >= 0);
XCTAssertEqual(1, [elementStore count]);
NSString *result = [NSString stringWithCString:(const char *)xmlbuff encoding:NSUTF8StringEncoding];
xmlFree(xmlbuff);
return result;
}
- (void)testDefaultXPathPresentation
{
XCElementSnapshotDouble *snapshot = [XCElementSnapshotDouble new];
id<FBElement> element = (id<FBElement>)[FBXCElementSnapshotWrapper ensureWrapped:(id)snapshot];
NSString *resultXml = [self xmlStringWithElement:(id<FBXCElementSnapshot>)element
xpathQuery:nil
excludingAttributes:nil];
NSLog(@"[DefaultXPath] Result XML:\n%@", resultXml);
NSString *expectedXml = [NSString stringWithFormat:@"<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<%@ type=\"%@\" value=\"%@\" name=\"%@\" label=\"%@\" enabled=\"%@\" visible=\"%@\" accessible=\"%@\" x=\"%@\" y=\"%@\" width=\"%@\" height=\"%@\" index=\"%lu\" placeholderValue=\"%@\" traits=\"%@\" private_indexPath=\"top\"/>\n",
element.wdType, element.wdType, element.wdValue, element.wdName, element.wdLabel, FBBoolToString(element.wdEnabled), FBBoolToString(element.wdVisible), FBBoolToString(element.wdAccessible), element.wdRect[@"x"], element.wdRect[@"y"], element.wdRect[@"width"], element.wdRect[@"height"], element.wdIndex, element.wdPlaceholderValue, element.wdTraits];
XCTAssertTrue([resultXml isEqualToString: expectedXml]);
}
- (void)testtXPathPresentationWithSomeAttributesExcluded
{
XCElementSnapshotDouble *snapshot = [XCElementSnapshotDouble new];
id<FBElement> element = (id<FBElement>)[FBXCElementSnapshotWrapper ensureWrapped:(id)snapshot];
NSString *resultXml = [self xmlStringWithElement:(id<FBXCElementSnapshot>)element
xpathQuery:nil
excludingAttributes:@[@"type", @"visible", @"value", @"index", @"traits", @"nativeFrame"]];
NSString *expectedXml = [NSString stringWithFormat:@"<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<%@ name=\"%@\" label=\"%@\" enabled=\"%@\" accessible=\"%@\" x=\"%@\" y=\"%@\" width=\"%@\" height=\"%@\" placeholderValue=\"%@\" private_indexPath=\"top\"/>\n",
element.wdType, element.wdName, element.wdLabel, FBBoolToString(element.wdEnabled), FBBoolToString(element.wdAccessible), element.wdRect[@"x"], element.wdRect[@"y"], element.wdRect[@"width"], element.wdRect[@"height"], element.wdPlaceholderValue];
XCTAssertEqualObjects(resultXml, expectedXml);
}
- (void)testXPathPresentationBasedOnQueryMatchingAllAttributes
{
XCElementSnapshotDouble *snapshot = [XCElementSnapshotDouble new];
snapshot.value = @"йоло<>&\"";
snapshot.label = @"a\nb";
id<FBElement> element = (id<FBElement>)[FBXCElementSnapshotWrapper ensureWrapped:(id)snapshot];
NSString *resultXml = [self xmlStringWithElement:(id<FBXCElementSnapshot>)element
xpathQuery:[NSString stringWithFormat:@"//%@[@*]", element.wdType]
excludingAttributes:@[@"visible"]];
NSString *expectedXml = [NSString stringWithFormat:@"<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<%@ type=\"%@\" value=\"%@\" name=\"%@\" label=\"%@\" enabled=\"%@\" visible=\"%@\" accessible=\"%@\" x=\"%@\" y=\"%@\" width=\"%@\" height=\"%@\" index=\"%lu\" hittable=\"%@\" placeholderValue=\"%@\" traits=\"%@\" nativeFrame=\"%@\" private_indexPath=\"top\"/>\n",
element.wdType, element.wdType, @"йоло<>&"", element.wdName, @"a b", FBBoolToString(element.wdEnabled), FBBoolToString(element.wdVisible), FBBoolToString(element.wdAccessible), element.wdRect[@"x"], element.wdRect[@"y"], element.wdRect[@"width"], element.wdRect[@"height"], element.wdIndex, FBBoolToString(element.wdHittable), element.wdPlaceholderValue, element.wdTraits, NSStringFromCGRect(element.wdNativeFrame)];
XCTAssertEqualObjects(expectedXml, resultXml);
}
- (void)testXPathPresentationBasedOnQueryMatchingSomeAttributes
{
XCElementSnapshotDouble *snapshot = [XCElementSnapshotDouble new];
id<FBElement> element = (id<FBElement>)[FBXCElementSnapshotWrapper ensureWrapped:(id)snapshot];
NSString *resultXml = [self xmlStringWithElement:(id<FBXCElementSnapshot>)element
xpathQuery:[NSString stringWithFormat:@"//%@[@%@ and contains(@%@, 'blabla')]", element.wdType, @"value", @"name"]
excludingAttributes:nil];
NSString *expectedXml = [NSString stringWithFormat:@"<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<%@ value=\"%@\" name=\"%@\" private_indexPath=\"top\"/>\n",
element.wdType, element.wdValue, element.wdName];
XCTAssertTrue([resultXml isEqualToString: expectedXml]);
}
- (void)testSnapshotXPathResultsMatching
{
xmlDocPtr doc;
xmlTextWriterPtr writer = xmlNewTextWriterDoc(&doc, 0);
NSMutableDictionary *elementStore = [NSMutableDictionary dictionary];
XCElementSnapshotDouble *snapshot = [XCElementSnapshotDouble new];
id<FBElement> root = (id<FBElement>)[FBXCElementSnapshotWrapper ensureWrapped:(id)snapshot];
NSString *query = [NSString stringWithFormat:@"//%@", root.wdType];
int rc = xmlTextWriterStartDocument(writer, NULL, "UTF-8", NULL);
if (rc >= 0) {
rc = [FBXPath xmlRepresentationWithRootElement:(id<FBXCElementSnapshot>)root
writer:writer
elementStore:elementStore
query:query
excludingAttributes:nil];
if (rc >= 0) {
rc = xmlTextWriterEndDocument(writer);
}
}
if (rc < 0) {
xmlFreeTextWriter(writer);
xmlFreeDoc(doc);
XCTFail(@"Unable to create the source XML document");
}
xmlXPathObjectPtr queryResult = [FBXPath evaluate:query document:doc contextNode:NULL];
if (NULL == queryResult) {
xmlFreeTextWriter(writer);
xmlFreeDoc(doc);
XCTAssertNotEqual(NULL, queryResult);
}
NSArray *matchingSnapshots = [FBXPath collectMatchingSnapshots:queryResult->nodesetval
elementStore:elementStore];
xmlXPathFreeObject(queryResult);
xmlFreeTextWriter(writer);
xmlFreeDoc(doc);
XCTAssertNotNil(matchingSnapshots);
XCTAssertEqual(1, [matchingSnapshots count]);
}
@end