Skip to content

Commit

Permalink
Cleanup attributeo definition with simple macro for exception catching
Browse files Browse the repository at this point in the history
  • Loading branch information
semperos committed Oct 10, 2012
1 parent ef0a6f7 commit 421cdcb
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 22 deletions.
26 changes: 6 additions & 20 deletions src/webdriver_logic/core.clj
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,11 @@
(:refer-clojure :exclude [==])
(:use clojure.core.logic
[webdriver-logic.state :only [*driver* *html-tags* *html-attributes*]]
[webdriver-logic.util :only [fresh? ground?]]
[webdriver-logic.util :only [fresh? ground? careful-attribute]]
[clojure.pprint :only [pprint]])
(:require [clojure.test :as test]
[clj-webdriver.core :as wd]
[webdriver-logic.state :as st])
(:import [org.openqa.selenium InvalidElementStateException]
[org.openqa.selenium.remote ErrorHandler$UnknownServerException]))
[webdriver-logic.state :as st]))

(defmacro s
"Deterministic test. Deterministic predicates are predicates that must succeed exactly once and, for well behaved predicates, leave no choicepoints.
Expand Down Expand Up @@ -107,35 +105,23 @@
(and (ground? gelem)
(ground? gattr)) (unify a
[elem attr value]
[gelem gattr (try
(wd/attribute gelem gattr)
(catch InvalidElementStateException _ nil)
(catch ErrorHandler$UnknownServerException _ nil))])
[gelem gattr (careful-attribute gelem gattr)])
(ground? gelem) (to-stream
(for [attribute *html-attributes*]
(unify a
[elem attr value]
[gelem attribute (try
(wd/attribute gelem attribute)
(catch InvalidElementStateException e nil)
(catch ErrorHandler$UnknownServerException _ nil))])))
[gelem attribute (careful-attribute gelem attribute)])))
(ground? gattr) (to-stream
(for [element (all-elements)]
(unify a
[elem attr value]
[element gattr (try
(wd/attribute element gattr)
(catch InvalidElementStateException e nil)
(catch ErrorHandler$UnknownServerException _ nil))])))
[element gattr (careful-attribute element gattr)])))
:default (to-stream
(for [element (all-elements)
attribute *html-attributes*]
(unify a
[elem attr value]
[element attribute (try
(wd/attribute element attribute)
(catch InvalidElementStateException e nil)
(catch ErrorHandler$UnknownServerException _ nil))])))))))
[element attribute (careful-attribute element attribute)])))))))

(defn childo
"A relation where `child-elem` is a child element of the `parent-elem` element on the current page."
Expand Down
15 changes: 13 additions & 2 deletions src/webdriver_logic/util.clj
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
(ns ^{:doc "Utilties atop third-party dependendies."}
webdriver-logic.util
(:use [clojure.core.logic :only [lvar?]]))
(:use [clojure.core.logic :only [lvar?]])
(:require [clj-webdriver.core :as wd])
(:import [org.openqa.selenium InvalidElementStateException]
[org.openqa.selenium.remote ErrorHandler$UnknownServerException]))

(defn fresh?
"Returns true, if `x' is fresh.
Expand All @@ -12,4 +15,12 @@
"Returns true, if `x' is ground.
`x' must have been `walk'ed before!"
[x]
(not (lvar? x)))
(not (lvar? x)))

(defmacro careful-attribute
"Wrap calls to wd/attribute in try and catch standard exceptions"
[elem attr]
`(try
(wd/attribute ~elem ~attr)
(catch InvalidElementStateException _# nil)
(catch ErrorHandler$UnknownServerException _# nil)))

0 comments on commit 421cdcb

Please sign in to comment.