/
ActorBrowser.java
257 lines (208 loc) · 8.24 KB
/
ActorBrowser.java
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
255
256
257
package net.selenate.client.user;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import net.selenate.common.comms.*;
import net.selenate.common.comms.req.*;
import net.selenate.common.comms.res.*;
import net.selenate.common.user.*;
import akka.actor.ActorRef;
public class ActorBrowser extends ActorBase implements IBrowser {
public ActorBrowser(final ActorRef session) {
super(session);
}
@Override
public void open(final String url) throws IOException {
typedBlock(new SeReqGet(url), SeResGet.class);
}
@Override
public Capture capture(final String name) throws IOException {
final SeResCapture res = typedBlock(new SeReqCapture(name), SeResCapture.class);
return resToUserCapture(res);
}
@Override
public Capture capture(final String name, final boolean takeScreenshot) throws IOException {
final SeResCapture res = typedBlock(new SeReqCapture(name, takeScreenshot), SeResCapture.class);
return resToUserCapture(res);
}
@Override
public byte[] captureWindow(final ElementSelector selector) throws IOException {
final SeElementSelectMethod reqMethod = userToReqElementSelectMethod(selector.method);
final SeResCaptureWindow res = typedBlock(
new SeReqCaptureWindow(reqMethod, selector.query),
SeResCaptureWindow.class);
return res.screenshot;
}
@Override
public String executeScript(final String javascript) throws IOException {
final SeResExecuteScript res = typedBlock(new SeReqExecuteScript(javascript), SeResExecuteScript.class);
return res.result;
}
@Override
public void resetFrame() throws IOException {
typedBlock(new SeReqResetFrame(), SeResResetFrame.class);
}
@Override
public void switchFrame(final int frame) throws IOException {
typedBlock(new SeReqSwitchFrame(frame), SeResSwitchFrame.class);
}
@Override
public void switchFrame(ElementSelectMethod method, String query) throws IOException {
switchFrame(new ElementSelector(method, query));
}
@Override
public void switchFrame(ElementSelector selector) throws IOException {
final SeElementSelector reqSelector = userToReqSelector(selector);
typedBlock(new SeReqSwitchFrame(reqSelector), SeResSwitchFrame.class);
}
@Override
public boolean waitFor(final List<ElementSelector> selectorList) throws IOException {
return waitFor(new BrowserPage("default", selectorList));
}
@Override
public boolean waitFor(final ElementSelector... selectorList) throws IOException {
return waitFor(new BrowserPage("default", new ArrayList<ElementSelector>(Arrays.asList(selectorList))));
}
@Override
public boolean waitFor(final BrowserPage page) throws IOException {
final List<BrowserPage> pageList = new ArrayList<BrowserPage>();
pageList.add(page);
String res = waitForAny(pageList);
return (res != null);
}
@Override
public String waitForAny(BrowserPage ... pageList) throws IOException {
final BrowserPage resPage = waitForAnyPage(Arrays.asList(pageList));
if(resPage == null) return null;
return resPage.name;
}
@Override
public String waitForAny(final List<BrowserPage> pageList) throws IOException {
final BrowserPage resPage = waitForAnyPage(pageList);
if(resPage == null) return null;
return resPage.name;
}
@Override
public void quit() throws IOException {
typedBlock(new SeReqQuit(), SeResQuit.class);
}
@Override
public boolean elementExists(
final ElementSelectMethod method,
final String query)
throws IOException {
final SeElementSelectMethod reqMethod = userToReqElementSelectMethod(method);
final SeResElementExists res = typedBlock(new SeReqElementExists(reqMethod, query), SeResElementExists.class);
return res.isFound;
}
@Override
public boolean elementExists(final ElementSelector selector) throws IOException {
return elementExists(selector.method, selector.query);
}
@Override
public IElement findElement(
final ElementSelectMethod method,
final String query)
throws IOException {
final SeElementSelectMethod reqMethod = userToReqElementSelectMethod(method);
final SeResFindElement res = typedBlock(new SeReqFindElement(reqMethod, query), SeResFindElement.class);
return resToUserElement(res.element);
}
@Override
public IElement findElement(final ElementSelector selector) throws IOException {
return findElement(selector.method, selector.query);
}
@Override
public List<IElement> findElementList(
final ElementSelectMethod method,
final String query)
throws IOException {
final SeElementSelectMethod reqMethod = userToReqElementSelectMethod(method);
final SeResFindElementList res = typedBlock(new SeReqFindElementList(reqMethod, query), SeResFindElementList.class);
return resToUserElementList(res.elementList);
}
@Override
public List<IElement> findElementList(final ElementSelector selector) throws IOException {
return findElementList(selector.method, selector.query);
}
@Override
public String findAlert() throws IOException {
final SeResFindAlert res = typedBlock(new SeReqFindAlert(), SeResFindAlert.class);
return res.text;
}
@Override
public void deleteCookieNamed(String name) throws IOException {
typedBlock(new SeReqDeleteCookieNamed(name), SeResDeleteCookieNamed.class);
}
@Override
public void addCookie(Cookie cookie) throws IOException {
typedBlock(new SeReqAddCookie(cookie), SeResAddCookie.class);
}
@Override
public INavigation navigate() throws IOException {
return new ActorNavigation();
}
@Override
public byte[] download(String url) throws IOException {
final SeResDownload res = typedBlock(new SeReqDownload(url), SeResDownload.class);
return res.body;
}
@Override
public void startKeepaliveClick(long delayMillis, ElementSelector selector) throws IOException {
startKeepaliveClick(delayMillis, selector.method, selector.query);
}
@Override
public void startKeepaliveClick(long delayMillis, ElementSelectMethod method, String query) throws IOException {
final SeElementSelectMethod reqMethod = userToReqElementSelectMethod(method);
final List<SeCommsReq> reqList = new ArrayList<SeCommsReq>();
reqList.add(new SeReqFindAndClick(reqMethod, query));
startKeepalive(delayMillis, reqList);
}
@Override
public void startKeepalive(long delayMillis, SeCommsReq... reqList) throws IOException {
startKeepalive(delayMillis, Arrays.asList(reqList));
}
@Override
public void startKeepalive(long delayMillis, List<SeCommsReq> reqList) throws IOException {
typedBlock(new SeReqStartKeepalive(delayMillis, reqList), SeResStartKeepalive.class);
}
@Override
public void stopKeepalive() throws IOException {
typedBlock(new SeReqStopKeepalive(), SeResStopKeepalive.class);
}
private class ActorNavigation implements INavigation {
@Override
public void back() throws IOException {
typedBlock(new SeReqNavigateBack(), SeResNavigateBack.class);
}
@Override
public void forward() throws IOException {
typedBlock(new SeReqNavigateForward(), SeResNavigateForward.class);
}
@Override
public void refresh() throws IOException {
typedBlock(new SeReqNavigateRefresh(), SeResNavigateRefresh.class);
}
}
@Override
public BrowserPage waitForAnyPage(BrowserPage... pageList) throws IOException {
return waitForAnyPage(Arrays.asList(pageList));
}
@Override
public BrowserPage waitForAnyPage(List<BrowserPage> pageList) throws IOException {
final SeResWaitForBrowserPage res = typedBlock(new SeReqWaitForBrowserPage(userToReqPageList(pageList)), SeResWaitForBrowserPage.class);
return res.isSuccessful ? res.foundPage : null;
}
@Override
public void setAutoFrames(final Boolean useFrames) throws IOException {
typedBlock(new SeReqSetUseFrames(useFrames), SeResSetUseFrames.class);
}
public boolean waitForSikuliImage(final byte[] image) throws IOException {
final SeResWaitForSikuliImage res = typedBlock(new SeReqWaitForSikuliImage(image, 30000), SeResWaitForSikuliImage.class);
return res.isImageFound();
}
public void clickSikuliImage(final byte[] image) throws IOException {
typedBlock(new SeReqClickSikuliImage(image, 30000), SeResClickSikuliImage.class);
}
}