Permalink
Browse files

Cleanup attributeo definition with simple macro for exception catching

  • Loading branch information...
1 parent ef0a6f7 commit 421cdcbdc8a059970b799b8ca3311d1a413738ac @semperos committed Oct 10, 2012
Showing with 19 additions and 22 deletions.
  1. +6 −20 src/webdriver_logic/core.clj
  2. +13 −2 src/webdriver_logic/util.clj
@@ -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.
@@ -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."
@@ -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.
@@ -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.