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

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

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

Добавлен: 01.01.2026

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

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

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

Vaadin TestBench

Check that the baseUrl is the correct URL for the application. It might not be.

Test Case Stub

The test case methods are marked with @Test annotation. They normally start by calling the get() method in the driver. This loads the URL in the browser.

Actual test commands usually call the findElement() method in the driver to get hold of an HTML element to work with. The button has the main.button ID, as we set that ID for the Button object with the setDebugId() method in the application. The HTML element is represented as a WebElement object.

@Test

public void testCase1() throws Exception { driver.get(concatUrl(baseUrl, "/myapp")); assertEquals("Push Me!", driver.findElement(By.vaadin(

"bookexamplestobetested::PID_Smain.button")).getText()); driver.findElement(By.vaadin(

"bookexamplestobetested::PID_Smain.button")).click(); assertEquals("Thanks!", driver.findElement(By.vaadin(

"bookexamplestobetested::/VVerticalLayout[0]/"+ "ChildComponentContainer[1]/VLabel[0]")).getText());

}

The get() call appends the application path to the base URL. If it is already included in the base URL, you can remove it.

After Testing

Finally after running all the test cases, the method annotated with @After is called. Calling quit() for the driver closes the browser window.

The stub includes code for collecting verification errors. If you do not collect those, as is often the case, you can remove the code.

@After

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

String verificationErrorString = verificationErrors.toString();

if (!"".equals(verificationErrorString)) { fail(verificationErrorString);

}

}

23.5.2. Finding Elements by Selectors

The Selenium WebDriver API provides a number of different selectors for finding HTML DOM elements.The available selectors are defined as static methods in the org.openqa.selenium.By class. They create and return a By instance, which you can use for the findElement() method in WebDriver.

The ID, CSS class, and Vaadin selectors are described below. For others, we refer to the Selenium WebDriver API documentation [http://seleniumhq.org/docs/03_webdriver.html].

Finding Elements by Selectors

517


Vaadin TestBench

Finding by ID

Selecting elements by their HTML element id attribute is usually the easiest way to select elements. It requires that you use debug IDs, as described in Section 23.3, “Preparing an Application for Testing”. The debug ID is used as is for the id attribute of the top element of the component. Selecting is done by the By.id() selector.

For example, in the SimpleCalculatorITCase.java example we use the debug ID as follows to click on the calculator buttons:

@Test

public void testOnePlusTwo() throws Exception { openCalculator();

//Click the buttons in the user interface getDriver().findElement(By.id("button_1")).click(); getDriver().findElement(By.id("button_+")).click(); getDriver().findElement(By.id("button_2")).click(); getDriver().findElement(By.id("button_=")).click();

//Get the result label value

assertEquals("3.0", getDriver().findElement( By.id("display")).getText());

}

The ID selectors are used extensively in the TestBench examples.

Finding by Vaadin Selector

In addition to the Selenium selectors, Vaadin TestBench provides a Vaadin selector, which allows pointing to a Vaadin component by its layout path. The JUnit test cases saved from the Recorder use Vaadin selectors by default.

You can create a Vaadin selector with the By.vaadin() method. You need to use the Vaadin By, defined in the com.vaadin.testbench package, which extends the Selenium By.

The other way is to use the findElementByVaadinSelector() method in the TestBenchCommands interface. It returns the WebElement object.

A Vaadin selector begins with an application identifier. It is the path to application without any slashes or other special characters. For example, /book-examples/tobetested would be bookexamplestobetested. After the identifier, comes two colons "::", followed by a slashdelimited component path to the component to be selected. The elements in the component path are client-side classes of the Vaadin user interfacer components. For example, the server-side VerticalLayout component has VVerticalLayout client-side counterpart. All path elements except the leaves are component containers, usually layouts.The exact contained component is identified by its index in brackets.

A reference to a debug ID is given with a PID_S suffix to the debug ID.

For example:

//Get the button's element.

//Use the debug ID given with setDebugId(). WebElement button = driver.findElement(By.vaadin(

"bookexamplestobetested::PID_Smain.button"));

//Get the caption text

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

518

Finding Elements by Selectors



Vaadin TestBench

//And click it button.click();

//Get the Label's element by full path WebElement label = driver.findElement(By.vaadin(

"bookexamplestobetested::/VVerticalLayout[0]/"+

"ChildComponentContainer[1]/VLabel[0]"));

//Make the assertion

assertEquals("Thanks!", label.getText());

Finding by CSS Class

An element with a particular CSS style class name can be selected with the By.className() method. CSS selectors are useful for elements which have no ID, nor can be found easily from the component hierarchy, but do have a particular unique CSS style. Tooltips are one example, as they are floating div elements under the root element of the application. Their v-tooltip style makes it possible to select them as follows:

// Verify that the tooltip contains the expected text String tooltipText = driver.findElement(

By.className("v-tooltip")).getText();

For a complete example, see the AdvancedCommandsITCase.java file in the examples.

23.5.3. Running JUnit Tests in Eclipse

The Eclipse IDE integrates JUnit with nice control features. To run TestBench JUnit test cases in Eclipse, you need to do the following:

1.Add the TestBench JAR to a library folder in the project, such as lib. You should not put the library in WEB-INF/lib as it is not used by the Vaadin web application. Refresh the project by selecting it and pressing F5.

2.Right-click the project in Project Explorer and select Properties, and open the Java Build Path and the Libraries tab. Click Add JARs, navigate to the library folder, select the library, and click OK.

3.Switch to the Order and Export tab in the project properties. Make sure that the TestBench JAR is above the gwt-dev.jar (it may contain an old httpclient package), by selecting it and moving it with the Up and Down buttons.

4.Click OK to exit the project properties.

5. Right-click a test source file and select Run As JUnit Test.

A JUnit view should appear, and it should open the Firefox browser, launch the application, run the test, and then close the browser window. If all goes well, you have a passed test case, which is reported in the JUnit view area in Eclipse, as illustrated in Figure 23.12, “Running JUnit Tests in Eclipse”.

Running JUnit Tests in Eclipse

519


Vaadin TestBench

Figure 23.12. Running JUnit Tests in Eclipse

If you are using some other IDE, it might support JUnit tests as well. If not, you can run the tests using Ant or Maven.

23.5.4. Executing Tests with Ant

Apache Ant has built-in support for executing JUnit tests. To enable the support, you need to have the JUnit library junit.jar and its Ant integration library ant-junit.jar in the Ant classpath, as described in the Ant documentation.

Once enabled, you can use the <junit> task in an Ant script. The following example assumes that the source files are located under a src directory under the current directory and compiles them to the classes directory. The the class path is defined with the classpath reference ID and should include the TestBench JAR and all relevant dependencies.

<project default="run-tests"> <path id="classpath">

<fileset dir="lib" includes="vaadin-testbench-standalone-*.jar" />

</path>

<!-- This target compiles the JUnit tests. --> <target name="compile-tests">

<mkdir dir="classes" />

<javac srcdir="src" destdir="classes" debug="on" encoding="utf-8">

<classpath>

<path refid="classpath" /> </classpath>

</javac>

</target>

<!-- This target calls JUnit -->

<target name="run-tests" depends="compile-tests"> <junit fork="yes">

<classpath>

<path refid="classpath" /> <pathelement path="classes" />

</classpath>

<formatter type="brief" usefile="false" />

<batchtest>

<fileset dir="src">

<include name="**/**.java" /> </fileset>

520

Executing Tests with Ant