-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathappiumXpathBuilder.java
More file actions
254 lines (225 loc) · 19.5 KB
/
appiumXpathBuilder.java
File metadata and controls
254 lines (225 loc) · 19.5 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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
package com.prototest.appiumcore;
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.Point;
import java.util.ArrayList;
import java.util.List;
import java.util.Stack;
/**
* Created with IntelliJ IDEA.
* User: sethurban
* Date: 11/1/13
* Time: 10:30 AM
* This thing is going to find xpath's for page objects. So we can look up objects by their location which should not change as much
*/
public class appiumXpathBuilder {
private String source = "{\"name\":\"Makr α\",\"type\":\"UIAApplication\",\"label\":\"Makr α\",\"value\":null,\"rect\":{\"origin\":{\"x\":0,\"y\":0},\"size\":{\"width\":1024,\"height\":768}},\"dom\":null,\"enabled\":true,\"valid\":true,\"visible\":true,\"children\":[{\"name\":null,\"type\":\"UIAWindow\",\"label\":null,\"value\":null,\"rect\":{\"origin\":{\"x\":0,\"y\":0},\"size\":{\"width\":1024,\"height\":768}},\"dom\":null,\"enabled\":true,\"valid\":true,\"visible\":true,\"children\":[{\"name\":null,\"type\":\"UIAButton\",\"label\":null,\"value\":null,\"rect\":{\"origin\":{\"x\":0,\"y\":0},\"size\":{\"width\":55,\"height\":44}},\"dom\":null,\"enabled\":true,\"valid\":true,\"visible\":true,\"children\":[]},{\"name\":null,\"type\":\"UIAStaticText\",\"label\":\"\",\"value\":\"\",\"rect\":{\"origin\":{\"x\":67,\"y\":5},\"size\":{\"width\":250,\"height\":34}},\"dom\":null,\"enabled\":true,\"valid\":true,\"visible\":false,\"children\":[]},{\"name\":null,\"type\":\"UIAButton\",\"label\":null,\"value\":1,\"rect\":{\"origin\":{\"x\":380,\"y\":0},\"size\":{\"width\":44,\"height\":44}},\"dom\":null,\"enabled\":true,\"valid\":true,\"visible\":true,\"children\":[]},{\"name\":null,\"type\":\"UIAButton\",\"label\":null,\"value\":null,\"rect\":{\"origin\":{\"x\":454,\"y\":0},\"size\":{\"width\":44,\"height\":44}},\"dom\":null,\"enabled\":true,\"valid\":true,\"visible\":true,\"children\":[]},{\"name\":null,\"type\":\"UIAButton\",\"label\":null,\"value\":null,\"rect\":{\"origin\":{\"x\":528,\"y\":0},\"size\":{\"width\":44,\"height\":44}},\"dom\":null,\"enabled\":true,\"valid\":true,\"visible\":true,\"children\":[]},{\"name\":null,\"type\":\"UIAButton\",\"label\":null,\"value\":null,\"rect\":{\"origin\":{\"x\":602,\"y\":0},\"size\":{\"width\":44,\"height\":44}},\"dom\":null,\"enabled\":true,\"valid\":true,\"visible\":true,\"children\":[]},{\"name\":null,\"type\":\"UIAButton\",\"label\":null,\"value\":null,\"rect\":{\"origin\":{\"x\":925,\"y\":0},\"size\":{\"width\":32,\"height\":44}},\"dom\":null,\"enabled\":true,\"valid\":true,\"visible\":false,\"children\":[{\"name\":null,\"type\":\"UIAImage\",\"label\":null,\"value\":null,\"rect\":{\"origin\":{\"x\":925,\"y\":0},\"size\":{\"width\":32,\"height\":44}},\"dom\":null,\"enabled\":true,\"valid\":true,\"visible\":false,\"children\":[]}]},{\"name\":null,\"type\":\"UIAButton\",\"label\":null,\"value\":null,\"rect\":{\"origin\":{\"x\":968,\"y\":0},\"size\":{\"width\":44,\"height\":44}},\"dom\":null,\"enabled\":true,\"valid\":true,\"visible\":true,\"children\":[]},{\"name\":\"BUSINESS\",\"type\":\"UIAButton\",\"label\":\"BUSINESS\",\"value\":null,\"rect\":{\"origin\":{\"x\":0,\"y\":44},\"size\":{\"width\":170,\"height\":49}},\"dom\":null,\"enabled\":true,\"valid\":true,\"visible\":true,\"children\":[]},{\"name\":\"PARTIES\",\"type\":\"UIAButton\",\"label\":\"PARTIES\",\"value\":null,\"rect\":{\"origin\":{\"x\":171,\"y\":44},\"size\":{\"width\":170,\"height\":49}},\"dom\":null,\"enabled\":true,\"valid\":true,\"visible\":true,\"children\":[]},{\"name\":\"WEDDING\",\"type\":\"UIAButton\",\"label\":\"WEDDING\",\"value\":null,\"rect\":{\"origin\":{\"x\":342,\"y\":44},\"size\":{\"width\":170,\"height\":49}},\"dom\":null,\"enabled\":true,\"valid\":true,\"visible\":true,\"children\":[]},{\"name\":\"BABY\",\"type\":\"UIAButton\",\"label\":\"BABY\",\"value\":null,\"rect\":{\"origin\":{\"x\":513,\"y\":44},\"size\":{\"width\":170,\"height\":49}},\"dom\":null,\"enabled\":true,\"valid\":true,\"visible\":true,\"children\":[]},{\"name\":\"HOLIDAY\",\"type\":\"UIAButton\",\"label\":\"HOLIDAY\",\"value\":null,\"rect\":{\"origin\":{\"x\":684,\"y\":44},\"size\":{\"width\":170,\"height\":49}},\"dom\":null,\"enabled\":true,\"valid\":true,\"visible\":true,\"children\":[]},{\"name\":\"HOME\",\"type\":\"UIAButton\",\"label\":\"HOME\",\"value\":null,\"rect\":{\"origin\":{\"x\":855,\"y\":44},\"size\":{\"width\":169,\"height\":49}},\"dom\":null,\"enabled\":true,\"valid\":true,\"visible\":true,\"children\":[]},{\"name\":null,\"type\":\"UIAScrollView\",\"label\":null,\"value\":null,\"rect\":{\"origin\":{\"x\":0,\"y\":96},\"size\":{\"width\":1024,\"height\":672}},\"dom\":null,\"enabled\":true,\"valid\":true,\"visible\":true,\"children\":[{\"name\":null,\"type\":\"UIAScrollView\",\"label\":null,\"value\":null,\"rect\":{\"origin\":{\"x\":0,\"y\":96},\"size\":{\"width\":1024,\"height\":460}},\"dom\":null,\"enabled\":true,\"valid\":true,\"visible\":true,\"children\":[{\"name\":null,\"type\":\"UIAImage\",\"label\":null,\"value\":null,\"rect\":{\"origin\":{\"x\":-1024,\"y\":96},\"size\":{\"width\":1024,\"height\":460}},\"dom\":null,\"enabled\":true,\"valid\":true,\"visible\":false,\"children\":[]},{\"name\":null,\"type\":\"UIAImage\",\"label\":null,\"value\":null,\"rect\":{\"origin\":{\"x\":0,\"y\":96},\"size\":{\"width\":1024,\"height\":460}},\"dom\":null,\"enabled\":true,\"valid\":true,\"visible\":false,\"children\":[]},{\"name\":null,\"type\":\"UIAImage\",\"label\":null,\"value\":null,\"rect\":{\"origin\":{\"x\":1024,\"y\":96},\"size\":{\"width\":1024,\"height\":460}},\"dom\":null,\"enabled\":true,\"valid\":true,\"visible\":false,\"children\":[]},{\"name\":null,\"type\":\"UIAImage\",\"label\":null,\"value\":null,\"rect\":{\"origin\":{\"x\":2048,\"y\":96},\"size\":{\"width\":1024,\"height\":460}},\"dom\":null,\"enabled\":true,\"valid\":true,\"visible\":false,\"children\":[]},{\"name\":null,\"type\":\"UIAImage\",\"label\":null,\"value\":null,\"rect\":{\"origin\":{\"x\":3072,\"y\":96},\"size\":{\"width\":1024,\"height\":460}},\"dom\":null,\"enabled\":true,\"valid\":true,\"visible\":false,\"children\":[]}]},{\"name\":null,\"type\":\"UIAPageIndicator\",\"label\":null,\"value\":\"page 1 of 3\",\"rect\":{\"origin\":{\"x\":100,\"y\":526},\"size\":{\"width\":824,\"height\":35}},\"dom\":null,\"enabled\":true,\"valid\":true,\"visible\":true,\"children\":[]},{\"name\":\"FRESH IDEAS\",\"type\":\"UIAStaticText\",\"label\":\"FRESH IDEAS\",\"value\":\"FRESH IDEAS\",\"rect\":{\"origin\":{\"x\":465,\"y\":581},\"size\":{\"width\":94,\"height\":18}},\"dom\":null,\"enabled\":true,\"valid\":true,\"visible\":true,\"children\":[]},{\"name\":null,\"type\":\"UIAButton\",\"label\":null,\"value\":null,\"rect\":{\"origin\":{\"x\":22,\"y\":619},\"size\":{\"width\":310,\"height\":242}},\"dom\":null,\"enabled\":true,\"valid\":true,\"visible\":true,\"children\":[]},{\"name\":null,\"type\":\"UIAButton\",\"label\":null,\"value\":null,\"rect\":{\"origin\":{\"x\":357,\"y\":619},\"size\":{\"width\":310,\"height\":242}},\"dom\":null,\"enabled\":true,\"valid\":true,\"visible\":true,\"children\":[]},{\"name\":null,\"type\":\"UIAButton\",\"label\":null,\"value\":null,\"rect\":{\"origin\":{\"x\":692,\"y\":619},\"size\":{\"width\":310,\"height\":242}},\"dom\":null,\"enabled\":true,\"valid\":true,\"visible\":true,\"children\":[]},{\"name\":null,\"type\":\"UIAImage\",\"label\":null,\"value\":null,\"rect\":{\"origin\":{\"x\":1017,\"y\":763},\"size\":{\"width\":7,\"height\":5}},\"dom\":null,\"enabled\":true,\"valid\":true,\"visible\":false,\"children\":[]},{\"name\":null,\"type\":\"UIAImage\",\"label\":null,\"value\":null,\"rect\":{\"origin\":{\"x\":1019,\"y\":761},\"size\":{\"width\":5,\"height\":7}},\"dom\":null,\"enabled\":true,\"valid\":true,\"visible\":false,\"children\":[]},{\"name\":\"MOST POPULAR\",\"type\":\"UIAStaticText\",\"label\":\"MOST POPULAR\",\"value\":\"MOST POPULAR\",\"rect\":{\"origin\":{\"x\":457,\"y\":886},\"size\":{\"width\":110,\"height\":18}},\"dom\":null,\"enabled\":true,\"valid\":true,\"visible\":false,\"children\":[]},{\"name\":null,\"type\":\"UIAButton\",\"label\":null,\"value\":null,\"rect\":{\"origin\":{\"x\":22,\"y\":924},\"size\":{\"width\":310,\"height\":242}},\"dom\":null,\"enabled\":true,\"valid\":true,\"visible\":false,\"children\":[]},{\"name\":null,\"type\":\"UIAButton\",\"label\":null,\"value\":null,\"rect\":{\"origin\":{\"x\":357,\"y\":924},\"size\":{\"width\":310,\"height\":242}},\"dom\":null,\"enabled\":true,\"valid\":true,\"visible\":false,\"children\":[]},{\"name\":null,\"type\":\"UIAButton\",\"label\":null,\"value\":null,\"rect\":{\"origin\":{\"x\":692,\"y\":924},\"size\":{\"width\":310,\"height\":242}},\"dom\":null,\"enabled\":true,\"valid\":true,\"visible\":false,\"children\":[]}]}]},{\"name\":null,\"type\":\"UIAWindow\",\"label\":null,\"value\":null,\"rect\":{\"origin\":{\"x\":0,\"y\":-256},\"size\":{\"width\":768,\"height\":1024}},\"dom\":null,\"enabled\":true,\"valid\":true,\"visible\":false,\"children\":[{\"name\":null,\"type\":\"UIAStatusBar\",\"label\":null,\"value\":null,\"rect\":{\"origin\":{\"x\":0,\"y\":0},\"size\":{\"width\":1024,\"height\":20}},\"dom\":null,\"enabled\":true,\"valid\":true,\"visible\":false,\"children\":[{\"name\":\"iPad\",\"type\":\"UIAElement\",\"label\":\"iPad\",\"value\":null,\"rect\":{\"origin\":{\"x\":6,\"y\":0},\"size\":{\"width\":26,\"height\":20}},\"dom\":null,\"enabled\":true,\"valid\":true,\"visible\":false,\"children\":[]},{\"name\":\"3 of 3 Wi-Fi bars\",\"type\":\"UIAElement\",\"label\":\"3 of 3 Wi-Fi bars\",\"value\":\"ProtoTest\",\"rect\":{\"origin\":{\"x\":37,\"y\":0},\"size\":{\"width\":13,\"height\":20}},\"dom\":null,\"enabled\":true,\"valid\":true,\"visible\":false,\"children\":[]},{\"name\":\"1:23 PM\",\"type\":\"UIAElement\",\"label\":\"1:23 PM\",\"value\":null,\"rect\":{\"origin\":{\"x\":490,\"y\":0},\"size\":{\"width\":45,\"height\":20}},\"dom\":null,\"enabled\":true,\"valid\":true,\"visible\":false,\"children\":[]},{\"name\":\"Bluetooth on\",\"type\":\"UIAElement\",\"label\":\"Bluetooth on\",\"value\":null,\"rect\":{\"origin\":{\"x\":898,\"y\":0},\"size\":{\"width\":8,\"height\":20}},\"dom\":null,\"enabled\":true,\"valid\":true,\"visible\":false,\"children\":[]},{\"name\":\"Not charging\",\"type\":\"UIAElement\",\"label\":\"Not charging\",\"value\":null,\"rect\":{\"origin\":{\"x\":912,\"y\":0},\"size\":{\"width\":76,\"height\":20}},\"dom\":null,\"enabled\":true,\"valid\":true,\"visible\":false,\"children\":[]},{\"name\":\"69% battery power, Charging\",\"type\":\"UIAElement\",\"label\":\"69% battery power, Charging\",\"value\":null,\"rect\":{\"origin\":{\"x\":994,\"y\":0},\"size\":{\"width\":25,\"height\":20}},\"dom\":null,\"enabled\":true,\"valid\":true,\"visible\":false,\"children\":[]}]}]}]}";
private List<Point> Locations;
private List<String> xpaths;
private List<String> elements;
private String ParentTerminator = "}]";
private Integer intWindow = 1;
private Stack WindowStack = new Stack();
private Integer intButton = 1;
private Stack ButtonStack = new Stack();
private Integer intStaticText = 1;
private Stack StaticTextStack = new Stack();
private Integer intScrollView = 1;
private Stack ScrollViewStack = new Stack();
private Integer intTextfield = 1;
private Stack TextfieldStack = new Stack();
private Integer intSecureTextField = 1;
private Stack SecureTextFieldStack = new Stack();
private Integer intImage = 1;
private Stack ImageStack = new Stack();
private Integer intStatusBar = 1;
private Stack StatusBarStack = new Stack();
public appiumXpathBuilder(){
getPageSource();
split();
CalculateXpaths();
CalculateLocations();
}
private void getPageSource() {
//This function will get the page source of whatever screen the user is on
//This is called from the ScreenBase class
JavascriptExecutor js = (JavascriptExecutor) appiumTestBase.getDriver();
source = (String) js.executeScript("wd_frame.getPageSource()");
}
private void CalculateLocations() {
//This function will parse the element strings and stip out the location strings, parse them into Points and add them to the locations array
//the locations and xpaths array are treated as parellel arrays
Locations = new ArrayList<Point>();
String XFinder = "\"x\":";
String YFinder = "\"y\":";
String Xloc = "";
String Yloc = "";
Point Location;
for(int i = 0; i < elements.size(); i++){
int XlocStart = elements.get(i).indexOf(XFinder);
int DividLoc = elements.get(i).indexOf(',', XlocStart);
int YlocStart = elements.get(i).indexOf(YFinder);
int Yend = elements.get(i).indexOf('}', YlocStart);
Xloc = elements.get(i).substring(XlocStart + XFinder.length(), DividLoc);
Yloc = elements.get(i).substring(YlocStart + YFinder.length(), Yend);
Location = new Point(Integer.valueOf(Xloc), Integer.valueOf(Yloc));
Locations.add(Location);
}
}
private void split(){
//Split the source into it's basic components
String[] firstsplit = source.split("\\},\\{");
List<String[]> secondsplit = new ArrayList<String[]>();
//for each component in the array, split them into parents and siblings
for(int i = 0; i < firstsplit.length; i++){
String[] things = firstsplit[i].split("\\[\\{");
if(things.length > 0){
secondsplit.add(things);
}
}
//let's simplify some things here -- add all the split strings to a new list
elements = new ArrayList<String>();
for(int i = 0; i < secondsplit.size(); i++){
for(int x = 0; x < secondsplit.get(i).length; x++){
elements.add(secondsplit.get(i)[x]);
}
}
}
private void CalculateXpaths(){
//so now we should just be able to iterate through the array to build xpath locators
//The first element in the array is assumed to be the parent of everything below
xpaths = new ArrayList<String>();
String ParentName = "//";
//now we need some way to determine how many of each thing is on the screen
//Now were going to go through each string and build the appropriate xpath locator and parse the origin point
for(int i = 0; i < elements.size(); i++){
String xpath = "";
if((elements.get(i).length() - 1) == elements.get(i).lastIndexOf(':')){
//this is a parent -- set the parent name
ParentName += TypeSelector(elements.get(i));
ParentName += "//";
xpath = ParentName;
SaveValues();
}
else{
if(elements.get(i).indexOf(ParentTerminator) == -1){
String theType = TypeSelector(elements.get(i));
xpath = ParentName + theType;
}
}
if(elements.get(i).indexOf(ParentTerminator) != -1){ //this string contains at least one parent terminator
//we need to figure out if there is more than one parent terminator -- so we'll remove the substring of the initial parent terminator and check for it again
xpath = ParentName + TypeSelector(elements.get(i));
while (elements.get(i).indexOf(ParentTerminator) != -1){
String thing = elements.get(i).replaceFirst(ParentTerminator, "");
elements.set(i, thing); //This is the weird Java way to change an element in the Array list
//need to update the parent string now to close the previous parent
String[] Parents = ParentName.split("//");
//need to check this.
ParentName = "";
for(int x = 0; x < Parents.length - 1; x++){
//we don't want the last element in the array since we found a parent terminator
ParentName += Parents[x] + "//";
}
ResetValues();
}
}
//we want to strip off the //Application Part and any trailing //
String app = "//Application";
String stripped = xpath.substring(app.length(), xpath.length());
xpaths.add(stripped);
}
}
private String TypeSelector(String raw){
String type = "UIA";
int typeloc = raw.indexOf(type) + type.length();
int typeend = raw.indexOf('\"', typeloc);
String selectedType = raw.substring(typeloc, typeend);
String TypeandID = selectedType + PrintID(selectedType);
return TypeandID;
}
private String PrintID(String type){
//TODO add all the other locators here
String thenumber = "";
if(type.equals("Window")){
thenumber = "[" + String.valueOf(intWindow) + "]";
intWindow += 1;
}
if(type.equals("Button")){
thenumber = "[" + String.valueOf(intButton) + "]";
intButton += 1;
}
if(type.equals("StaticText")){
thenumber = "[" + String.valueOf(intStaticText) + "]";
intStaticText += 1;
}
if(type.equals("ScrollView")){
thenumber = "[" + String.valueOf(intScrollView) + "]";
intScrollView += 1;
}
if(type.equals("TextField")){
thenumber = "[" + String.valueOf(intTextfield) + "]";
intTextfield += 1;
}
if(type.equals("SecureTextField")){
thenumber = "[" + String.valueOf(intSecureTextField) + "]";
intSecureTextField += 1;
}
if(type.equals("Image")){
thenumber = "[" + String.valueOf(intImage) + "]";
intImage += 1;
}
if(type.equals("StatusBar")){
thenumber = "[" + String.valueOf(intStatusBar) + "]";
intStatusBar += 1;
}
return thenumber;
}
private void SaveValues(){
WindowStack.push(intWindow);
ButtonStack.push(intButton);
StaticTextStack.push(intStaticText);
ScrollViewStack.push(intScrollView);
ImageStack.push(intImage);
StatusBarStack.push(intStatusBar);
TextfieldStack.push(intTextfield);
SecureTextFieldStack.push(intSecureTextField);
//reset the values of the ints to 1 now that we saved them previously
intWindow = 1;
intButton = 1;
intStaticText = 1;
intScrollView = 1;
intImage = 1;
intStatusBar = 1;
intTextfield = 1;
intSecureTextField = 1;
}
private void ResetValues(){
intWindow = (Integer) WindowStack.pop();
intButton = (Integer) ButtonStack.pop();
intStaticText = (Integer) StaticTextStack.pop();
intScrollView = (Integer) ScrollViewStack.pop();
intImage = (Integer) ImageStack.pop();
intStatusBar = (Integer) StatusBarStack.pop();
intTextfield = (Integer) TextfieldStack.pop();
intSecureTextField = (Integer) SecureTextFieldStack.pop();
}
public String FindByLocation(int X, int Y){
int indexFound = 0;
boolean foundMatch = false;
String xpathFound = "";
for(int i = 0; i < Locations.size(); i++){
if((Locations.get(i).x == X) && (Locations.get(i).y == Y)){
indexFound = i;
foundMatch = true;
i = Locations.size(); //to escape the loop once the matching index is found
}
}
if(foundMatch){
xpathFound = xpaths.get(indexFound);
}
else{
System.out.println("Searching for xPath at Location did not find an element. Try again.");
}
return xpathFound;
}
}