Skip to content

Commit ce511da

Browse files
committed
updated page load detection mechanism that doesn't forget selected frames.
1 parent d24f077 commit ce511da

File tree

7 files changed

+88
-34
lines changed

7 files changed

+88
-34
lines changed

release.notes

+5-3
Original file line numberDiff line numberDiff line change
@@ -28,9 +28,9 @@
2828
- handle alerts for web
2929
- cleanup and document setValue vs sendKeys
3030
- send info to jason to start / compile etc + feature list.
31-
- implicit wait for web ( would help with pageLoad issues )
32-
- avoid switching between native and web for internal command.
33-
31+
+ implicit wait for web ( would help with pageLoad issues )
32+
+ avoid switching between native and web for internal command.
33+
- make an executable
3434

3535

3636
TODOs:
@@ -51,9 +51,11 @@ TODOs:
5151
- add selenium tests for the inspector ( update mocks ? )
5252
- confusion between locale and languages
5353
- real device for web.
54+
- setup and teardown for device.
5455
- encapsulate result of logEleemntTree.
5556
- inspector should save the tmp screenshot in tmp dir
5657
- nativeDriver.executeScript should return UIAElement cast to what they are rather than WebElements.
5758
- use implicit wait to wait for the native keyboard
5859
- remove all calls to plutil
5960
- instruments crash from user define script. stackoverflow for method def for instance.
61+
- investigate why safari is slow to start

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

+56-10
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,6 @@
1313
*/
1414
package org.uiautomation.ios.server;
1515

16-
import java.util.List;
17-
import java.util.logging.Logger;
18-
1916
import org.json.JSONArray;
2017
import org.json.JSONObject;
2118
import org.openqa.selenium.TimeoutException;
@@ -27,6 +24,11 @@
2724
import org.uiautomation.ios.mobileSafari.events.inserted.ChildIframeInserted;
2825
import org.uiautomation.ios.webInspector.DOM.RemoteWebElement;
2926

27+
import java.util.List;
28+
import java.util.logging.Logger;
29+
30+
31+
// TODO freynaud revisit pageLoad, reset, setFrame and newContext and expose only 1 thing.
3032
public class DOMContext implements EventListener {
3133

3234
private static final Logger log = Logger.getLogger(DOMContext.class.getName());
@@ -70,13 +72,42 @@ public RemoteWebElement getWindow() {
7072
return window;
7173
}
7274

73-
public void reset() {
75+
public void newContext() {
7476
window = null;
7577
document = null;
7678
iframe = null;
7779
mainDocument = null;
7880
}
7981

82+
public String toString() {
83+
StringBuilder b = new StringBuilder();
84+
b.append("window " + window);
85+
b.append("document " + document);
86+
b.append("iframe " + iframe);
87+
b.append("mainDocument " + mainDocument);
88+
return b.toString();
89+
}
90+
91+
// TODO freynaud reset() != pageLoad
92+
public void reset() {
93+
RemoteWebElement newDocument = null;
94+
RemoteWebElement newWindow = null;
95+
96+
// check is what changed was the context for the current frame.
97+
if (iframe != null) {
98+
try {
99+
newDocument = iframe.getContentDocument();
100+
newWindow = iframe.getContentWindow();
101+
setCurrentFrame(iframe, newDocument, newWindow);
102+
return;
103+
} catch (Exception e) {
104+
e.printStackTrace();
105+
}
106+
}
107+
// couldn't update the current frame. Reseting everything.
108+
newContext();
109+
}
110+
80111
// TODO freynaud. Cleanup. A reference to the main document of the page needs
81112
// to be kept.
82113
// calling getDocument again to have the document after siwtching to an iframe
@@ -117,6 +148,7 @@ public RemoteWebElement getCurrentFrame() {
117148

118149
@Override
119150
public void onPageLoad() {
151+
System.err.println("new page loaded");
120152
pageLoaded = true;
121153
reset();
122154
}
@@ -145,17 +177,31 @@ public boolean isLoading() {
145177
return !isReady();
146178
}
147179

148-
private boolean isReady() {
180+
public String getDocumentReadyState() {
181+
String state = null;
149182
try {
150-
String state = (String) session.getWebInspector().executeScript(
183+
state = (String) session.getWebInspector().executeScript(
151184
"var state = document.readyState; return state",
152185
new JSONArray());
153-
boolean ready = "complete".equals(state);
154-
return ready; ///*"interactive".equals(state) || */"complete".equals(state);
155186
} catch (Exception e) {
156-
// TODO detect frame gone and call getSession().getContext().getDOMContext().reset();
157-
return false;
187+
// Arguments should belong to the same JavaScript world as the target object.
188+
System.err.println("error, resetting because " + e.getMessage());
189+
reset();
190+
return "unknown";
158191
}
192+
return state; ///*"interactive".equals(state)
193+
/*String state = null;
194+
try {
195+
state = (String)session.getWebInspector().evaluate("document.readyState;");
196+
} catch (Exception e) {
197+
e.printStackTrace(); //To change body of catch statement use File | Settings | File Templates.
198+
}
199+
System.out.println("state:"+state);
200+
return state; */
201+
}
202+
203+
private boolean isReady() {
204+
return "complete".equals(getDocumentReadyState());
159205
}
160206

161207
private void waitForDocumentReady(long deadLine) {

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

+17-1
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,11 @@
1313
*/
1414
package org.uiautomation.ios.server.command;
1515

16+
import org.openqa.selenium.TimeoutException;
1617
import org.uiautomation.ios.UIAModels.configuration.WorkingMode;
18+
import org.uiautomation.ios.communication.WebDriverLikeCommand;
1719
import org.uiautomation.ios.communication.WebDriverLikeRequest;
20+
import org.uiautomation.ios.mobileSafari.WebInspector;
1821
import org.uiautomation.ios.server.IOSDriver;
1922

2023
public abstract class BaseWebCommandHandler extends BaseCommandHandler {
@@ -30,7 +33,20 @@ protected <T> T getConfiguration(String key) {
3033

3134
protected void waitForPageToLoad() throws InterruptedException {
3235
boolean loadHappened = false;
33-
while (getSession().getContext().getDOMContext().isLoading()) {
36+
long
37+
timeout =
38+
(Long) getSession().configure(WebDriverLikeCommand.URL)
39+
.opt("page load", WebInspector.defaultPageLoadTimeoutInMs);
40+
41+
long deadline = System.currentTimeMillis() + timeout;
42+
while (getSession().getContext().getDOMContext()
43+
.isLoading()) {
44+
45+
if (System.currentTimeMillis() > deadline) {
46+
throw new TimeoutException(
47+
"failed to load the page after " + timeout + " ms. Page is currently is state : "
48+
+ getSession().getContext().getDOMContext().getDocumentReadyState());
49+
}
3450
loadHappened = true;
3551
Thread.sleep(500);
3652
}

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ public Response handle() throws Exception {
3737
} else {
3838
backWeb();
3939
}
40-
getSession().getContext().getDOMContext().reset();
40+
getSession().getContext().getDOMContext().newContext();
4141
getSession().getWebInspector().waitForPageToLoad();
4242
Response resp = new Response();
4343
resp.setSessionId(getSession().getSessionId());

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

+3-3
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ public ForwardHandler(IOSDriver driver, WebDriverLikeRequest request) {
3232
@Override
3333
public Response handle() throws Exception {
3434
boolean useNativeEvents = getConfiguration("nativeEvents", nativeEvents);
35-
getSession().getContext().getDOMContext().reset();
35+
getSession().getContext().getDOMContext().newContext();
3636
if (useNativeEvents) {
3737
// forwardNative();
3838
} else {
@@ -54,8 +54,8 @@ public JSONObject configurationDescription() throws JSONException {
5454
desc.put(
5555
"nativeEvents",
5656
"{boolean}, default to "
57-
+ nativeEvents
58-
+ ".true = UIAutomation native events will be used to enter click the forward arrow (slow) , Web = WebKit remote debugging will be used.Faster.");
57+
+ nativeEvents
58+
+ ".true = UIAutomation native events will be used to enter click the forward arrow (slow) , Web = WebKit remote debugging will be used.Faster.");
5959
return desc;
6060
}
6161

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

+5-15
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@
1818
import org.openqa.selenium.WebDriverException;
1919
import org.openqa.selenium.remote.Response;
2020
import org.uiautomation.ios.UIAModels.UIAElement;
21-
import org.uiautomation.ios.UIAModels.configuration.WorkingMode;
2221
import org.uiautomation.ios.UIAModels.predicate.AndCriteria;
2322
import org.uiautomation.ios.UIAModels.predicate.Criteria;
2423
import org.uiautomation.ios.UIAModels.predicate.NameCriteria;
@@ -72,7 +71,7 @@ public Response handle() throws Exception {
7271
}
7372

7473
if (newPageWillBeLoaded) {
75-
getSession().getContext().getDOMContext().reset();
74+
getSession().getContext().getDOMContext().newContext();
7675
}
7776

7877
boolean useNativeEvents = getConfiguration("nativeEvents", nativeEvents);
@@ -101,19 +100,10 @@ private UIAElement getAddressBar() {
101100
}
102101

103102
private void typeURLNative(String url) {
104-
105-
WorkingMode base = getSession().getMode();
106-
try {
107-
108-
getSession().setMode(WorkingMode.Native);
109-
getAddressBar().tap();
110-
RemoteUIAKeyboard keyboard = (RemoteUIAKeyboard) getSession().getNativeDriver().getKeyboard();
111-
keyboard.sendKeys(url);
112-
keyboard.findElement(new NameCriteria("Go")).tap();
113-
114-
} finally {
115-
getSession().setMode(base);
116-
}
103+
getAddressBar().tap();
104+
RemoteUIAKeyboard keyboard = (RemoteUIAKeyboard) getSession().getNativeDriver().getKeyboard();
105+
keyboard.sendKeys(url);
106+
keyboard.findElement(new NameCriteria("Go")).tap();
117107
}
118108

119109
private void fakeTypeURL(String url) {

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ public RefreshHandler(IOSDriver driver, WebDriverLikeRequest request) {
2828

2929
@Override
3030
public Response handle() throws Exception {
31-
getSession().getContext().getDOMContext().reset();
31+
getSession().getContext().getDOMContext().newContext();
3232
getSession().getWebInspector().refresh();
3333
getSession().getWebInspector().waitForPageToLoad();
3434
Response res = new Response();

0 commit comments

Comments
 (0)