Docs

Documentation versions (currently viewingVaadin 7)

You are viewing documentation for an older Vaadin version. View latest documentation

Vaadin 7 TestBench Quickstart

This section walks you through the steps needed to add a Testbench test to your Vaadin application. We use Maven to set up the project and handle the dependencies. If you do not have a Vaadin application, you can generate it using Maven archetype:

 $ mvn -B archetype:generate \
    -DarchetypeGroupId=com.vaadin \
    -DarchetypeArtifactId=vaadin-archetype-application \
    -DarchetypeVersion=7.7.x \
    -DgroupId=org.test \
    -DartifactId=vaadin-app \
    -Dversion=0.1 \
    -Dpackaging=war
Note
Mozilla Firefox changed important APIs in version 48. This means that TestBench versions 4.1.0 or older do not work with the latest versions of Firefox. It is recommended you use Firefox 47 or Firefox ESR (currently at 45.3) to execute tests on Firefox until this can be fixed.

You can run your tests using Internet Explorer or Chrome browsers. Both Internet Explorer and Chrome require a special driver and some additional setup, see "Installing Browser Drivers".

Adding the Maven Dependency

Let us add the Vaadin Testbench dependency to the project. Open the pom.xml file and find the dependencies tag. Add the following dependency declaration just before the end tag (/dependencies):

   <dependency>
      <groupId>com.vaadin</groupId>
      <artifactId>vaadin-testbench</artifactId>
      <version>4.1.0</version>
      <scope>test</scope>
   </dependency>

Installing License Key

Before running tests, you need to install a license key. You can purchase a license or obtain a free trial key from the Vaadin Testbench download page in Vaadin Directory. The easiest way to install the license key is to copy and paste it to a .vaadin.testbench.developer.license file in your home directory. Other options of installing license key are described "here".

Create a Sample Test Class

In the Maven world, all test classes and resources live in the src/test directory, so create that directory. Continue by creating a java directory under that so that you end up with an src/test/java directory structure. Create a new file called MyAppTest.java in the src/test/java directory.

To create a TestBench test, you need to perform the following steps:

  • Set the browser driver and open the tested web page.

  • Simulate user actions by using TestbenchElement classes.

  • Compare expected and actual results.

  • Close the browser window.

To give an example, we create a test that clicks the button in the UI and checks that the value of the label has changed:

import org.junit.Assert;
import org.junit.Test;

public class MyAppTest extends ParallelTest {

    private static final String URL="http://localhost";
    private static final String PORT="8080";

    @Before
    public void setup() throws Exception {
        setDriver(new FirefoxDriver());
        //Open the web page
        getDriver().get(URL+":"+PORT);
    }

    @After
    public void tearDown() throws Exception {
        //close the browser window
        getDriver().quit();
    }

    @Test
    public void test() {
        //Get a reference to the button
        ButtonElement button = $(ButtonElement.class).first();

        //Simulate button click;
        button.click();

        //Get text field value;
        String actualValue = $(LabelElement.class).first().getText();

        //Check that the value is not empty
        Assert.assertFalse(actualValue.isEmpty());
    }
}

Now you have your first Testbench test case ready.

Running Tests

Before running the test, you should start your Vaadin application under test.

mvn jetty:run

You can now run the test by issuing Maven command line:

mvn test

or run it as a Junit Test from you IDE.