Writing Your First Selenium+Germanium JUnit Test
Let’s see how we can easily write a Java Selenium test, without a PhD in WebDriver.
We will actually just reproduce the original code from the front page, using the Java version of the Germanium API.
The full project is available at: https://github.com/germaniumhq/germanium-java-sample/.
1. Add the Maven Dependency
In order to use it, all that is needed is to define a dependency on the Germanium API. This is available as a regular Maven dependency from the Germanium Maven Repository. (Here are details on how to set it up)
This will be the definition you will need to add into your
<dependency> <groupId>com.germaniumhq</groupId> <artifactId>germanium</artifactId> <version>2.0.0</version> <scope>test</scope> </dependency>
We’re also going to add a JUnit dependency, for testing.
2. Use the API
The Germanium API is provided in three classes, as static methods:
GermaniumApi- the operations for opening/closing the browser (
js, or getting the parent element in the DOM (
GermaniumActions- the user actions, such as
GermaniumSelectors- selectors for referring the elements, and their relationships, such as
The recommended way, it’s to just include statically all the methods from these classes.
The full test is in the end:
openBrowser("chrome").get(); (1) getGermanium().get("http://www.google.com"); (2) typeKeys("germanium pypi<enter>", Input().name("q")); (3) click(waited(Link("Python Package Index"))); (4) closeBrowser();
We just open the broser. Unlike the python version, in order not to have an exploding API in the number of parameters, the Java API uses a builder for the
Go to the given site.
Type the text into the
Inputwith the name
q. Note, that shortcuts such as
<enter>are implicitly supported in Germanium, including
waitedstatement. In most cases, when testing, the pattern is usually to do an action, then wait for an element, then do another action. Using
waited, you can move the waiting for a selector to resolve into that call, and use directly the
WebElementthat will be returned. Germanium also offers
waitFor, that greatly simplify waiting for elements to appear, or asserting them not to be there.
In the last step, where we clicked the link we used a Germanium concept named selectors. Selectors are similar to CSS or XPath string selectors, but on steroids:
They allow visual positional references:
They allow DOM structural references:
You can create your own composite selectors, that are even parametrized (see the
If you’re doing Germanium right, you shouldn’t use CSS/XPath selectors anymore.