• +43 660 1453541
  • contact@germaniumhq.com

Writing Your First Selenium+Germanium Java Test


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 pom.xml.

<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 (openBrowser/closeBrowser), and low level operations inside the browser, such as running custom JavaScript using js, or getting the parent element in the DOM (getParent).

  • GermaniumActions - the user actions, such as click, or typeKeys.

  • GermaniumSelectors - selectors for referring the elements, and their relationships, such as Text, Link, Button and InputText.

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();
  1. 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 openBrowser.

  2. Go to the given site.

  3. Type the text into the Input with the name q. Note, that shortcuts such as <enter> are implicitly supported in Germanium, including <shift-enter>, <c-s-backspace>, etc.

  4. Click the Link using a waited statement. 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 WebElement that 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: Link(..).rightOf(Text(..))

  • They allow DOM structural references: Link(..).containing(Element('div'))

  • You can create your own composite selectors, that are even parametrized (see the Element selector).

If you’re doing Germanium right, you shouldn’t use CSS/XPath selectors anymore.

Happy automating!