1+ package io .appium .java_client ;
2+
3+ import com .google .gson .JsonParser ;
4+ import org .apache .http .HttpEntity ;
5+ import org .apache .http .client .HttpClient ;
6+ import org .apache .http .client .methods .HttpPost ;
7+ import org .apache .http .entity .StringEntity ;
8+ import org .apache .http .impl .client .HttpClients ;
9+ import org .apache .http .util .EntityUtils ;
10+ import org .openqa .selenium .remote .RemoteWebElement ;
11+
12+ public class ComplexFind {
13+
14+ private AppiumDriver driver ;
15+ private static final HttpClient client = HttpClients .createDefault ();
16+ private static final JsonParser parser = new JsonParser ();
17+
18+ public ComplexFind (AppiumDriver driver ) {
19+ this .driver = driver ;
20+ }
21+
22+ /**
23+ * Create a new remote web element.
24+ */
25+ private MobileElement newElement (final String elementId ) {
26+ final MobileElement element = new MobileElement (new RemoteWebElement (), driver );
27+ element .setParent (driver );
28+ element .setId (elementId );
29+ element .setFileDetector (driver .getFileDetector ());
30+ return element ;
31+ }
32+
33+ /*
34+ This is a port of the following Ruby code from github.com/appium/ruby_lib
35+ The Selenium Java bindings make it impossible to post JSON so we're using
36+ HttpPost directly.
37+
38+ # Scroll to an element containing target text or description.
39+ # @param text [String] the text to search for in the text value and content description
40+ # @return [Element] the element scrolled to
41+ def scroll_to text
42+ args = 'scroll',
43+ # textContains(text)
44+ [ [3, text] ],
45+ # descriptionContains(text)
46+ [ [7, text] ]
47+
48+ mobile :find, args
49+ end
50+ */
51+ protected MobileElement scrollTo (String text ) {
52+ text = text .replaceAll ("\" " , "\\ \" " ); // quotes must be escaped.
53+ // ["scroll", [[3,"animation"]], [[7,"animation"]]]
54+ final String complex = "[\" scroll\" ,[[3,\" " + text + "\" ]],[[7,\" " + text + "\" ]]]" ;
55+ return execute (complex );
56+ }
57+
58+ protected MobileElement scrollToExact (String text ) {
59+ text = text .replaceAll ("\" " , "\\ \" " ); // quotes must be escaped.
60+ // ["scroll", [[1,"Animation"]], [[5,"Animation"]]]
61+ final String complex = "[\" scroll\" ,[[1,\" " + text + "\" ]],[[5,\" " + text + "\" ]]]" ;
62+ return execute (complex );
63+ }
64+
65+ protected MobileElement execute (String complex ) {
66+ MobileElement element = null ;
67+ try {
68+ final String id = driver .getSessionId ().toString ();
69+ final String executeURL = driver .getRemoteAddress () + "/session/" + id + "/appium/app/complex_find" ;
70+
71+ final HttpPost post = new HttpPost (executeURL );
72+ post .setEntity (new StringEntity (complex , "UTF8" ));
73+ post .setHeader ("Content-type" , "application/json" );
74+
75+ final HttpEntity responseEntity = client .execute (post ).getEntity ();
76+
77+ if (responseEntity != null ) {
78+ try {
79+ final String responseString = EntityUtils .toString (responseEntity );
80+ // {"status":0,"value":{"ELEMENT":"1"},"sessionId":"8e982755-980f-4036-b3d1-c0e14e890273"}
81+ final String elementId = parser .parse (responseString )
82+ .getAsJsonObject ().get ("value" ).getAsJsonObject ().get ("ELEMENT" )
83+ .getAsString ();
84+
85+ element = newElement (elementId );
86+ } catch (final Exception e ) {
87+ e .printStackTrace ();
88+ } finally {
89+ EntityUtils .consume (responseEntity );
90+ }
91+ }
92+ } catch (final Exception e ) {
93+ e .printStackTrace ();
94+ }
95+ return element ;
96+ }
97+ }
0 commit comments