16
16
import org .json .JSONArray ;
17
17
import org .json .JSONException ;
18
18
import org .json .JSONObject ;
19
+ import org .openqa .selenium .NoSuchElementException ;
19
20
import org .openqa .selenium .remote .Response ;
20
21
import org .uiautomation .ios .communication .WebDriverLikeRequest ;
21
22
import org .uiautomation .ios .mobileSafari .NodeId ;
22
23
import org .uiautomation .ios .server .IOSDriver ;
23
24
import org .uiautomation .ios .server .command .BaseWebCommandHandler ;
25
+ import org .uiautomation .ios .webInspector .DOM .RemoteExceptionException ;
24
26
import org .uiautomation .ios .webInspector .DOM .RemoteWebElement ;
25
27
26
28
import java .util .ArrayList ;
@@ -34,6 +36,41 @@ public FindElementsHandler(IOSDriver driver, WebDriverLikeRequest request) {
34
36
35
37
@ Override
36
38
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 {
37
74
JSONObject payload = getRequest ().getPayload ();
38
75
String type = payload .getString ("using" );
39
76
String value = payload .getString ("value" );
@@ -58,19 +95,7 @@ public Response handle() throws Exception {
58
95
String cssSelector = ToCSSSelectorConvertor .convertToCSSSelector (type , value );
59
96
res = element .findElementsByCSSSelector (cssSelector );
60
97
}
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 ;
74
99
}
75
100
76
101
@ Override
0 commit comments