Selenium is a software-testing framework for web applications. Tests can be run against most modern web browsers and can be controlled by many programming languages. Selenium is available as open-source software, released under the Apache 2.0 license.

The following example shows how to setup an automated unit test for your PrimeFaces web application using Selenium, Spring Boot, and Maven.

General Project Setup

Tools used:

  • PrimeFaces 6.1
  • Selenium 3.8
  • JoinFaces 2.4
  • Spring Boot 1.5
  • Maven 3.5

We will start from a previous Spring Boot Primefaces Tutorial in which we created a greeting dialog based on a first and last name input form.

We add spring-boot-starter-test to the existing Maven POM file. This will include the core dependencies for testing Spring Boot applications with libraries that include JUnit, Hamcrest and Mockito.

In order to create local Selenium scripts, we need to make use of language-specific client drivers. As this example is based on Java we include the selenium-java client dependency.

The key interface against which tests should be written in Java is the WebDriver which has a number of implementing classes.

For this example we will use HtmlUnitDriver which is a WebDriver compatible driver for the HtmlUnit headless browser. As such we also add the selenium-htmlunit-driver dependency.

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="" xmlns:xsi=""


  <description>JSF - Primefaces Automated Unit Testing using Selenium</description>

    <relativePath /> <!-- lookup parent from repository -->



    <!-- spring-boot -->
    <!-- joinfaces -->
    <!-- selenium -->

      <!-- spring-boot-maven-plugin -->

Creating a PrimeFaces Test Page

In order to test the PrimeFaces Hello World page we will use the PageObject pattern. Within our web app’s UI there are areas that our test will interact with (in this example input/output text fields and a button). A page object simply models these as objects within the test code.

The basic rule of thumb for a page object is that it should allow a software client to do anything and see anything that a human can. It should also provide an interface that’s easy to program to and hides the underlying widgetry in the window. In this example the page object is the HelloWorldPage class shown below.

In order to set/get the input/output field values we lookup the corresponding WebElements using the @FindBy annotation. Matching is done using the 'id' of the HTML elements which are specified in the helloworld.xhtml located under src/main/resources/META-INF/resources. The submit button is also retrieved using its corresponding ID.

Note that @FindBy is just an alternate way of finding elements. You could also use the findElement() method on the WebDriver.

The JSF framework prefixes HTML elements inside a <form> with the ID of the form. If no ID is present then one will be auto-generated.

Next we represent our submit button as a submit() method that takes a first and last name as an input parameter. These parameters are set on the corresponding input fields and the submit button is executed. The method finishes by reloading the page elements so that the greeting output field is set with the new value.

The getGreeting() method allows to retrieve the greeting shown to the user. Note that we used getAttribute("textContent") as using getText() returns an empty string.

package com.codenotfound.primefaces.view;

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;

public class HelloWorldPage extends PageObject {

  @FindBy(id = "hello-world-form:first-name")
  private WebElement firstNameInput;

  @FindBy(id = "hello-world-form:last-name")
  private WebElement lastNameInput;

  @FindBy(id = "hello-world-form:submit")
  private WebElement submitButton;

  @FindBy(id = "hello-world-form:greeting")
  private WebElement greetingOutput;

  public HelloWorldPage(WebDriver driver) {

  public void submit(String firstName, String lastName) {
    // set the input fields
    // submit the form
    // refresh the output field
    PageFactory.initElements(driver, this);

  public String getGreeting() {
    return greetingOutput.getAttribute("textContent");

Notice that the above HelloWorldPage page object extends a PageObject class. This is a small helper class that uses the PageFactory factory class provided by the WebDriver’s support library in order to help realize the PageObject pattern.

In the constructor the PageFactory is used to instantiate WebElements that we have annotated in the HelloWorldPage class.

package com.codenotfound.primefaces.view;

import org.openqa.selenium.WebDriver;

public class PageObject {
  protected WebDriver driver;

  public PageObject(WebDriver driver) {
    this.driver = driver;
    PageFactory.initElements(driver, this);

In our test case we navigate to the Hello World web page using the get() method of the WebDriver. We create a new instance of our HelloWorldPage page object and call the submit() method.

Finally, an assert is used to verify that the correct greeting is generated.

package com.codenotfound.primefaces.view;

import static org.assertj.core.api.Assertions.assertThat;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.boot.test.context.SpringBootTest.WebEnvironment;
import org.springframework.test.context.junit4.SpringRunner;

@SpringBootTest(webEnvironment = WebEnvironment.DEFINED_PORT)
public class HelloWorldTest extends WebDriverTest {

  public void testSubmit() {

    HelloWorldPage page = new HelloWorldPage(driver);
    page.submit("Jane", "Doe");

    assertThat(page.getGreeting()).isEqualTo("Hello Jane Doe!");

Based on following great example on writing functional tests using Selenium we have made above test case extend a WebDriverTest class.

This class holds all the WebDriver lifecycle management code and assures correct setup and cleanup is done after each test case.

package com.codenotfound.primefaces.view;

import java.util.concurrent.TimeUnit;

import org.junit.After;
import org.junit.AfterClass;
import org.junit.BeforeClass;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.htmlunit.HtmlUnitDriver;

public class WebDriverTest {

  protected static WebDriver driver;

  public static void setUp() {
    driver = new HtmlUnitDriver();
    driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);

  public void cleanUp() {

  public static void tearDown() {

Let’s go ahead and run the HelloWorldTest test case by opening a command prompt in the projects root folder and executing following Maven command:

mvn test

The result should be a successful build during which the test is executed.

  .   ____          _            __ _ _
 /\\ / ___'_ __ _ _(_)_ __  __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
 \\/  ___)| |_)| | | | | || (_| |  ) ) ) )
  '  |____| .__|_| |_|_| |_\__, | / / / /
 :: Spring Boot ::        (v1.5.7.RELEASE)

09:25:38.666 [main] INFO  c.c.primefaces.view.HelloWorldTest - Starting HelloWorldTest on cnf-pc with PID 2808 (started by CodeNotFound in c:\codenotfound\code\jsf-primefaces\jsf-primefaces-unit-testing-selenium)
09:25:38.668 [main] INFO  c.c.primefaces.view.HelloWorldTest - No active profile set, falling back to default profiles: default
09:25:43.773 [main] INFO  c.c.primefaces.view.HelloWorldTest - Started HelloWorldTest in 5.405 seconds (JVM running for 6.127)
Tests run: 1, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 7.454 sec - in com.codenotfound.primefaces.view.HelloWorldTest

Results :

Tests run: 1, Failures: 0, Errors: 0, Skipped: 0

[INFO] ------------------------------------------------------------------------
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 10.405 s
[INFO] Finished at: 2018-01-01T09:25:45+01:00
[INFO] Final Memory: 23M/228M
[INFO] ------------------------------------------------------------------------

github mark If you would like to run the above code sample you can get the full source code here.

Using the PageObject pattern we were able to unit test a previous PrimeFaces Hello World example using Selenium, Spring Boot, and Maven.

Drop a line below in case you enjoyed reading or just to let us know how you are testing your JSF application.

Leave a Comment