Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
anyfailures-v2.clj
(require 'leiningen.exec)
(leiningen.exec/deps '[[enlive/enlive "1.1.4"]])
(require '[net.cgrand.enlive-html :as html])
(defn result-file-loc
"Returns the location of the result files (either provided as the first
command line argument or a default value of 'test/system/results'"
[]
(let [first-cmd-line-arg (second *command-line-args*)]
(if (or
(nil? first-cmd-line-arg)
(= ":headless" first-cmd-line-arg))
;; Default dir
"test/system/results"
;; user provided dir
first-cmd-line-arg)))
(defn read-html-files
"Given a directory, returns a sequence of java.io.File instances of each .html
file in the directory"
[dir]
;; Include only files that end with .html
(filter #(and (.isFile %) (.endsWith (.getName %) ".html"))
(file-seq (clojure.java.io/file dir))))
(defn extract-failed-test-names
"Given an Enlive html-resource of a Selenium result file, returns names of the
failed test cases"
[selenium-html-resource]
(map html/text
(html/select selenium-html-resource [:tr.status_failed :td :a])))
(defn find-failed-tests
"Given a collection of Selenium test result files, returns the names of the
failed test cases in the result files"
[files]
(reduce (fn [all-failed-tests file]
(let [failed-tests
(->> file
(slurp)
(java.io.StringReader.)
(html/html-resource)
(extract-failed-test-names))]
(if-not (empty? failed-tests)
(apply conj all-failed-tests failed-tests)
all-failed-tests))) [] files))
(defn run
"Finds and prints the names of failed test cases from the Selenium result
files in the user provided or default directory"
[]
(doseq
[failed-test-case
(->> (result-file-loc) (read-html-files) (find-failed-tests))]
(println failed-test-case)))
;; Run when executed from the command line
(run)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment