Skip to content

Commit d24f077

Browse files
committed
creating a new server side driver using the force native flag to prevent un necessary context switching
1 parent dc94d5a commit d24f077

File tree

6 files changed

+114
-54
lines changed

6 files changed

+114
-54
lines changed

client/src/main/java/org/uiautomation/ios/client/uiamodels/impl/AttachRemoteUIADriver.java

+7-2
Original file line numberDiff line numberDiff line change
@@ -13,16 +13,20 @@
1313
*/
1414
package org.uiautomation.ios.client.uiamodels.impl;
1515

16-
import java.net.URL;
17-
import java.util.Map;
16+
import com.google.common.collect.ImmutableBiMap;
17+
import com.google.common.collect.ImmutableMap;
1818

1919
import org.openqa.selenium.Capabilities;
2020
import org.openqa.selenium.remote.HttpCommandExecutor;
2121
import org.openqa.selenium.remote.SessionId;
2222
import org.uiautomation.ios.IOSCapabilities;
23+
import org.uiautomation.ios.communication.Path;
2324
import org.uiautomation.ios.communication.WebDriverLikeCommand;
2425
import org.uiautomation.ios.communication.WebDriverLikeRequest;
2526

27+
import java.net.URL;
28+
import java.util.Map;
29+
2630
public class AttachRemoteUIADriver extends RemoteUIADriver {
2731

2832
@Override
@@ -55,4 +59,5 @@ private IOSCapabilities loadCapabilities() {
5559
return new IOSCapabilities(c);
5660
}
5761

62+
5863
}

client/src/main/java/org/uiautomation/ios/client/uiamodels/impl/RemoteUIADriver.java

+4-7
Original file line numberDiff line numberDiff line change
@@ -285,9 +285,6 @@ private <T> T cast(Object o) {
285285
if (o == null) {
286286
return null;
287287
}
288-
if (o instanceof String) {
289-
return (T) o;
290-
}
291288
if (o instanceof Map) {
292289
Map<String, Object> map = (Map<String, Object>) o;
293290
if (map.containsKey("ELEMENT")) {
@@ -311,8 +308,7 @@ private <T> T cast(Object o) {
311308
}
312309
return (T) res;
313310
}
314-
315-
return null;
311+
return (T) o;
316312
}
317313

318314
/*
@@ -351,12 +347,13 @@ public Object executeScript(String script, Object... args) {
351347
Iterable<Object>
352348
convertedArgs =
353349
Iterables.transform(Lists.newArrayList(args), new WebElementToJsonConverter());
354-
355350
Map<String, ?>
356351
params =
357352
ImmutableMap.of("script", script, "args", Lists.newArrayList(convertedArgs));
353+
WebDriverLikeRequest request = buildRequest(WebDriverLikeCommand.EXECUTE_SCRIPT, params);
358354

359-
return execute(DriverCommand.EXECUTE_SCRIPT, params).getValue();
355+
return execute(request);
356+
//return execute(DriverCommand.EXECUTE_SCRIPT, params).getValue();
360357
}
361358

362359
@Override
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
/*
2+
* Copyright 2013 ios-driver committers.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
5+
* in compliance with the License. You may obtain a copy of the License at
6+
*
7+
* http://www.apache.org/licenses/LICENSE-2.0
8+
*
9+
* Unless required by applicable law or agreed to in writing, software distributed under the License
10+
* is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
11+
* or implied. See the License for the specific language governing permissions and limitations under
12+
* the License.
13+
*/
14+
15+
package org.uiautomation.ios.client.uiamodels.impl;
16+
17+
18+
import com.google.common.collect.ImmutableMap;
19+
20+
import org.openqa.selenium.remote.SessionId;
21+
import org.uiautomation.ios.communication.Path;
22+
import org.uiautomation.ios.communication.WebDriverLikeCommand;
23+
import org.uiautomation.ios.communication.WebDriverLikeRequest;
24+
25+
import java.net.URL;
26+
import java.util.Map;
27+
28+
29+
/**
30+
* attaches itself to an existing session, and forces all requests to be executed in native mode.
31+
* This is useful on the server side to bypass the current working mode. If a user is currently
32+
* using the web mode, but using native clicks, using this driver to click will force the click to
33+
* be native without impacting the working mode, making it safely usable in threads.
34+
*/
35+
public class ServerSideNativeDriver extends AttachRemoteUIADriver {
36+
37+
public ServerSideNativeDriver(URL url, SessionId session) {
38+
super(url, session);
39+
}
40+
41+
/**
42+
* forces the requests to be executed native by adding the native flag in the param list.
43+
*/
44+
@Override
45+
public WebDriverLikeRequest buildRequest(WebDriverLikeCommand command, RemoteUIAElement element,
46+
Map<String, ?> params) {
47+
String method = command.method();
48+
Path p = new Path(command).withSession(getSessionId());
49+
if (element != null) {
50+
p.withReference(element.getReference());
51+
}
52+
53+
ImmutableMap.Builder<String, Object> builder = new ImmutableMap.Builder<String, Object>();
54+
builder.put("native", true);
55+
if (params != null) {
56+
builder.putAll(params).build();
57+
}
58+
WebDriverLikeRequest request = new WebDriverLikeRequest(method, p, builder.build());
59+
return request;
60+
}
61+
}

server/src/main/java/org/uiautomation/ios/server/CommandMapping.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -300,7 +300,7 @@ private boolean isNative(IOSDriver driver, WebDriverLikeRequest request) {
300300
return request.getPayload().optBoolean("native");
301301
}
302302

303-
// else, get it from the current mode.Ò
303+
// else, get it from the current mode.
304304
boolean isNative = true;
305305
WebDriverLikeCommand command = request.getGenericCommand();
306306

server/src/main/java/org/uiautomation/ios/server/ServerSideSession.java

+14-8
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
import org.uiautomation.ios.UIAModels.configuration.WorkingMode;
2828
import org.uiautomation.ios.client.uiamodels.impl.AttachRemoteUIADriver;
2929
import org.uiautomation.ios.client.uiamodels.impl.RemoteUIADriver;
30+
import org.uiautomation.ios.client.uiamodels.impl.ServerSideNativeDriver;
3031
import org.uiautomation.ios.communication.WebDriverLikeCommand;
3132
import org.uiautomation.ios.communication.device.Device;
3233
import org.uiautomation.ios.mobileSafari.WebInspector;
@@ -50,10 +51,11 @@ public class ServerSideSession extends Session {
5051

5152
private final DriverConfiguration configuration;
5253

53-
54-
public IOSCapabilities getCapabilities(){
54+
55+
public IOSCapabilities getCapabilities() {
5556
return capabilities;
5657
}
58+
5759
ServerSideSession(IOSDriver driver, IOSCapabilities capabilities) {
5860
super(UUID.randomUUID().toString());
5961
this.driver = driver;
@@ -72,7 +74,7 @@ public IOSCapabilities getCapabilities(){
7274

7375
if (!driver.getHostInfo().getInstalledSDKs().contains(version)) {
7476
throw new SessionNotCreatedException("Cannot start on version " + version + ".Installed : "
75-
+ driver.getHostInfo().getInstalledSDKs());
77+
+ driver.getHostInfo().getInstalledSDKs());
7678
}
7779
}
7880
instruments = new InstrumentsManager(driver.getPort());
@@ -126,18 +128,22 @@ public InstrumentsManager getInstruments() {
126128
}
127129

128130
public void start() {
129-
String appleLanguage = application.getAppleLocaleFromLanguageCode(capabilities.getLanguage()).getAppleLanguagesForPreferencePlist();
130-
instruments.startSession(capabilities.getDevice(),capabilities.getDeviceVariation(), capabilities.getSDKVersion(), capabilities.getLocale(),
131-
appleLanguage, application, getSessionId(), capabilities.isTimeHack(),
132-
capabilities.getExtraSwitches());
131+
String
132+
appleLanguage =
133+
application.getAppleLocaleFromLanguageCode(capabilities.getLanguage())
134+
.getAppleLanguagesForPreferencePlist();
135+
instruments.startSession(capabilities.getDevice(), capabilities.getDeviceVariation(),
136+
capabilities.getSDKVersion(), capabilities.getLocale(),
137+
appleLanguage, application, getSessionId(), capabilities.isTimeHack(),
138+
capabilities.getExtraSwitches());
133139

134140
URL url = null;
135141
try {
136142
url = new URL("http://localhost:" + driver.getHostInfo().getPort() + "/wd/hub");
137143
} catch (Exception e) {
138144
e.printStackTrace();
139145
}
140-
nativeDriver = new AttachRemoteUIADriver(url, new SessionId(instruments.getSessionId()));
146+
nativeDriver = new ServerSideNativeDriver(url, new SessionId(instruments.getSessionId()));
141147
}
142148

143149
public WebInspector getWebInspector() {

server/src/main/java/org/uiautomation/ios/webInspector/DOM/RemoteWebElement.java

+27-36
Original file line numberDiff line numberDiff line change
@@ -103,13 +103,13 @@ private void clickAtom() throws Exception {
103103
}
104104

105105
private void clickNative() throws Exception {
106-
WorkingMode origin = session.getMode();
107-
session.setMode(WorkingMode.Native);
106+
108107
((JavascriptExecutor) nativeDriver).executeScript(getNativeElementClickOnIt());
109-
session.setMode(origin);
110-
long start = System.currentTimeMillis();
111108

112-
/*while (true) {
109+
110+
111+
/* long start = System.currentTimeMillis();
112+
while (true) {
113113
long end = System.currentTimeMillis();
114114
System.out.println((end-start)+"ms,is loading? :" + session.getContext().getDOMContext().isLoading());
115115
} */
@@ -185,7 +185,6 @@ private String getNativeElementClickOnIt() throws Exception {
185185
// web stuff.
186186
scrollIntoViewIfNeeded();
187187
Point po = findPosition();
188-
System.out.println("click native on po :" + po.toString());
189188

190189
Dimension dim = inspector.getSize();
191190
int webPageWidth = inspector.getInnerWidth();
@@ -320,27 +319,24 @@ private UIAElement getNativeElement() throws Exception {
320319

321320
UIARect rect = null;
322321
UIARect offset = null;
323-
try {
324-
session.setMode(WorkingMode.Native);
325-
UIAElement sv = nativeDriver.findElement(new TypeCriteria(UIAWebView.class));
326-
327-
// scrollview container. Doesn't start in 0,0 // x=0,y=96,h=928w=768
328-
// TODO freynaud : should save the current value, and reset to that at
329-
// the end. Not to false.
330-
nativeDriver.configure(WebDriverLikeCommand.RECT).set("checkForStale", false);
331-
rect = sv.getRect();
332-
333-
UIAElement addressBar = nativeDriver
334-
.findElement(
335-
new AndCriteria(new TypeCriteria(UIAElement.class), new NameCriteria("Address",
336-
L10NStrategy.serverL10N),
337-
new LabelCriteria("Address", L10NStrategy.serverL10N)));
338-
offset = addressBar.getRect();
339-
nativeDriver.configure(WebDriverLikeCommand.RECT).set("checkForStale", true);
340-
// rect = sv.getRect();
341-
} finally {
342-
session.setMode(origin);
343-
}
322+
323+
session.setMode(WorkingMode.Native);
324+
UIAElement sv = nativeDriver.findElement(new TypeCriteria(UIAWebView.class));
325+
326+
// scrollview container. Doesn't start in 0,0 // x=0,y=96,h=928w=768
327+
// TODO freynaud : should save the current value, and reset to that at
328+
// the end. Not to false.
329+
nativeDriver.configure(WebDriverLikeCommand.RECT).set("checkForStale", false);
330+
rect = sv.getRect();
331+
332+
UIAElement addressBar = nativeDriver
333+
.findElement(
334+
new AndCriteria(new TypeCriteria(UIAElement.class), new NameCriteria("Address",
335+
L10NStrategy.serverL10N),
336+
new LabelCriteria("Address", L10NStrategy.serverL10N)));
337+
offset = addressBar.getRect();
338+
nativeDriver.configure(WebDriverLikeCommand.RECT).set("checkForStale", true);
339+
// rect = sv.getRect();
344340

345341
int top = po.getY();
346342
int left = po.getX();
@@ -361,12 +357,9 @@ private UIAElement getNativeElement() throws Exception {
361357
delta = 96;
362358
}
363359
int y = delta + top;
364-
try {
365-
session.setMode(WorkingMode.Native);
366-
nativeElement = nativeDriver.findElement(new LocationCriteria(x, y));
367-
} finally {
368-
session.setMode(origin);
369-
}
360+
361+
nativeElement = nativeDriver.findElement(new LocationCriteria(x, y));
362+
370363

371364
}
372365
return nativeElement;
@@ -833,11 +826,9 @@ public void setValueNative(String value) throws Exception {
833826
* session.getNativeDriver().pinchClose(300, 400, 50, 100, 1); } finally {
834827
* session.setMode(origin); }
835828
*/
836-
WorkingMode origin = session.getMode();
837-
session.setMode(WorkingMode.Native);
829+
838830
((JavascriptExecutor) nativeDriver)
839831
.executeScript(getNativeElementClickOnItAndTypeUsingKeyboardScript(value));
840-
session.setMode(origin);
841832
}
842833

843834
public void scrollIntoViewIfNeeded() throws Exception {

0 commit comments

Comments
 (0)