Web functional/integration/acceptance tests for Java made easy
Japybara is a library intended to make functional tests as simple as possible. So, don't expect a super-flexible API.
What it does:
- It starts a jetty container (fast!) on demmand. You can even interact with the server at (by default) http://localhost:8080/
- Offers a really simple API: just extends
WebTest
and use methods likevisit
,fillIn
,click
, andassertHasContent
.
For example, to test if a request to "/hello/jmalk" returns a web page with "Welcome back, Mr. Malkovich!"
,
just do it:
public class SomeFunctionalTest extends WebTestCase {
@Test
public void shouldSayHello() throws IOException {
visit("/hello/jmalk");
assertHasContent("Welcome back, Mr. Malkovich!");
}
}
So, running this simple test case actually does the following:
- Starts Jetty (port 8080)
- Deploys your web application in the context "/". So, it is accessible at URL http://localhost:8080/
visit("/hello/jmalk")
: makes an http request (GET) to http://localhost:8080/hello/jmalkassertHasContent("Welcome back, Mr. Malkovich!")
: verifies if the returning HTML contains the substring "Welcome back, Mr. Malkovich!"
It grabs ideas from some other test frameworks (mostly Capybara) and JWebUnit. It is built on top of Jetty and WebDriver.
The main interaction methods are:
visit(String path)
: goes to Goes to the specified pageclick(String locator)
: simulates a click on the object identified by locatorfillIn(String locator, String content)
: acts like an user, typingcontent
into the object identified bylocator
back()
: returns to the previews page, like a browsers's 'back' button
The locator
parameter used in click
and fillIn
methods is passed to guessElement
to find that element. So, this is the description on guessElement
method:
WebElement guessElement(String locator)
- Finds an object using a kind of fussy approach. It works if the given
locator
is anid
orname
attribute, or if it identifies alabel
(so it returns the target field), or CSS/JQuery locator style (like#users li.enabled
), the link content (the text within<a<
tag) orhref
target. - Returns an instance of
WebElement
(from the WebDriver API) or throwsNoSuchElementException
if not found.
It is possible to program more advanced interactions using the WebDriver
(aka Selenium) API. The WebDriver
instance is available throw the getDriver()
method.
By default, a HtmlUnitWebDriver
emulating Internet Explorer 8 with javascript enabled is used. To change the default
driver, simply override the setUpWebDriver()
method (and call setDriver()
within).
assertHasContent(String)
: checks if the page has the given contentString getContent()
: return a snapshot of the HTML, as it looks right now (potentially modified by Javascript).assertCurrentPath(String)
: Checks if the current path is the given parameter
Before your first test case, there will be a Jetty container ready. You can configure some defaults using -D
jvm
parameters:
japybara.app_host
: context url (host, port, context). Default:http://localhost:8080/
japybara.webapp
: path to webapp directory. Default:src/main/webapp
Tip: use maven plus war:inplace.
In WebTestCase
, call server.getWebServer()
to access the Jetty server (org.mortbay.jetty.Server
). Or rewrite
the startServer
method to use another implementation of WebTestServer
other than JettyServer
.
Note: Actually it doesn't support JNDI lookups.
There is also a standalone server:
Usage: org.japybara.JappybaraServer [-p path] [-u url]
where
-p path Webapp path. Default: ./src/main/webapp
-u url Context url. Default: http://localhost:8080/
Smells like rails2's script/server
I've just started this project, so there are LOTS of things to do.
Oh, and change its name. Japybara is supposed to be something like a java version of capybara... but is ugly as hell.