|
13 | 13 | */
|
14 | 14 | package org.uiautomation.ios.server;
|
15 | 15 |
|
16 |
| -import java.util.List; |
17 |
| -import java.util.logging.Logger; |
18 |
| - |
19 | 16 | import org.json.JSONArray;
|
20 | 17 | import org.json.JSONObject;
|
21 | 18 | import org.openqa.selenium.TimeoutException;
|
|
27 | 24 | import org.uiautomation.ios.mobileSafari.events.inserted.ChildIframeInserted;
|
28 | 25 | import org.uiautomation.ios.webInspector.DOM.RemoteWebElement;
|
29 | 26 |
|
| 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. |
30 | 32 | public class DOMContext implements EventListener {
|
31 | 33 |
|
32 | 34 | private static final Logger log = Logger.getLogger(DOMContext.class.getName());
|
@@ -70,13 +72,42 @@ public RemoteWebElement getWindow() {
|
70 | 72 | return window;
|
71 | 73 | }
|
72 | 74 |
|
73 |
| - public void reset() { |
| 75 | + public void newContext() { |
74 | 76 | window = null;
|
75 | 77 | document = null;
|
76 | 78 | iframe = null;
|
77 | 79 | mainDocument = null;
|
78 | 80 | }
|
79 | 81 |
|
| 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 | + |
80 | 111 | // TODO freynaud. Cleanup. A reference to the main document of the page needs
|
81 | 112 | // to be kept.
|
82 | 113 | // calling getDocument again to have the document after siwtching to an iframe
|
@@ -117,6 +148,7 @@ public RemoteWebElement getCurrentFrame() {
|
117 | 148 |
|
118 | 149 | @Override
|
119 | 150 | public void onPageLoad() {
|
| 151 | + System.err.println("new page loaded"); |
120 | 152 | pageLoaded = true;
|
121 | 153 | reset();
|
122 | 154 | }
|
@@ -145,17 +177,31 @@ public boolean isLoading() {
|
145 | 177 | return !isReady();
|
146 | 178 | }
|
147 | 179 |
|
148 |
| - private boolean isReady() { |
| 180 | + public String getDocumentReadyState() { |
| 181 | + String state = null; |
149 | 182 | try {
|
150 |
| - String state = (String) session.getWebInspector().executeScript( |
| 183 | + state = (String) session.getWebInspector().executeScript( |
151 | 184 | "var state = document.readyState; return state",
|
152 | 185 | new JSONArray());
|
153 |
| - boolean ready = "complete".equals(state); |
154 |
| - return ready; ///*"interactive".equals(state) || */"complete".equals(state); |
155 | 186 | } 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"; |
158 | 191 | }
|
| 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()); |
159 | 205 | }
|
160 | 206 |
|
161 | 207 | private void waitForDocumentReady(long deadLine) {
|
|
0 commit comments