ВУЗ: Не указан

Категория: Не указан

Дисциплина: Не указана

Добавлен: 01.01.2026

Просмотров: 2653

Скачиваний: 0

ВНИМАНИЕ! Если данный файл нарушает Ваши авторские права, то обязательно сообщите нам.

Vaadin TestBench

</batchtest>

</junit>

</target>

</project>

You also need to deploy the application to test, and possibly launch a dedicated server for it.

23.5.5. Executing Tests with Maven

Executing JUnit tests with Vaadin TestBench under Maven requires installing the TestBench library in the local Maven repository and defining it as a dependency in any POM that needs to execute TestBench tests.

A complete example of a Maven test setup is given in the example/maven folder in the installation package. Please see the README file in the folder for further instructions.

Installing TestBench in Local Repository

You can install TestBench in the local Maven repository with the following commands:

$ cd maven

$ mvn install:install-file \ -Dfile=vaadin-testbench-3.1.0-SNAPSHOT.jar \ -Djavadoc=vaadin-testbench-3.1.0-SNAPSHOT-javadoc.jar \ -DpomFile=pom.xml

The maven folder also includes an INSTALL file, which contains instructions for installing TestBench in Maven.

Defining TestBench as a Dependency

Once TestBench is installed in the local repository as instructed in the previous section, you can define it as a dependency in the Maven POM of your project as follows:

<dependency>

<groupId>com.vaadin</groupId> <artifactId>vaadin-testbench</artifactId> <version>&version.testbench;-SNAPSHOT</version>

</dependency>

For instructions on how to create a new Vaadin project with Maven, please see Section 2.6, “Using Vaadin with Maven”.

Running the Tests

To compile and run the tests, simply execute the test lifecycle phase with Maven as follows:

$ mvn test

...

-----------------------------------------------------

T E S T S

-----------------------------------------------------

Running TestBenchExample

Tests run: 6, Failures: 2, Errors: 0, Skipped: 1, Time elapsed: 36.736 sec <<< FAILURE!

Results :

Failed tests: testDemo(TestBenchExample):

expected:<[5/17/]12> but was:<[17.6.20]12> testScreenshot(TestBenchExample): Screenshots differ

Executing Tests with Maven

521



Vaadin TestBench

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

...

The example configuration starts Jetty to run the application that is tested. Error screenshots from screenshot comparison are written to the target/testbench/errors folder. To enable comparing them to "expected" screenshots, you need to copy the screenshots to the src/test/resources/screenshots/reference/ folder. See Section 23.6, “Taking and Comparing Screenshots” for more information regarding screenshots.

23.5.6. Test Setup

Test configuration is done in a method annotated with @Before. The method is executed before each test case. In a JUnit stub exported from Recorder, this is done in the setUp() method.

The basic configuration tasks are:

Set TestBench parameters

Create the web driver

Do any other initialization

TestBench Parameters

TestBench parameters are defined with static methods in the com.vaadin.testbench.Parameters class. The parameters are mainly for screenshots and documented in Section 23.6, “Taking and Comparing Screenshots”.

23.5.7. Creating and Closing a Web Driver

Vaadin TestBench uses Selenium WebDriver to execute tests in a browser. The WebDriver instance is created with the static createDriver() method in the TestBench class. It takes the driver as the parameter and returns it after registering it. The test cases must extend the TestBenchTestCase class, which manages the TestBench-specific features.

The basic way is to create the driver in a method annotated with the JUnit @Before annotation and close it in a method annotated with @After.

public class AdvancedTest extends TestBenchTestCase { private WebDriver driver;

@Before

public void setUp() throws Exception {

...

driver = TestBench.createDriver(new FirefoxDriver());

}

...

@After

public void tearDown() throws Exception { driver.quit();

}

}

This creates the driver for each test you have in the test class, causing a new browser instance to be opened and closed. If you want to keep the browser open between the test, you can use @BeforeClass and @AfterClass methods to create and quit the driver. In that case, the methods as well as the driver instance have to be static.

522

Test Setup


Vaadin TestBench

public class AdvancedTest extends TestBenchTestCase { static private WebDriver driver;

@BeforeClass

static public void createDriver() throws Exception { driver = TestBench.createDriver(new FirefoxDriver());

}

...

@AfterClass

static public void tearDown() throws Exception { driver.quit();

}

}

Browser Drivers

Please see the API documentation of the WebDriver interface for a complete list of supported drivers, that is, classes implementing the interface.

Both the Internet Explorer and Chrome require a special driver, as was noted in Section 23.2.7, “Installing Browser Drivers”. The driver executable must be included in the operating system PATH or be given with a driver-specific system property in Java with:

System.setProperty(prop, key)).

Chrome: webdriver.chrome.driver

IE: webdriver.ie.driver

If you use the Firefox 10.x ESR version, which is recommended because of test stability, you need to the binary when creating the driver as follows:

FirefoxBinary binary =

new FirefoxBinary(new File("/path/to/firefox_ESR_10")); driver = TestBench.createDriver(

new FirefoxDriver(binary, new FirefoxProfile()));

23.5.8. Basic Test Case Structure

A typical test case does the following:

1.Open the URL

2.Navigate to desired state

a.Find a HTML element (WebElement) for navigation

b.Use click() and other commands to interact with the element

c.Repeat with different elements until desired state is reached

3.Find a HTML element (WebElement) to check

4.Get and assert the value of the HTML element

5.Get a screenshot

The WebDriver allows finding HTML elements in a page in various ways, for example, with XPath expressions. The access methods are defined statically in the By class.

These tasks are realized in the following test code:

Basic Test Case Structure

523


Vaadin TestBench

@Test

public void testCase1() throws Exception { driver.get(baseUrl + "/book-examples/tobetested");

//Get the button's element.

//(Actually the caption element inside the button.)

//Use the debug ID given with setDebugId(). WebElement button = driver.findElement(By.xpath( "//div[@id='main.button']/span/span"));

//Get the caption text

assertEquals("Push Me!", button.getText());

//And click it. It's OK to click the caption element. button.click();

//Get the Label's element.

//Use the automatically generated ID.

WebElement label = driver.findElement(By.xpath( "//div[@id='myapp-949693921']" + "/div/div[2]/div/div[2]/div/div"));

// Make the assertion assertEquals("Thanks!", label.getText());

}

You can also use URI fragments in the URL to open the application at a specific state. For information about URI fragments, see Section 11.10, “URI Fragment and History Management with

UriFragmentUtility.

You should use the JUnit assertion commands. They are static methods defined in the org.junit.Assert class, which you can import (for example) with:

import static org.junit.Assert.assertEquals;

P l e a s e s e e t h e S e l e n i u m A P I d o c u m e n t a t i o n [http://seleniumhq.org/docs/03_webdriver.html#selenium-webdriver-api-commands-and-operations] for a complete reference of the element search methods in the WebDriver and By classes and for the interaction commands in the WebElement class.

TestBench has a collection of its own commands, defined in the TestBenchCommands interface. You can get a command object that you can use by calling testBench(driver) in a test case.

23.5.9. Waiting for Vaadin

Selenium is intended for regular web applications that load a page that is immediately rendered by the browser. Vaadin, on the other hand, is an Ajax framework where page is loaded just once and rendering is done in JavaScript. This takes more time so that the rendering might not be finished when the WebDriver continues executing the test. Vaadin TestBench allows waiting until the rendering is finished.

The waiting is automatically enabled. You can disable waiting by calling disableWaitForVaadin() in the TestBenchCommands interface. You can call it in a test case as follows:

testBench(driver).disableWaitForVaadin();

When disabled, you can wait for the rendering to finish by calling waitForVaadin() explicitly.

testBench(driver).waitForVaadin();

524

Waiting for Vaadin