This module integrates Selenium into a jUnit test. Because of this integration you can use the full stack of features of Unitils when you use its runner.
GUI testing
In this chapter we'll talk about automating tests for classic web
application. When we talk about classic web application we mean html based applications, so no flash generated sites.
The tests we're talking about are not unit tests but system tests. We'll test a whole system. And because the test runs against a gui, the html pages, we'll often use the word Gui tests. You could mock out the backend and just test the frontend but that is an effort we discourage you to do.
Two schools:
There are mainly two different approaches to create automated Gui test.
- You can use a tool to record and playback tests
- You can program yourself an automated test using an automation api
The reason why there are two approaches is explained by the fact that there are mainly two types of people doing the job.
You have functional people who understand the business and are best placed to define the testcases. Often they also execute the testcases to validate that the application acts as they expect. Recording and playback tools will help ease the task of test automation for these people. There are a lot of tools on the market, commercial and opensource.
People with a technical background can choose another option: programming the automation scripts. This approach allows you to create much cleaner code. It will also force you to think of scriptcode reusage which will reduce script maintanance. In this chapter we'll defend this statement. Record and Playback:
Using a tool to record testcases will allow you to create a lot of scripts in a record period of time. In this power lays directly the danger. The more you create the more you have to maintain.
NOTE:
You can download the selenium-ide add-on for Firefox. You can
record some moves and than it will replay it. But even when you want to use the unitils-selenium module, it can be handy. When you go to options, then to clipboard format and if you select the option JUnit 4 (Webdriver) than you can copy paste all the commands into your eclipse and you will see that you already have the ids of the different components of your html page. This is one option, but there
are other posibilities like firebug.
Domain Specific Language (DSL)
You can use inheritance \& composition to avoid code duplication and to limit the code to maintain to a minimum. You can create a base page with all the items in the header, that all other pages must extend.
unitils.modules=[…othermodules…],webDriverunitils.module.webDriver.className=org.unitils.selenium.WebDriverModuleunitils.module.webDriver.runAfter=unitils.module.webDriver.enabled=trueorg.unitils.selenium.browser.name=FIREFOXorg.unitils.selenium.baseUrl=???# optional values: This is only needed when you want to download a file with Selenium.org.unitils.selenium.downloadpath=???# (f.e.: C:\\Temp)org.unitils.selenium.filetype=???
browser
There are several web drivers that you can choose from: Internet Explorer (IE),
HtmlUnit(HTMLUNIT), firefox (FIREFOX), Chrome (CHROME), remote web driver (REMOTE).
downloadpath
The downloadpath can be absolute or relative.
file types
The default value for org.unitils.selenium.filetype: application/pdf, application/vnd.fdf, application/x-msdos-program, application/x-unknown-application-octet-stream, application/vnd.ms-powerpoint, application/excel, application/vnd.ms-publisher, application/x-unknown-message-rfc822, application/vnd.ms-excel, application/msword, application/x-mspublisher, application/x-tar, application/zip, application/x-gzip,application/x-stuffit,application/vnd.ms-works, application/powerpoint, application/rtf, application/postscript, application/x-gtar, video/quicktime, video/x-msvideo, video/mpeg, audio/x-wav, audio/x-midi, audio/x-aiff, application/octet-stream
But you can override this property in the unitils.properties.
Remote web driver
In version 1.0.6 we have added the option to choose for the remote web driver from selenium.
You only have to add two properties.
The annotations: BaseUrl:
You can define the base url in the unitils.properties or you can define it with this annotation. TestWebDriver:
This annotation creates the driver. With the property "org.unitils.selenium.browser.name" in the unitils.properties file you define which browser you want to use for your tests. So if you define that you want to use firefox, than you get a FirefoxDriver. WebPage:
The field with this annotation represents a web page. this means that it contains the webelements of a webpage.It uses the webdriver (field with @TestWebDriver) to initialize the elements.
Example
If the webpage has a menu, you should make a base page with all the menu items and all the pages extends this base page. If you know the id, path ... of the webelement, you can use the FindBy annotation.
In this example there is a menu with a link to a search page and a create page.
There is a new feature in unitils-selenium 1.0.4: taking screenshots.
The ScreenshotTakingWebDriver is an extension of the WebDriverBackedSelenium and makes it possible to create screenshots and safe them in a specific folder or if the test has a Testlink ID than it will be automatically saved in Testlink.
Videos
In unitils-selenium 1.0.8 there is a new module that you can enable that creates videos from your tests. If a test goes wrong than you can see second by second what happens in your test and where it goes wrong.
unitils.modules = [… other modules … ], video
unitils.module.video.className = org.unitils.selenium.screenrecorder.ScreenRecordingModule
unitils.module.video.runAfter=
unitils.module.video.enabled=true
By default, the recordings are saved in the /target/screenRecordings/[name of the test method]/ScreenRecording [date] at [time].avi.
You can open the files with an advanced video player like VLC.
If you want to override any of these properties, you can create your own ScreenRecorderFactory. Activate your factory with this property:
Sometimes selenium cannot find the correct driver and unitils-selenium makes sure that it can find a driver.
There are default drivers given by unitils-selenium, but this should be the last option.
The best solution is to pinpoint your system variables to the correct driver locations.
Chrome driver property: webdriver.chrome.driver
Firefox driver property: webdriver.firefox.bin
IE driver property: webdriver.ie.driver
But if you don't have the rights to do this, than you can set the drivers as followed in the unitils.properties.
org.unitils.selenium.firefoxbinary= ? #location of the firefox driver
org.unitils.selenium.chromebinary= ? #location of the chrome driver
org.unitils.selenium.iebinary= ? #location of the ie driver
If you have to download other drivers than you can find them here:
This feature enables you to get the logging of the webdriver.
Selenium keeps information from the client, browser, driver, performance, server.
You can use the java.util.logging.Level list to decide which level you want to choose:
(version 1.0.8)
Downloading files with Selenium can be a little bit tricky.
For Firefox and Chrome it's quite easy... just add the download folder property in your unitils.properties and the files are downloaded in that folder. The filetype property can be set, but it is only needed if you want to download a specific type of file. (see: file types in "Selenium Module project config" paragraph)
But if you want to test your application with Internet Explorer than it can be more difficult, because there appears a window that you can't get rid off.
There are a few options, you can work with AutoIt or you can use a robot.
We created a robot for downloading files with Internet Explorer 9.
But if you use another Internet Explorer that downloads his files in another way, than you only have to set the following property and RobotDownloaderFactory.createDownloaderIE() will create the correct RobotDownloader for your type of IE.
This module integrates Selenium into a jUnit test. Because of this integration you can use the full stack of features of Unitils when you use its runner.
GUI testing
In this chapter we'll talk about automating tests for classic web
application. When we talk about classic web application we mean html based applications, so no flash generated sites.
The tests we're talking about are not unit tests but system tests. We'll test a whole system. And because the test runs against a gui, the html pages, we'll often use the word Gui tests. You could mock out the backend and just test the frontend but that is an effort we discourage you to do.
Two schools:
There are mainly two different approaches to create automated Gui test.
- You can use a tool to record and playback tests
- You can program yourself an automated test using an automation api
The reason why there are two approaches is explained by the fact that there are mainly two types of people doing the job.
You have functional people who understand the business and are best placed to define the testcases. Often they also execute the testcases to validate that the application acts as they expect. Recording and playback tools will help ease the task of test automation for these people. There are a lot of tools on the market, commercial and opensource.
People with a technical background can choose another option: programming the automation scripts. This approach allows you to create much cleaner code. It will also force you to think of scriptcode reusage which will reduce script maintanance. In this chapter we'll defend this statement.
Record and Playback:
Using a tool to record testcases will allow you to create a lot of scripts in a record period of time. In this power lays directly the danger. The more you create the more you have to maintain.
NOTE:
You can download the selenium-ide add-on for Firefox. You can
record some moves and than it will replay it. But even when you want to use the unitils-selenium module, it can be handy. When you go to options, then to clipboard format and if you select the option JUnit 4 (Webdriver) than you can copy paste all the commands into your eclipse and you will see that you already have the ids of the different components of your html page. This is one option, but there
are other posibilities like firebug.
Domain Specific Language (DSL)
You can use inheritance \& composition to avoid code duplication and to limit the code to maintain to a minimum. You can create a base page with all the items in the header, that all other pages must extend.
Selenium Module load artifact (maven)
Selenium Module project config
browser
There are several web drivers that you can choose from: Internet Explorer (IE),
HtmlUnit(HTMLUNIT), firefox (FIREFOX), Chrome (CHROME), remote web driver (REMOTE).
downloadpath
The downloadpath can be absolute or relative.
file types
The default value for org.unitils.selenium.filetype: application/pdf, application/vnd.fdf, application/x-msdos-program, application/x-unknown-application-octet-stream, application/vnd.ms-powerpoint, application/excel, application/vnd.ms-publisher, application/x-unknown-message-rfc822, application/vnd.ms-excel, application/msword, application/x-mspublisher, application/x-tar, application/zip, application/x-gzip,application/x-stuffit,application/vnd.ms-works, application/powerpoint, application/rtf, application/postscript, application/x-gtar, video/quicktime, video/x-msvideo, video/mpeg, audio/x-wav, audio/x-midi, audio/x-aiff, application/octet-stream
But you can override this property in the unitils.properties.
Remote web driver
In version 1.0.6 we have added the option to choose for the remote web driver from selenium.
You only have to add two properties.
How does a test work?
The annotations:
BaseUrl:
You can define the base url in the unitils.properties or you can define it with this annotation.
TestWebDriver:
This annotation creates the driver. With the property "org.unitils.selenium.browser.name" in the unitils.properties file you define which browser you want to use for your tests. So if you define that you want to use firefox, than you get a FirefoxDriver.
WebPage:
The field with this annotation represents a web page. this means that it contains the webelements of a webpage.It uses the webdriver (field with @TestWebDriver) to initialize the elements.
Example
If the webpage has a menu, you should make a base page with all the menu items and all the pages extends this base page. If you know the id, path ... of the webelement, you can use the FindBy annotation.
In this example there is a menu with a link to a search page and a create page.
The Searchpage of the following example extends from the BasePage and contains all the important web elements of that specific webpage.
In the example the nameField and firstName are textboxes and it will put the correct string into the text box.
The best way to create your tests, is creating a base web test class. In this class you can put your web driver and base URL.
All your other tests have to extends this base test class, just like in the following example.
Screenshots
There is a new feature in unitils-selenium 1.0.4: taking screenshots.
The ScreenshotTakingWebDriver is an extension of the WebDriverBackedSelenium and makes it possible to create screenshots and safe them in a specific folder or if the test has a Testlink ID than it will be automatically saved in Testlink.
Videos
In unitils-selenium 1.0.8 there is a new module that you can enable that creates videos from your tests. If a test goes wrong than you can see second by second what happens in your test and where it goes wrong.
By default, the recordings are saved in the /target/screenRecordings/[name of the test method]/ScreenRecording [date] at [time].avi.
You can open the files with an advanced video player like VLC.
If you want to override any of these properties, you can create your own ScreenRecorderFactory. Activate your factory with this property:
Drivers
Sometimes selenium cannot find the correct driver and unitils-selenium makes sure that it can find a driver.
There are default drivers given by unitils-selenium, but this should be the last option.
The best solution is to pinpoint your system variables to the correct driver locations.
But if you don't have the rights to do this, than you can set the drivers as followed in the unitils.properties.
If you have to download other drivers than you can find them here:
Logging
This feature enables you to get the logging of the webdriver.
Selenium keeps information from the client, browser, driver, performance, server.
You can use the java.util.logging.Level list to decide which level you want to choose:
Each type can be configured like this:
(This feature is not yet available for Internet Explorer).
Chrome Binary
Use this feature if you want to use another location then the default location of the Chrome binary.
Downloading files with Selenium
(version 1.0.8)
Downloading files with Selenium can be a little bit tricky.
For Firefox and Chrome it's quite easy... just add the download folder property in your unitils.properties and the files are downloaded in that folder. The filetype property can be set, but it is only needed if you want to download a specific type of file. (see: file types in "Selenium Module project config" paragraph)
But if you want to test your application with Internet Explorer than it can be more difficult, because there appears a window that you can't get rid off.
There are a few options, you can work with AutoIt or you can use a robot.
We created a robot for downloading files with Internet Explorer 9.
But if you use another Internet Explorer that downloads his files in another way, than you only have to set the following property and RobotDownloaderFactory.createDownloaderIE() will create the correct RobotDownloader for your type of IE.
This example will try to download a file with IE, verifies if the file actually exists and deletes the file at the end of the test.
If you know the link and you only want to test how the file looks like, than you can check it another way.
The files (downloaded with the RobotDownloader) are downloaded in the user downloads folder.
Last edit: Willemijn Wouters 2015-12-10