Skip to content

Commit

Permalink
Example with QueryBuilder
Browse files Browse the repository at this point in the history
  • Loading branch information
krukow committed Aug 27, 2012
1 parent 5963c62 commit ef1762f
Show file tree
Hide file tree
Showing 5 changed files with 267 additions and 19 deletions.
4 changes: 4 additions & 0 deletions calabash-jvm/compile-run-example.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
#!/bin/bash
javac -cp target/calabash-jvm-0.0.1-standalone.jar:target/classes example/*.java -d target/classes && java -cp target/calabash-jvm-0.0.1-standalone.jar:target/classes calabash_jvm.Example


35 changes: 29 additions & 6 deletions calabash-jvm/example/Example.java
Original file line number Diff line number Diff line change
@@ -1,17 +1,40 @@
package calabash_jvm;

import calabash_jvm.API;
import java.util.List;
import java.util.Map;
import calabash_jvm.QueryBuilder;


import static calabash_jvm.API.*;
import static calabash_jvm.QueryBuilder.view;

class Example
{

public static void main(String[] args)
{
List l = (List) API.queryq("[:UITableView]",null);
System.err.println("-----");
for (Object m : l)
{
System.err.println(m);
}

screenshot(null);
touch(view("UITabBarButton").
marked("Fourth"), null);


QueryBuilder q = view("UIWebView");

waitForExists(q,null);

screenshot(null);

scroll(q,"down");

waitForExists(q.css("a"), null);

touch(q, null);
screenshot(null);

System.exit(0);


}
}
94 changes: 94 additions & 0 deletions calabash-jvm/example/QueryBuilder.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
package calabash_jvm;
import calabash_jvm.API;
import java.util.*;

import static calabash_jvm.API.*;

public class QueryBuilder extends AbstractList
{

private List list;

public QueryBuilder()
{
this.list = new ArrayList(10);
}

public QueryBuilder(List l)
{
this.list = new ArrayList(l);
}

public Object get(int i) { return this.list.get(i);}
public int size() {return this.list.size();}


public QueryBuilder dup()
{
return new QueryBuilder(this.list);
}

public static QueryBuilder view(String s)
{
return new QueryBuilder().type(s);
}

public QueryBuilder type(String t)
{
this.list.add(t);
return this;
}

public QueryBuilder parent()
{
this.list.add("parent");
return this;
}
public QueryBuilder descendant()
{
this.list.add("descendant");
return this;
}

public QueryBuilder child()
{
this.list.add("child");
return this;
}

public QueryBuilder with(String key, Object val)
{
this.list.add(Collections.singletonMap(key,val));
return this;
}

public QueryBuilder marked(String mark)
{
return with("marked",mark);
}

public QueryBuilder index(int i)
{
this.list.add(calabash_jvm.API.index(i));
return this;
}

public QueryBuilder css(String s)
{
this.list.add(calabash_jvm.API.css(s));
return this;
}

public QueryBuilder xpath(String xp)
{
this.list.add(calabash_jvm.API.xpath(xp));
return this;
}

public List toQuery()
{
return Collections.unmodifiableList(this.list);
}


}
5 changes: 2 additions & 3 deletions calabash-jvm/run-example.sh
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@

#!/bin/bash
lein clean
lein uberjar

javac -cp target/calabash-jvm-0.0.1-standalone.jar:target/classes example/Example.java -d target/classes

java -cp target/calabash-jvm-0.0.1-standalone.jar:target/classes Example
./compile-run-example.sh
148 changes: 138 additions & 10 deletions calabash-jvm/src/calabash_jvm.clj
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
[utils :as utils]
[env :as env]
[http :as http]
[wait :as wait]
[events :as events]])
(:use [calabash-jvm.utils :only [logging]]))

Expand Down Expand Up @@ -57,7 +58,7 @@
(defn touch-mark
"Touch a view by accessibilityIdentifier/Label (mark)"
[mark]
(query* [:UIView {:marked mark}]))
(touch* [:UIView {:marked mark}]))

(defn scroll
"Scrolls a scroll view in direction dir (:up, :down, :left, :right)
Expand All @@ -77,7 +78,9 @@
"Pinch :in or :out (in-out). May specify query."
([in-out] (events/playback (str "pinch_" (name in-out))))
([q in-out]
(core/pinch q in-out)))
(if q
(core/pinch q in-out)
(pinch in-out))))


(defn enter-char
Expand Down Expand Up @@ -105,6 +108,11 @@
(defn search "Touches return/done/search on keyboard" [] (done))


(defn screenshot
"Takes a screenshot of the current view (keyword args :prefix and :name determine output file)"
[& args]
(apply core/screenshot args))

(defn record-begin!
"Begins recording touch events"
[]
Expand Down Expand Up @@ -161,24 +169,144 @@



(defprotocol ^:private Keywordize
(kw [this] "Keyworded version of this")
(st [this] "Stringified version of this"))

(extend-protocol Keywordize
nil
(kw [this] nil)
(st [this] nil)

clojure.lang.Keyword
(kw [this] this)
(st [this] (name this))

String
(kw [this] (keyword this))
(st [this] this)

java.util.List
(kw [this] (mapv kw this))
(st [this] (mapv st this))

java.util.Map
(kw [this] (clojure.walk/keywordize-keys this))
(st [this] (clojure.walk/stringify-keys this)))


;; interop

(gen-class
:name calabash_jvm.API
:main true
:methods [^:static [query [java.util.List java.util.List] java.util.List]
^:static [queryq [String java.util.List] java.util.List]
^:static [q [String] java.util.List]])
:methods [
^:static [index [Integer] java.util.Map]
^:static [xpath [String] java.util.Map]
^:static [css [String] java.util.Map]

^:static [query [java.util.List java.util.List] java.util.List]
^:static [queryq [String String] java.util.List]
^:static [queryAll [java.util.List java.util.List] java.util.List]
^:static [queryqAll [String String] java.util.List]
^:static [q [String] java.util.List]
^:static [touch [java.util.List java.util.Map] java.util.Map]
^:static [touchq [String String] java.util.Map]
^:static [touchAt [Integer Integer] java.util.Map]
^:static [touchMark [String] java.util.Map]
^:static [scroll [java.util.List String] java.util.List]
^:static [scrollToRow [java.util.List Integer] java.util.List]
^:static [pinch [java.util.List String] java.util.Map]
^:static [enterChar [String] java.util.Map]
^:static [enterText [String] java.util.Map]
^:static [done [] java.util.Map]
^:static [recordBegin [] String]
^:static [recordEnd [String] String]
^:static [playbackEvents [String java.util.Map] java.util.Map]
^:static [interpolateEvents [String java.util.Map] java.util.Map]
^:static [setHttpLogLevel [String] void]
^:static [setCalabashLogLevel [String] void]
^:static [existsq [String] boolean]
^:static [exists [java.util.List] boolean]
^:static [waitForExistsq [String String] void]
^:static [waitForExists [java.util.List java.util.Map] void]
^:static [waitForAllExistq [String String] void]
^:static [waitForAllExist [java.util.List java.util.Map] void]
^:static [screenshot [java.util.Map] String]

])

(defn- -index
[i]
(index i))
(defn- -css
[s]
(css s))

(defn- -xpath
[s]
(xpath s))

(defn- -q [s] (eval (read-string s))) ;;todo use namespace of DSL constructors

(defn- -query [x args] (st (apply query* x (kw args))))

(defn- -queryq [qs os]
(st (apply query* (-q qs) (-q os))))

(defn- -queryqAll
[qs os]
(st (apply query-all* (-q qs) (-q os))))

(defn- -touchq
[qs os]
(st (touch* (-q qs) (-q os))))

(defn- -queryAll [x args] (st (apply query-all* x (kw args))))


(defn- -touch [q opt] (st (touch* q (kw opt))))
(defn- -touchAt [x y] (st (touch-point x y)))
(defn- -touchMark [mark] (st (touch-mark mark)))
(defn- -scroll [q dir] (st (scroll q (keyword dir))))
(defn- -scrollToRow [q n] (st (scroll-to-row q n)))
(defn- -pinch [q dir] (st (pinch q (keyword dir))))
(defn- -enterChar [s] (st (enter-char s)))
(defn- -enterText [s] (st (enter-text s)))
(def ^:private -done done)
(def ^:private -recordBegin record-begin!)
(def ^:private -recordEnd record-end!)
(defn- -playbackEvents [name opts] (playback name (kw opts)))
(defn- -interpolateEvents [name opts] (interpolate name (kw opts)))
(defn- -setHttpLogLevel [name] (set-http-log-level! (keyword name)))
(defn- -setCalabashLogLevel [name] (set-calabash-log-level! (keyword name)))


(defn- -existsq [qs] (core/exists? (-q qs)))
(defn- -exists [q] (core/exists? q))

(defn- -waitForExistsq
[qs opts]
(wait/wait_for_exists [(-q qs)] (-q opts)))

(defn- -waitForExists
[qs opts]
(wait/wait_for_exists [qs] opts))


(defn- -query [x & args] (apply query* x args))
(defn- -waitForAllExistq
[qs opts]
(wait/wait_for_exists (-q qs) (-q opts)))


(defn- -q [s] (eval (read-string s)))
(defn- -waitForAllExist
[qs opts]
(wait/wait_for_exists qs opts))

(defn- -screenshot
[opts]
(apply screenshot (flatten (seq (or (kw opts) {})))))

(defn- -queryq [x ss]
(let [ds (-q x)]
(apply query* ds ss)))

(defn -main [& args]
(when (< (count args) 1)
Expand Down

0 comments on commit ef1762f

Please sign in to comment.