Integration testing is often painful. That’s why we’re trying to mimimize the need for integration tests by making the view logic completely unit testable. Still you also need to make sure that your data-bind attributes are correct, or that you dind’t introduce any bugs when changing the view. That’s why we’ve created selenium-webdriver. It allows you to test your application using the Selenium API.
In order to allow that, we created a Selenium Webdriver that works with DukeScripts BrwsrCtx, so you can easily run and debug your tests, without caring ( a lot ) about threading. Let’s start by writing a test for the DukeScript Demo Application.
- The selenium-webdriver isn’t officially released yet, so you need to clone it on github and build it:
Use the DukeScript Wizard in NetBeans to create a new DukeScript Project named “IntegrationTest”, and make sure you include the sample code on the last wizard page.
In Project “IntegrationTest General Client Code” open the pom.xml and add this to register a System.property. This will help us later locating the index.html :
Implementing the Test
Open the project “IntegrationTest General Client Code”
Create a “New File” -> “Unit Tests” -> “JUnit Test”.
Name it “IntegrationTest” and check “Create Class Initializer” and “Create Class Finalizer”. Deselect the other options and click “Finish”.
The file opens in the editor. Now we need to set up the application window:
The method findURL simply helps you locating the index.html. It uses the System property we registered via the Maven surefire plugin. Our init method will be executed first. It starts the webdriver, loads index.html, creates our viewmodel and binds it to the page.
- We’ll also make sure the driver is closed in the end:
- Now we can write our test. You can find elements via their id, css, or xpath expressions. To simulate user input, you can either manipulate the model directly, or simulate clicks and typing. The API is pretty self explaning:
I’ve added some Thread.sleeps, so you can actually see what happens in the UI. Now right-click your test class and choose “Test File” or “Debug Test File” from the context menu and enjoy the show :-).