Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

split ide stuff into a package #339

Merged
merged 3 commits into from
Oct 5, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
179 changes: 2 additions & 177 deletions src/etaoin/ide.clj → src/etaoin/ide/api.clj
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
(ns etaoin.ide
(:require [cheshire.core :refer [parse-stream generate-string]]
[clojure.java.io :as io]
[clojure.spec.alpha :as s]
(ns etaoin.ide.api
(:require [cheshire.core :refer [generate-string]]
[clojure.string :as str]
[clojure.test :refer [is]]
[clojure.tools.logging :as log]
Expand Down Expand Up @@ -634,176 +632,3 @@
(when-not (= msg "OK")
(throw result))
result))



(def control-flow-commands
#{:do :times :while :forEach :if :elseIf :else :end :repeatIf})

(defn cmd? [cmd]
(fn [command]
(some-> command :command (= cmd))))


(s/def ::command-if
(s/cat :if (s/cat :this (cmd? :if)
:branch ::commands)
:else-if (s/* (s/cat :this (cmd? :elseIf)
:branch ::commands))
:else (s/? (s/cat :this (cmd? :else)
:branch ::commands))
:end (cmd? :end)))

(s/def ::command-times
(s/cat :this (cmd? :times)
:branch ::commands
:end (cmd? :end)))

(s/def ::command-while
(s/cat :this (cmd? :while)
:branch ::commands
:end (cmd? :end)))

(s/def ::command-do
(s/cat :this (cmd? :do)
:branch ::commands
:repeat-if (cmd? :repeatIf)))

(s/def ::command-for-each
(s/cat :this (cmd? :forEach)
:branch ::commands
:end (cmd? :end)))

(s/def ::cmd-with-open-window
(fn [{:keys [opensWindow]}]
(true? opensWindow)))

(s/def ::command
(fn [{:keys [command]}]
(and (some? command)
(nil? (get control-flow-commands command)))))

(s/def ::commands
(s/+ (s/alt
:if ::command-if
:times ::command-times
:while ::command-while
:do ::command-do
:for-each ::command-for-each
:cmd-with-open-window ::cmd-with-open-window
:cmd ::command)))

(declare execute-commands)

(defn execute-branch
[driver {:keys [this branch]} opt]
(when (run-command-with-log driver this opt)
(execute-commands driver branch opt)
true))

(defn execute-if
[driver {:keys [if else-if else end]} opt]
(or (execute-branch driver if opt)
(some #(execute-branch driver % opt) else-if)
(execute-commands driver (:branch else) opt))
(run-command-with-log driver end opt))

(defn execute-times
[driver {:keys [this branch end]} opt]
(let [n (run-command-with-log driver this opt)]
(doseq [commands (repeat n branch)]
(execute-commands driver commands opt))
(run-command-with-log driver end opt)))

(defn execute-do
[driver {:keys [this branch repeat-if]} opt]
(run-command-with-log driver this opt)
(loop [commands branch]
(execute-commands driver commands opt)
(when (run-command-with-log driver repeat-if opt)
(recur commands))))

(defn execute-while
[driver {:keys [this branch end]} opt]
(while (run-command-with-log driver this opt)
(execute-commands driver branch opt))
(run-command-with-log driver end opt))

(defn execute-for-each
[driver {:keys [this branch end]} {vars :vars :as opt}]
(let [[var-name arr] (run-command-with-log driver this opt)]
(doseq [val arr]
(swap! vars assoc var-name val)
(execute-commands driver branch opt))
(run-command-with-log driver end opt)))

(defn execute-cmd-with-open-window
[driver {:keys [windowHandleName windowTimeout] :as cmd} {vars :vars :as opt}]
(let [init-handles (set (get-window-handles driver))
_ (run-command-with-log driver cmd opt)
_ (wait (/ windowTimeout 1000))
final-handles (set (get-window-handles driver))
handle (first (clojure.set/difference final-handles init-handles))]
(swap! vars assoc (str->var windowHandleName) handle)))

(defn execute-commands
[driver commands opt]
(doseq [[cmd-name cmd] commands]
(case cmd-name
:if (execute-if driver cmd opt)
:times (execute-times driver cmd opt)
:do (execute-do driver cmd opt)
:while (execute-while driver cmd opt)
:for-each (execute-for-each driver cmd opt)
:cmd-with-open-window (execute-cmd-with-open-window driver cmd opt)
:cmd (run-command-with-log driver cmd opt)
(throw (ex-info "Command is not valid" {:command cmd})))))


(defn run-ide-test
[driver {:keys [commands]} & [opt]]
(let [command->kw (fn [{:keys [command] :as cmd}]
(assoc cmd :command (keyword command)))
commands (map command->kw commands)
commands-tree (s/conform ::commands commands)]
(when (s/invalid? commands-tree)
(throw (ex-info "Incomplete or invalid command in the config"
{:explain-data (s/explain-data ::commands commands)})))
(execute-commands driver commands-tree opt)))

(defn get-tests-by-suite-id
[suite-id id {:keys [suites tests]}]
(let [test-ids (-> (filter #(= suite-id (id %)) suites)
first
:tests
set)
suite-tests (filter #(test-ids (:id %)) tests)]
suite-tests))

(defn find-tests
[{:keys [test-id test-ids suite-id suite-ids test-name suite-name]}
{:keys [suites tests] :as parsed-file}]
(let [test-ids (cond-> #{}
test-id (conj (first (filter #(= test-id (:id %)) tests)))
test-name (conj (first (filter #(= test-name (:name %)) tests)))
suite-id (into (get-tests-by-suite-id suite-id :id parsed-file))
suite-name (into (get-tests-by-suite-id suite-name :name parsed-file))
test-ids (into (filter #((set test-ids) (:id %)) tests))
suite-ids (into (mapcat #(get-tests-by-suite-id % :id parsed-file) suite-ids)))
tests-found (filter test-ids tests)]
(if (empty? tests-found)
tests
tests-found)))

(defn run-ide-script
[driver path & [opt]]
(let [parsed-file (with-open [rdr (io/reader path)]
(parse-stream rdr true))
opt-search (select-keys opt [:test-name :test-id :test-ids
:suite-name :suite-id :suite-ids])
tests-found (find-tests opt-search parsed-file)
opt (merge {:base-url (:url parsed-file)
:vars (atom {})}
opt)]
(doseq [test tests-found]
(run-ide-test driver test opt))))
121 changes: 121 additions & 0 deletions src/etaoin/ide/flow.clj
Original file line number Diff line number Diff line change
@@ -0,0 +1,121 @@
(ns etaoin.ide.flow
(:require [cheshire.core :refer [parse-stream]]
[clojure.java.io :as io]
[clojure.spec.alpha :as s]
[etaoin.api :refer :all]
[etaoin.ide.api :refer [run-command-with-log str->var]]
[etaoin.ide.spec :as spec]))

(declare execute-commands)

(defn execute-branch
[driver {:keys [this branch]} opt]
(when (run-command-with-log driver this opt)
(execute-commands driver branch opt)
true))

(defn execute-if
[driver {:keys [if else-if else end]} opt]
(or (execute-branch driver if opt)
(some #(execute-branch driver % opt) else-if)
(execute-commands driver (:branch else) opt))
(run-command-with-log driver end opt))

(defn execute-times
[driver {:keys [this branch end]} opt]
(let [n (run-command-with-log driver this opt)]
(doseq [commands (repeat n branch)]
(execute-commands driver commands opt))
(run-command-with-log driver end opt)))

(defn execute-do
[driver {:keys [this branch repeat-if]} opt]
(run-command-with-log driver this opt)
(loop [commands branch]
(execute-commands driver commands opt)
(when (run-command-with-log driver repeat-if opt)
(recur commands))))

(defn execute-while
[driver {:keys [this branch end]} opt]
(while (run-command-with-log driver this opt)
(execute-commands driver branch opt))
(run-command-with-log driver end opt))

(defn execute-for-each
[driver {:keys [this branch end]} {vars :vars :as opt}]
(let [[var-name arr] (run-command-with-log driver this opt)]
(doseq [val arr]
(swap! vars assoc var-name val)
(execute-commands driver branch opt))
(run-command-with-log driver end opt)))

(defn execute-cmd-with-open-window
[driver {:keys [windowHandleName windowTimeout] :as cmd} {vars :vars :as opt}]
(let [init-handles (set (get-window-handles driver))
_ (run-command-with-log driver cmd opt)
_ (wait (/ windowTimeout 1000))
final-handles (set (get-window-handles driver))
handle (first (clojure.set/difference final-handles init-handles))]
(swap! vars assoc (str->var windowHandleName) handle)))

(defn execute-commands
[driver commands opt]
(doseq [[cmd-name cmd] commands]
(case cmd-name
:if (execute-if driver cmd opt)
:times (execute-times driver cmd opt)
:do (execute-do driver cmd opt)
:while (execute-while driver cmd opt)
:for-each (execute-for-each driver cmd opt)
:cmd-with-open-window (execute-cmd-with-open-window driver cmd opt)
:cmd (run-command-with-log driver cmd opt)
(throw (ex-info "Command is not valid" {:command cmd})))))

(defn run-ide-test
[driver {:keys [commands]} & [opt]]
(let [command->kw (fn [{:keys [command] :as cmd}]
(assoc cmd :command (keyword command)))
commands (map command->kw commands)
commands-tree (s/conform ::spec/commands commands)]
(when (s/invalid? commands-tree)
(throw (ex-info "Incomplete or invalid command in the config"
{:explain-data (s/explain-data ::spec/commands commands)})))
(execute-commands driver commands-tree opt)))

(defn get-tests-by-suite-id
[suite-id id {:keys [suites tests]}]
(let [test-ids (-> (filter #(= suite-id (id %)) suites)
first
:tests
set)
suite-tests (filter #(test-ids (:id %)) tests)]
suite-tests))

(defn find-tests
[{:keys [test-id test-ids suite-id suite-ids test-name suite-name]}
{:keys [tests] :as parsed-file}]
(let [test-ids (cond-> #{}
test-id (conj (first (filter #(= test-id (:id %)) tests)))
test-name (conj (first (filter #(= test-name (:name %)) tests)))
suite-id (into (get-tests-by-suite-id suite-id :id parsed-file))
suite-name (into (get-tests-by-suite-id suite-name :name parsed-file))
test-ids (into (filter #((set test-ids) (:id %)) tests))
suite-ids (into (mapcat #(get-tests-by-suite-id % :id parsed-file) suite-ids)))
tests-found (filter test-ids tests)]
(if (empty? tests-found)
tests
tests-found)))

(defn run-ide-script
[driver path & [opt]]
(let [parsed-file (with-open [rdr (io/reader path)]
(parse-stream rdr true))
opt-search (select-keys opt [:test-name :test-id :test-ids
:suite-name :suite-id :suite-ids])
tests-found (find-tests opt-search parsed-file)
opt (merge {:base-url (:url parsed-file)
:vars (atom {})}
opt)]
(doseq [test tests-found]
(run-ide-test driver test opt))))
8 changes: 8 additions & 0 deletions src/etaoin/ide/main.clj
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
(ns etaoin.ide.main
(:require [etaoin.api :refer :all]
[etaoin.ide.flow :refer [run-ide-script]])
(:gen-class))


(defn -main
[& args])
58 changes: 58 additions & 0 deletions src/etaoin/ide/spec.clj
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
(ns etaoin.ide.spec
(:require [clojure.spec.alpha :as s]))

(def control-flow-commands
#{:do :times :while :forEach :if :elseIf :else :end :repeatIf})

(defn cmd? [cmd]
(fn [command]
(some-> command :command (= cmd))))


(s/def ::command-if
(s/cat :if (s/cat :this (cmd? :if)
:branch ::commands)
:else-if (s/* (s/cat :this (cmd? :elseIf)
:branch ::commands))
:else (s/? (s/cat :this (cmd? :else)
:branch ::commands))
:end (cmd? :end)))

(s/def ::command-times
(s/cat :this (cmd? :times)
:branch ::commands
:end (cmd? :end)))

(s/def ::command-while
(s/cat :this (cmd? :while)
:branch ::commands
:end (cmd? :end)))

(s/def ::command-do
(s/cat :this (cmd? :do)
:branch ::commands
:repeat-if (cmd? :repeatIf)))

(s/def ::command-for-each
(s/cat :this (cmd? :forEach)
:branch ::commands
:end (cmd? :end)))

(s/def ::cmd-with-open-window
(fn [{:keys [opensWindow]}]
(true? opensWindow)))

(s/def ::command
(fn [{:keys [command]}]
(and (some? command)
(nil? (get control-flow-commands command)))))

(s/def ::commands
(s/+ (s/alt
:if ::command-if
:times ::command-times
:while ::command-while
:do ::command-do
:for-each ::command-for-each
:cmd-with-open-window ::cmd-with-open-window
:cmd ::command)))
2 changes: 1 addition & 1 deletion test/etaoin/ide_test.clj
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
(ns etaoin.ide-test
(:require [etaoin.api :as api]
[etaoin.ide :as ide]
[etaoin.ide.flow :as ide]
[clojure.test :refer :all]
[clojure.java.io :as io]))

Expand Down
Loading