From f357adb002724796a83f3e43240b1969cd1d4c78 Mon Sep 17 00:00:00 2001 From: "Alex.Shi" Date: Mon, 5 Oct 2020 17:19:20 +0300 Subject: [PATCH] split ide stuff into a package (#339) * split ide stuff into a package * fix * fix after fix --- src/etaoin/{ide.clj => ide/api.clj} | 179 +--------------------------- src/etaoin/ide/flow.clj | 121 +++++++++++++++++++ src/etaoin/ide/main.clj | 8 ++ src/etaoin/ide/spec.clj | 58 +++++++++ test/etaoin/ide_test.clj | 2 +- test/etaoin/unit_test.clj | 7 +- 6 files changed, 194 insertions(+), 181 deletions(-) rename src/etaoin/{ide.clj => ide/api.clj} (76%) create mode 100644 src/etaoin/ide/flow.clj create mode 100644 src/etaoin/ide/main.clj create mode 100644 src/etaoin/ide/spec.clj diff --git a/src/etaoin/ide.clj b/src/etaoin/ide/api.clj similarity index 76% rename from src/etaoin/ide.clj rename to src/etaoin/ide/api.clj index c4e83d56..bafe9580 100644 --- a/src/etaoin/ide.clj +++ b/src/etaoin/ide/api.clj @@ -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] @@ -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)))) diff --git a/src/etaoin/ide/flow.clj b/src/etaoin/ide/flow.clj new file mode 100644 index 00000000..c8a40fc6 --- /dev/null +++ b/src/etaoin/ide/flow.clj @@ -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)))) diff --git a/src/etaoin/ide/main.clj b/src/etaoin/ide/main.clj new file mode 100644 index 00000000..389167bc --- /dev/null +++ b/src/etaoin/ide/main.clj @@ -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]) diff --git a/src/etaoin/ide/spec.clj b/src/etaoin/ide/spec.clj new file mode 100644 index 00000000..20268b0b --- /dev/null +++ b/src/etaoin/ide/spec.clj @@ -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))) diff --git a/test/etaoin/ide_test.clj b/test/etaoin/ide_test.clj index 733ba1cd..58822b2b 100644 --- a/test/etaoin/ide_test.clj +++ b/test/etaoin/ide_test.clj @@ -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])) diff --git a/test/etaoin/unit_test.clj b/test/etaoin/unit_test.clj index 475d91a9..e82e9f51 100644 --- a/test/etaoin/unit_test.clj +++ b/test/etaoin/unit_test.clj @@ -2,7 +2,8 @@ (:require [clojure.spec.alpha :as s] [clojure.test :refer :all] [etaoin.api :refer :all] - [etaoin.ide :as ide] + [etaoin.ide.flow :as ide] + [etaoin.ide.spec :as spec] etaoin.proc) (:import java.io.File java.nio.file.attribute.FileAttribute @@ -181,6 +182,6 @@ {:command :if} {:command :do-something} {:command :end}]] - (is (= (s/conform :etaoin.ide/commands commands) + (is (= (s/conform ::spec/commands commands) valid-commands-tree)) - (is (s/invalid? (s/conform :etaoin.ide/commands invalid-commands))))) + (is (s/invalid? (s/conform ::spec/commands invalid-commands)))))