Someone already did it
Scalatest is neat.
December 18, 2012It turns out that the things I was trying to do with Selophane are already implemented in several libraries. I'm not mad; my solution to the issue of PageFactory
is still somewhat cool. It is still one of the few Java based libraries to attempt a PageFactory
hack, and it's definitely on the right track.
The problem is that other frameworks have solved the same problem in surprisingly Good Ways.
I've spent the past two days evaluating ScalaTest, which adds a lot of behavior to WebElements in 2.0 RC5's as documented on their using selenium page.
I was able to model my search functionality in about 150 lines of Scala, inclusive of whitespace. Most of my time was spent getting more comfortable with Scala. Instead of multiple boiler-plate infested classes, I was able to write a few concise, understandable behaviors.
What's magical about Scala? (And ScalaTest?)
Traits are the primary source of magic (and understanding) in Scala. Traits add layers of functionality into classes. It basically enables multiple-behavior inheritance to classes, which is really the reason we extend classes in the first place.
This trait, when added to an object, adds a required property called bodyId. It also provides loaded
, a boolean:
trait BodyId extends Eventually {
this: WebBrowser =>
val bodyId: String
def loaded(implicit driver:WebDriver) = eventually {
cssSelector("body#" + bodyId).element.isDisplayed
}
}
Eventually
provides some sugar for a wait loop. At the project level, this is defined as 5s, with 150ms pause between checks.
The minimal object that uses this functionality:
object HomePage extends Page with WebBrowser with BodyId {
val url = "http://" + host + "/"
val bodyId = "homepage"
}
by setting bodyId = "homepage"
, the loaded function is provided everything it needs to check the state of a page after it is loaded.
In tests, all we have to do (using ScalaTest idioms):
// in tests:
go to HomePage
HomePage.loaded should equal(true)
go to
is sugar forHomePage.get()
Homepage.loaded should equal(true)
is the same as sayingassert(Homepage.loaded === true)
I'm going to cover other advantages of ScalaTest as I use it more.