Skip to content

Commit 6bc9117

Browse files
committed
adding more TODOs
1 parent 0be502b commit 6bc9117

12 files changed

+159
-34
lines changed

release.notes

+2-1
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,8 @@
2828
- handle alerts for web
2929
- cleanup and document setValue vs sendKeys
3030
- send info to jason to start / compile etc + feature list.
31-
31+
- implicit wait for web ( would help with pageLoad issues )
32+
- avoid switching between native and web for internal command.
3233

3334

3435

server/src/main/java/org/uiautomation/ios/mobileSafari/DefaultMessageHandler.java

-1
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,6 @@ public DefaultMessageHandler(EventListener listener, ResponseFinder... finders)
4646
for (ResponseFinder finder : finders) {
4747
this.extraFinders.add(finder);
4848
}
49-
5049
}
5150

5251
@Override

server/src/main/java/org/uiautomation/ios/mobileSafari/WebInspector.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@ public WebInspector(UIADriver nativeDriver, String bundleId, ServerSideSession s
110110
this.session = session;
111111
DOMContext context = session.getContext().getDOMContext();
112112
protocol =
113-
new DebugProtocol(context, bundleId, new AlertDetector((RemoteUIADriver) nativeDriver));
113+
new DebugProtocol(context, bundleId/*, new AlertDetector((RemoteUIADriver) nativeDriver)*/);
114114
enablePageEvent();
115115
}
116116

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

+2-1
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@
4444
import org.uiautomation.ios.server.command.web.IsSelectedHandler;
4545
import org.uiautomation.ios.server.command.web.RefreshHandler;
4646
import org.uiautomation.ios.server.command.web.SetFrameHandler;
47+
import org.uiautomation.ios.server.command.web.SetImplicitWaitTimeoutHandler;
4748
import org.uiautomation.ios.server.command.web.SetTimeoutHandler;
4849
import org.uiautomation.ios.server.command.web.SetValueHandler;
4950
import org.uiautomation.ios.server.command.web.SubmitHandler;
@@ -59,7 +60,7 @@ public enum CommandMapping {
5960
SESSIONS(GetSessionsNHandler.class),
6061
DELETE_SESSION(StopSessionNHandler.class),
6162
SET_TIMEOUT(SetTimeoutNHandler.class, SetTimeoutHandler.class),
62-
IMPLICIT_WAIT(SetImplicitWaitTimeoutNHandler.class, NotImplementedWebHandler.class),
63+
IMPLICIT_WAIT(SetImplicitWaitTimeoutNHandler.class, SetImplicitWaitTimeoutHandler.class),
6364
GET_TIMEOUT(GetTimeoutNHandler.class),
6465

6566
CONFIGURE(SetConfigurationNHandler.class),

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

+2-2
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,9 @@
1818

1919
/**
2020
* state of the current application under test
21-
*
2221
*/
2322
public class Context {
23+
2424
private WorkingMode mode = WorkingMode.Native;
2525
private final DOMContext context;
2626

@@ -37,7 +37,7 @@ public WorkingMode getWorkingMode() {
3737
}
3838

3939
public void switchToFrame(String id) {
40-
40+
4141
}
4242

4343
public DOMContext getDOMContext() {

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

+4
Original file line numberDiff line numberDiff line change
@@ -141,6 +141,10 @@ public void waitForPageToLoad(long timeout) {
141141
return;
142142
}
143143

144+
public boolean isLoading() {
145+
return !isReady();
146+
}
147+
144148
private boolean isReady() {
145149
try {
146150
String state = (String) session.getWebInspector().executeScript(

server/src/main/java/org/uiautomation/ios/server/command/BaseWebCommandHandler.java

+16-2
Original file line numberDiff line numberDiff line change
@@ -23,12 +23,26 @@ public BaseWebCommandHandler(IOSDriver driver, WebDriverLikeRequest request) {
2323
super(driver, request);
2424
}
2525

26-
26+
2727
protected <T> T getConfiguration(String key) {
2828
return getConfiguration(key, (T) null);
2929
}
3030

31-
31+
protected void waitForPageToLoad() throws InterruptedException {
32+
boolean loadHappened = false;
33+
while (getSession().getContext().getDOMContext().isLoading()) {
34+
loadHappened = true;
35+
Thread.sleep(500);
36+
}
37+
38+
// if safari, wait for things to stop moving ( for instance for the JS to execute and the URL
39+
// bar to finish its animation.
40+
if (loadHappened) {
41+
Thread.sleep(1000);
42+
}
43+
44+
}
45+
3246
protected <T> T getConfiguration(String key, T defaultValue) {
3347
T webSpecific = getConf(WorkingMode.Web + "." + key);
3448
if (webSpecific != null) {

server/src/main/java/org/uiautomation/ios/server/command/web/FindElementHandler.java

+37-12
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
import org.uiautomation.ios.mobileSafari.NodeId;
2222
import org.uiautomation.ios.server.IOSDriver;
2323
import org.uiautomation.ios.server.command.BaseWebCommandHandler;
24+
import org.uiautomation.ios.webInspector.DOM.RemoteExceptionException;
2425
import org.uiautomation.ios.webInspector.DOM.RemoteWebElement;
2526

2627
public class FindElementHandler extends BaseWebCommandHandler {
@@ -31,6 +32,41 @@ public FindElementHandler(IOSDriver driver, WebDriverLikeRequest request) {
3132

3233
@Override
3334
public Response handle() throws Exception {
35+
waitForPageToLoad();
36+
37+
int implicitWait = (Integer) getConf("implicit_wait", 0);
38+
long deadline = System.currentTimeMillis() + implicitWait;
39+
RemoteWebElement rwe = null;
40+
do {
41+
try {
42+
rwe = findElement();
43+
break;
44+
} catch (NoSuchElementException e) {
45+
//ignore.
46+
} catch (RemoteExceptionException e2) {
47+
// ignore.
48+
// if the page is reloading, the previous nodeId won't be there anymore, resulting in a
49+
// RemoteExceptionException: Could not find node with given id.Keep looking.
50+
}
51+
} while (System.currentTimeMillis() < deadline);
52+
53+
if (rwe == null) {
54+
throw new NoSuchElementException(
55+
"No element found for " + getRequest().getPayload() + " after waiting for " + implicitWait
56+
+ " ms.");
57+
} else {
58+
JSONObject res = new JSONObject();
59+
res.put("ELEMENT", "" + rwe.getNodeId().getId());
60+
Response resp = new Response();
61+
resp.setSessionId(getSession().getSessionId());
62+
resp.setStatus(0);
63+
resp.setValue(res);
64+
return resp;
65+
}
66+
}
67+
68+
69+
private RemoteWebElement findElement() throws Exception {
3470
JSONObject payload = getRequest().getPayload();
3571
String type = payload.getString("using");
3672
String value = payload.getString("value");
@@ -56,18 +92,7 @@ public Response handle() throws Exception {
5692
String cssSelector = ToCSSSelectorConvertor.convertToCSSSelector(type, value);
5793
rwe = element.findElementByCSSSelector(cssSelector);
5894
}
59-
60-
JSONObject res = new JSONObject();
61-
if (rwe == null) {
62-
throw new NoSuchElementException("No element found for " + type + "=" + value);
63-
} else {
64-
res.put("ELEMENT", "" + rwe.getNodeId().getId());
65-
Response resp = new Response();
66-
resp.setSessionId(getSession().getSessionId());
67-
resp.setStatus(0);
68-
resp.setValue(res);
69-
return resp;
70-
}
95+
return rwe;
7196
}
7297

7398
@Override

server/src/main/java/org/uiautomation/ios/server/command/web/FindElementsHandler.java

+38-13
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,13 @@
1616
import org.json.JSONArray;
1717
import org.json.JSONException;
1818
import org.json.JSONObject;
19+
import org.openqa.selenium.NoSuchElementException;
1920
import org.openqa.selenium.remote.Response;
2021
import org.uiautomation.ios.communication.WebDriverLikeRequest;
2122
import org.uiautomation.ios.mobileSafari.NodeId;
2223
import org.uiautomation.ios.server.IOSDriver;
2324
import org.uiautomation.ios.server.command.BaseWebCommandHandler;
25+
import org.uiautomation.ios.webInspector.DOM.RemoteExceptionException;
2426
import org.uiautomation.ios.webInspector.DOM.RemoteWebElement;
2527

2628
import java.util.ArrayList;
@@ -34,6 +36,41 @@ public FindElementsHandler(IOSDriver driver, WebDriverLikeRequest request) {
3436

3537
@Override
3638
public Response handle() throws Exception {
39+
waitForPageToLoad();
40+
41+
int implicitWait = (Integer) getConf("implicit_wait", 0);
42+
long deadline = System.currentTimeMillis() + implicitWait;
43+
List<RemoteWebElement> elements = null;
44+
do {
45+
try {
46+
elements = findElements();
47+
if (elements.size() != 0) {
48+
break;
49+
}
50+
} catch (NoSuchElementException e) {
51+
//ignore.
52+
} catch (RemoteExceptionException e2) {
53+
// ignore.
54+
// if the page is reloading, the previous nodeId won't be there anymore, resulting in a
55+
// RemoteExceptionException: Could not find node with given id.Keep looking.
56+
}
57+
} while (System.currentTimeMillis() < deadline);
58+
59+
JSONArray array = new JSONArray();
60+
61+
List<JSONObject> list = new ArrayList<JSONObject>();
62+
for (RemoteWebElement el : elements) {
63+
list.add(new JSONObject().put("ELEMENT", "" + el.getNodeId().getId()));
64+
}
65+
66+
Response resp = new Response();
67+
resp.setSessionId(getSession().getSessionId());
68+
resp.setStatus(0);
69+
resp.setValue(list);
70+
return resp;
71+
}
72+
73+
private List<RemoteWebElement> findElements() throws Exception {
3774
JSONObject payload = getRequest().getPayload();
3875
String type = payload.getString("using");
3976
String value = payload.getString("value");
@@ -58,19 +95,7 @@ public Response handle() throws Exception {
5895
String cssSelector = ToCSSSelectorConvertor.convertToCSSSelector(type, value);
5996
res = element.findElementsByCSSSelector(cssSelector);
6097
}
61-
62-
JSONArray array = new JSONArray();
63-
64-
List<JSONObject> list = new ArrayList<JSONObject>();
65-
for (RemoteWebElement el : res) {
66-
list.add(new JSONObject().put("ELEMENT", "" + el.getNodeId().getId()));
67-
}
68-
69-
Response resp = new Response();
70-
resp.setSessionId(getSession().getSessionId());
71-
resp.setStatus(0);
72-
resp.setValue(list);
73-
return resp;
98+
return res;
7499
}
75100

76101
@Override

server/src/main/java/org/uiautomation/ios/server/command/web/GetHandler.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ public class GetHandler extends BaseWebCommandHandler {
3333

3434
// TODO freynaud cached by session.
3535
private UIAElement addressBar;
36-
private static final boolean nativeEvents = true;
36+
private static final boolean nativeEvents = false;
3737

3838
public GetHandler(IOSDriver driver, WebDriverLikeRequest request) {
3939
super(driver, request);
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
package org.uiautomation.ios.server.command.web;
2+
3+
import org.json.JSONException;
4+
import org.json.JSONObject;
5+
import org.openqa.selenium.remote.Response;
6+
import org.uiautomation.ios.communication.WebDriverLikeCommand;
7+
import org.uiautomation.ios.communication.WebDriverLikeRequest;
8+
import org.uiautomation.ios.server.IOSDriver;
9+
import org.uiautomation.ios.server.command.BaseWebCommandHandler;
10+
11+
import java.util.ArrayList;
12+
import java.util.List;
13+
14+
15+
public class SetImplicitWaitTimeoutHandler extends BaseWebCommandHandler {
16+
17+
private static final List<WebDriverLikeCommand> impacted = new ArrayList<WebDriverLikeCommand>();
18+
19+
public SetImplicitWaitTimeoutHandler(IOSDriver driver, WebDriverLikeRequest request) {
20+
super(driver, request);
21+
impacted.add(WebDriverLikeCommand.ELEMENT);
22+
impacted.add(WebDriverLikeCommand.ELEMENT_ROOT);
23+
impacted.add(WebDriverLikeCommand.ELEMENTS);
24+
impacted.add(WebDriverLikeCommand.ELEMENTS_ROOT);
25+
}
26+
27+
28+
@Override
29+
public Response handle() throws Exception {
30+
Integer ms = getRequest().getPayload().getInt("ms");
31+
for (WebDriverLikeCommand command : impacted) {
32+
getSession().configure(command).set("implicit_wait", ms);
33+
}
34+
35+
Response res = new Response();
36+
res.setSessionId(getSession().getSessionId());
37+
res.setStatus(0);
38+
res.setValue(new JSONObject());
39+
return res;
40+
}
41+
42+
43+
@Override
44+
public JSONObject configurationDescription() throws JSONException {
45+
return noConfigDefined();
46+
}
47+
48+
}

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

+8
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,12 @@ private void clickNative() throws Exception {
107107
session.setMode(WorkingMode.Native);
108108
((JavascriptExecutor) nativeDriver).executeScript(getNativeElementClickOnIt());
109109
session.setMode(origin);
110+
long start = System.currentTimeMillis();
111+
112+
/*while (true) {
113+
long end = System.currentTimeMillis();
114+
System.out.println((end-start)+"ms,is loading? :" + session.getContext().getDOMContext().isLoading());
115+
} */
110116
}
111117

112118
public NodeId getNodeId() {
@@ -179,6 +185,8 @@ private String getNativeElementClickOnIt() throws Exception {
179185
// web stuff.
180186
scrollIntoViewIfNeeded();
181187
Point po = findPosition();
188+
System.out.println("click native on po :" + po.toString());
189+
182190
Dimension dim = inspector.getSize();
183191
int webPageWidth = inspector.getInnerWidth();
184192
if (dim.getWidth() != webPageWidth) {

0 commit comments

Comments
 (0)