status-mobile/test/clj/status_im/appium.clj

103 lines
3.0 KiB
Clojure
Raw Normal View History

2016-05-26 12:31:11 +00:00
(ns status-im.appium
(:require [clojure.java.io :as io]
[clojure.test :refer :all])
(:import (org.openqa.selenium.remote DesiredCapabilities)
(org.openqa.selenium By)
(io.appium.java_client.android AndroidDriver)
(java.net URL)
(java.util.concurrent TimeUnit)))
(defn init []
(let [dir (io/file (str (System/getProperty "user.dir")
"/android/app/build/outputs/apk"))
app (io/file dir "app-debug.apk")
capabilities (doto (DesiredCapabilities.)
(.setCapability "deviceName" "device")
(.setCapability "platformVersion" "6.0.0")
(.setCapability "app" (.getAbsolutePath app))
(.setCapability "appPackage" "com.statusim")
(.setCapability "appActivity" ".MainActivity"))
driver (AndroidDriver. (URL. "http://127.0.0.1:4723/wd/hub") capabilities)]
(-> driver
.manage
.timeouts
2016-05-26 15:13:37 +00:00
(.implicitlyWait 25 TimeUnit/SECONDS))
2016-05-26 12:31:11 +00:00
driver))
(defn by-xpath [driver xpath]
(.findElement driver (By/xpath xpath)))
(defn elements-by-xpath [driver xpath]
(.findElements driver (By/xpath xpath)))
2016-05-26 13:46:14 +00:00
(defn by-id [driver id]
(.findElementByAccessibilityId driver (name id)))
(defn get-element [driver id]
(if (keyword? id)
(by-id driver id)
(by-xpath driver id)))
(defn click [driver id]
(.click (get-element driver id)))
2016-05-26 12:31:11 +00:00
(defn write [driver input-xpath text]
2016-05-26 13:46:14 +00:00
(.sendKeys (get-element driver input-xpath) (into-array [text])))
2016-05-26 12:31:11 +00:00
(defn get-text [driver xpath]
(.getText (by-xpath driver xpath)))
(defn xpath-by-text [text]
2016-05-26 14:00:42 +00:00
(str ".//*[@text='" text "']"))
2016-05-26 12:31:11 +00:00
2016-07-18 13:22:32 +00:00
(defn click-by-text [driver text]
(let [elements (->> (xpath-by-text text)
(elements-by-xpath driver))]
(when (pos? (.size elements))
(let [element (.get elements 0)]
(.click element)))))
2016-05-26 12:31:11 +00:00
(defn contains-text [driver text]
2016-07-18 13:22:32 +00:00
(is (pos? (->> (xpath-by-text text)
(elements-by-xpath driver)
(.size)))
(format "Text \"%s\" was not found on screen." text)))
2016-05-26 12:31:11 +00:00
(defn quit [driver]
(.quit driver))
2016-05-26 14:00:42 +00:00
2016-05-30 07:22:29 +00:00
(defmacro appium-test
"Defines test which will create new appium session and will pass that
session as first argument to each command inside it's body. After execution
of all commands session will be closed.
For instance,
(appium-test my-test
(click :button)
(write :input \"oops\"))
will be expanded to
(deftest my-test
(let [session (init)]
(click session :button)
(write session :input \"oops\")
(quit session)))"
[name & body]
2016-05-26 14:00:42 +00:00
(let [sym (gensym)]
`(deftest ~name
(let [~sym (init)]
2016-07-18 13:22:32 +00:00
(click-by-text ~sym "Continue")
2016-05-26 14:00:42 +00:00
~@(for [[f & rest] body]
`(~f ~sym ~@rest))
(quit ~sym)))))
2016-07-18 13:22:32 +00:00
(defmacro defaction
[name parameters & body]
(let [session (gensym)]
`(defn ~name [~@(concat [session] parameters)]
~@(for [[f & rest] body]
`(~f ~session ~@rest)))))