-
Notifications
You must be signed in to change notification settings - Fork 9
/
ActorBase.java
251 lines (209 loc) · 8.57 KB
/
ActorBase.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
package net.selenate.client.user;
import java.io.IOException;
import java.util.*;
import java.util.concurrent.TimeUnit;
import net.selenate.common.comms.*;
import net.selenate.common.comms.res.SeResCapture;
import net.selenate.common.user.*;
import akka.actor.ActorRef;
import akka.pattern.Patterns;
import akka.util.Timeout;
import scala.concurrent.*;
import scala.concurrent.duration.Duration;
public abstract class ActorBase {
protected Timeout timeout = new Timeout(30, TimeUnit.SECONDS);
protected final ActorRef session;
public ActorBase(final ActorRef session) {
if (session == null) {
throw new IllegalArgumentException("Session cannot be null!");
}
this.session = session;
}
protected Object block(final Object req) throws IOException {
try {
final Future<Object> future = Patterns.ask(session, req, timeout);
final Object result = Await.result(future, new Timeout(32, TimeUnit.SECONDS).duration());
return result;
} catch (final Exception e) {
throw new IOException(String.format("An error occured while sending the message to remote actor!\nMessage: %s", req.toString()), e);
}
}
public void gracefullStop() throws IOException {
try {
Future<Boolean> stopped = akka.pattern.Patterns.gracefulStop(session, Duration.create(5, TimeUnit.SECONDS), ActorFactory.system);
Await.result(stopped, Duration.create(6, TimeUnit.SECONDS));
} catch (Exception e) {
throw new IOException("Error while trying to gracefully stop the actor session.", e);
}
}
protected <T> T typedBlock(
final Object req,
final Class<T> clazz) throws IOException {
final Object obj = block(req);
try {
final T res = clazz.cast(obj);
return res;
} catch (final ClassCastException e0) {
try {
final Exception ex = Exception.class.cast(obj);
throw new IOException("Received an exception!", ex);
} catch (final ClassCastException e1) {
throw new IOException(String.format("Received an unexpected response! Found: %s; required: %s.", obj.getClass().toString(), clazz.toString()), e0);
}
}
}
protected SeElementSelectMethod userToReqElementSelectMethod(final ElementSelectMethod userMethod) {
final SeElementSelectMethod reqMethod;
switch (userMethod) {
case CLASS_NAME: reqMethod = SeElementSelectMethod.CLASS_NAME; break;
case CSS_SELECTOR: reqMethod = SeElementSelectMethod.CSS_SELECTOR; break;
case ID: reqMethod = SeElementSelectMethod.ID; break;
case LINK_TEXT: reqMethod = SeElementSelectMethod.LINK_TEXT; break;
case NAME: reqMethod = SeElementSelectMethod.NAME; break;
case PARTIAL_LINK_TEXT: reqMethod = SeElementSelectMethod.PARTIAL_LINK_TEXT; break;
case TAG_NAME: reqMethod = SeElementSelectMethod.TAG_NAME; break;
case UUID: reqMethod = SeElementSelectMethod.UUID; break;
case XPATH: reqMethod = SeElementSelectMethod.XPATH; break;
default: throw new RuntimeException("Unexpected error!");
}
return reqMethod;
}
protected SeOptionSelectMethod userToReqOptionSelectMethod(final OptionSelectMethod userMethod) {
final SeOptionSelectMethod reqMethod;
switch (userMethod) {
case INDEX: reqMethod = SeOptionSelectMethod.INDEX; break;
case VALUE: reqMethod = SeOptionSelectMethod.VALUE; break;
case VISIBLE_TEXT: reqMethod = SeOptionSelectMethod.VISIBLE_TEXT; break;
default: throw new RuntimeException("Unexpected error!");
}
return reqMethod;
}
protected Capture resToUserCapture(final SeResCapture resCapture) {
return new Capture(
resCapture.name,
resCapture.time,
resToUserCookieList(resCapture.cookieList),
resToUserWindowList(resCapture.windowList));
}
protected CaptureCookie resToUserCookie(final SeCookie resCookie) {
return new CaptureCookie(
resCookie.domain,
resCookie.expiry,
resCookie.name,
resCookie.path,
resCookie.value,
resCookie.isSecure);
}
protected Set<CaptureCookie> resToUserCookieList(final Set<SeCookie> resCookieList) {
final Set<CaptureCookie> userCookieList = new HashSet<CaptureCookie>();
for (final SeCookie resCookie : resCookieList) {
final CaptureCookie cookie = resToUserCookie(resCookie);
userCookieList.add(cookie);
}
return userCookieList;
}
protected CaptureWindow resToUserWindow(final SeWindow resWindow) {
return new CaptureWindow(
resWindow.title,
resWindow.url,
resWindow.handle,
new Position(resWindow.posX, resWindow.posY),
new Location(resWindow.width, resWindow.height),
resWindow.html,
resWindow.screenshot,
resToUserFrameList(resWindow.frameList));
}
protected List<CaptureWindow> resToUserWindowList(final List<SeWindow> resWindowList) {
final List<CaptureWindow> userWindowList = new ArrayList<CaptureWindow>();
for (final SeWindow resWindow : resWindowList) {
final CaptureWindow window = resToUserWindow(resWindow);
userWindowList.add(window);
}
return userWindowList;
}
protected CaptureFrame resToUserFrame(final SeFrame resFrame) {
return new CaptureFrame(
resFrame.index,
resFrame.name,
resFrame.src,
resFrame.html,
resToUserFrameList(resFrame.frameList));
}
protected List<CaptureFrame> resToUserFrameList(final List<SeFrame> resFrameList) {
final List<CaptureFrame> userFrameList = new ArrayList<CaptureFrame>();
for (final SeFrame resFrame : resFrameList) {
final CaptureFrame frame = resToUserFrame(resFrame);
userFrameList.add(frame);
}
return userFrameList;
}
protected ActorElement resToUserElement(final SeElement resElement) {
return new ActorElement(
session,
resElement.uuid,
new Position(resElement.posX, resElement.posY),
new Location(resElement.width, resElement.height),
resElement.name,
resElement.text,
resElement.isDisplayed,
resElement.isEnabled,
resElement.isSelected,
resElement.windowHandle,
resElement.framePath,
resElement.attributeList,
new ArrayList<IElement>());
}
protected List<IElement> resToUserElementList(final List<SeElement> resElementList) {
final List<IElement> userElementList = new ArrayList<IElement>();
for (final SeElement resElement : resElementList) {
final ActorElement actorElement = resToUserElement(resElement);
userElementList.add(actorElement);
}
return userElementList;
}
protected SeElementSelector userToReqSelector(final ElementSelector userSelector) {
return new SeElementSelector(userToReqElementSelectMethod(userSelector.method), userSelector.query);
}
protected List<SeElementSelector> userToReqSelectorList(final List<ElementSelector> userSelectorList) {
final List<SeElementSelector> reqSelectorList = new ArrayList<SeElementSelector>();
for (final ElementSelector userSelector : userSelectorList) {
final SeElementSelector reqSelector = userToReqSelector(userSelector);
reqSelectorList.add(reqSelector);
}
return reqSelectorList;
}
protected List<SePage> userToReqPageList(final List<BrowserPage> userPageList) {
final List<SePage> reqPageList = new ArrayList<SePage>();
for (final BrowserPage userPage : userPageList) {
final SePage reqPage = new SePage(userPage.name, userToReqSelectorList(userPage.selectorList));
reqPageList.add(reqPage);
}
return reqPageList;
}
protected ActorSelect resToUserSelect(final SeSelect resSelect) {
return new ActorSelect(
session,
resToUserElement(resSelect.element),
resSelect.optionCount,
resSelect.selectedIndex,
resToUserOption(resSelect.selectedOption),
resToUserOptionList(resSelect.options));
}
protected ActorOption resToUserOption(final SeOption resOption) {
if (resOption == null) {
return null;
} else {
return new ActorOption(
session,
resToUserElement(resOption.element));
}
}
protected List<IOption> resToUserOptionList(final List<SeOption> resOptionList) {
final List<IOption> userOptionList = new ArrayList<IOption>();
for (final SeOption resOption : resOptionList) {
final ActorOption actorOption = resToUserOption(resOption);
userOptionList.add(actorOption);
}
return userOptionList;
}
}