-
Notifications
You must be signed in to change notification settings - Fork 96
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
split ide stuff into a package (#339)
* split ide stuff into a package * fix * fix after fix
- Loading branch information
Showing
6 changed files
with
194 additions
and
181 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)))) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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]) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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))) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.