You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

README-TESTS.md 10KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. # TestBench tests in Vaadin Framework
  2. ## Project setup
  3. The project currently supports running TestBench 3+ (java) tests. Each test consists of a Vaadin UI class and a TestBench script/java test.
  4. All test UI classes go into `uitest/src`. These files are automatically packaged into a war file which is deployed to a Jetty server during the build process so that tests can open and interact with the UI:s. For development purposes, the Jetty server can be started in Eclipse, see running tests in Eclipse.
  5. The project is setup so that `/run` is mapped to a specialized servlet which allows you to add the UI you want to test to the URL, e.g. http://localhost:8888/run/com.vaadin.tests.component.label.LabelModes or just http://localhost:8888/run/LabelModes if there are no multiple classes named LabelModes. Because of caching, the ?restartApplication parameter is needed after the first run if you want to run multiple test classes.
  6. ## Creating a new test
  7. Creating a new test involves two steps: Creating a test UI (typically this should be provided in the ticket) and creating a TestBench test for the UI.
  8. ## Creating a new test UI
  9. Test UIs are created inside the `uitest/src` folder in the `com.vaadin.tests` package. For tests for a given component, use the `com.vaadin.tests.components.<component>` package. For other features, use a suitable existing com.vaadin.tests.<something> package or create a new one if no suitable exists.
  10. The test should be named according to what it tests, e.g. EnsureFormTooltipWorks. Names should not refer to a ticket, e.g. Ticket1123 will automatically be rejected during code review as it is non-descriptive.
  11. There are a couple of helper UI super classes which you can use for the test. You should never extend UI directly:
  12. * `AbstractTestUI`
  13. * Automatically sets up a VerticalLayout with a description label on the top. Use addComponent() to add components to the layout
  14. * Supports ?transport=websocket/streaming/xhr parameter for setting push mode. This is for testing core push functionality, typically you should just add @Push to your test UI
  15. * `AbstractTestUIWithLog`
  16. * AbstractTestUI but adds a log at the top to which you can add rows using log(String). Handy for printing state information which the TB test can assert reliably.
  17. * `AbstractComponentTest`, `AbstractFieldTest`, ...
  18. * Base classes for generic component tests. Generates test which have a menu on the top containing options for configuring the component. Classes follow the same component hierarchy as Vaadin component classes and this way automatically gets menu items for setting features the parent class supports.
  19. * Gotcha: If you add a new feature to a menu you need to run and possibly (probably) fix all TB tests which use the class as they will click on the wrong item (fixable by implementing [http://dev.vaadin.com/ticket/11307](http://dev.vaadin.com/ticket/11307))
  20. [Note] `AbstractTestCase` and `TestBase` are old base classes for tests. Don't use them for any new tests. They extend `LegacyApplication` which is a deprecated class.
  21. ## Creating a TestBench test for a UI
  22. All new test for the projects must be created as TestBench3+ tests
  23. ### Test class naming
  24. TestBench 3+ tests usually follow a naming convention which automatically maps the test to the UI class. By the convention, the TB3 test class should be named **UIClassTest**, e.g. if UI is **LabelModes** then the TB3+ test is **LabelModesTest**. Inside the LabelModesTest class there are 1..N methods which test various aspects of the LabelModes UI. This is the preferred way, but not strictly necessary; sometimes it's more conventient to use a different name for the UI and test class. In that case, remember to override the `AbstractTB3Test::getUIClass()` method in the test.
  25. ### Super class
  26. There are a couple of super classes you can use for a TB3+ test:
  27. * `MultiBrowserTest`
  28. * Ensures the test is run on the browsers we support automatically
  29. * **This is what you typically should use**
  30. * `WebsocketTest`
  31. * Run only on browsers which supports websockets
  32. ### Creating the test
  33. The actual test is one method in the test class created for the given UI. The method declaration is something like:
  34. @Test
  35. public void testLabelModes() throws Exception {
  36. `@Test` is needed for it to be run at all.
  37. `throws Exception` should usually be added to avoid catching exceptions inside the test, as most often an exception means the test has failed. Unhandled exceptions will always fail the test; if you use checked exceptions, you'll need to handle them somehow or decide they're automatically failures on exception.
  38. The beginning of the test should request the UI using `openTestURL()`;
  39. public void testLabelModes() throws Exception {
  40. // Causes the test to be opened with the debug console open. Typically not needed
  41. // setDebug(true);
  42. // Causes the test to be opened with push enabled even though the UI does not have @Push.
  43. //Typically not needed
  44. // setPush(true);
  45. openTestURL();
  46. After this it is up to you what the test should do. The browser will automatically be started before the test method and shutdown after it. If the test fails, a failure screenshot will automatically be created in addition to an exception being thrown.
  47. When done, run the test locally, as described below, and ensure it passes. Then run it remotely, as described below, a couple of times to ensure it passes reliably.
  48. ### Best practices for creating a TB3 test
  49. #### Communicate intent through your test class
  50. JUnit allows you (as opposed to TB2 HTML tests) to describe what the test should do using method names. The test method should be a chain of calls which enables any reader to quickly understand what the test does, all details should be abstracted into methods.
  51. Over the time we should collect useful helper methods into the parent classes (AbstractTB3Test already contain some, e.g. assertGreater/assertLessThan/vaadinElementById/...).
  52. #### Do a sensible amount of things in one test method
  53. There is an overhead before and after each test method when the browser instance is started. The tests are therefore not restricted to testing one single thing. Instead a test method should test one logical group of things (TB3 tests are not pure unit tests).
  54. #### Use ids in your test class
  55. Use ids in your UI class. Define the IDs as constants in the UI class. Use the constants in your Test class. Use static imports to avoid massive prefixes for the constants.
  56. public class NativeButtonIconAndText extends AbstractTestUI implements
  57. ClickListener {
  58. static final String BUTTON_TEXT = "buttonText";
  59. [...]
  60. buttonText.setId(BUTTON_TEXT);
  61. public class NativeButtonIconAndTextTest extends MultiBrowserTest {
  62. @Test
  63. public void testNativeButtonIconAltText() {
  64. openTestURL();
  65. assertAltText(NativeButtonIconAndText.BUTTON_TEXT, "");
  66. ## Running the DevelopmentServerLauncher
  67. The Jetty included in the project can be started in Eclipse by running the “Development Server (Vaadin)” launch configuration in `/eclipse` (right click -> Debug as -> Development Server (Vaadin) ). This deploys all the tests to `localhost:8888` (runs on port 8888, don’t change that unless you want problems). Use `/run/<uiclass>` to open a test.
  68. The DevelopmentServerLauncher has a built-in feature which ensures only one instance can be running at a time. There is therefore no need to first stop the old and and then start a new one. Start the server and the old one will be killed automatically.
  69. ## Setup before running any tests in Eclipse
  70. Before running any tests in Eclipse you need to
  71. 1. copy `uitest/eclipse-run-selected-test.properties` to `work/eclipse-run-selected-test.properties`
  72. 2. edit `work/eclipse-run-selected-test.properties`
  73. 1. Define `com.vaadin.testbench.screenshot.directory` as the directory where you checked out the screenshots repository (this directory contains the “references” subdirectory)
  74. ## Running TB3+ tests
  75. TB3+ tests are standard JUnit tests which can be run using the “Run as -> JUnit test” (or Debug as) in Eclipse. Right click on a single test instance in the JUnit result view and select to debug to re-run it on a single browser.
  76. ### Debugging TB3+ tests
  77. #### Debugging remotely
  78. Running remotely on a single browser (as described above) can be used to debug issues with a given browser but with the downside that you cannot see what is happening with the browser. In theory this is possible if you figure out on what machine the test is run (no good way for this at the moment) and use VNC to connect to that.
  79. #### Debugging locally
  80. A better option is to run the test on a local browser instance. To do this you need to add a `@RunLocally` annotation to the test class. `@RunLocally` uses Firefox by default but also supports other browsers using `@RunLocally(CHROME)`, `@RunLocally(SAFARI)`, `@RunLocally(Browser.IE11)` and `@RunLocally(PHANTOMJS)`.
  81. By default using `@RunLocally` annotation in Framework tests is not allowed. In order to run a test locally, you need to uncomment the line `com.vaadin.testbench.allowRunLocally=true` in `work/eclipse-run-selected-test.properties`.
  82. Besides, some local configurations are needed for certain browsers, especially if you did not install them in the “standard” locations.
  83. **PhantomJS**: PhantomJS is a headless browser, good especially for fast validation. Download it from the [PhantomJS site](http://phantomjs.org/download.html) and add the binary to your PATH.
  84. **Firefox**: If you have Firefox in your PATH, this is everything you need. If Firefox cannot be started, add a **firefox.path** property to `/work/eclipse-run-selected-test.properties`, pointing to your Firefox binary (e.g.`firefox.path=/Applications/Firefox 17 ESR.app/Contents/MacOS/firefox`).
  85. **Chrome**: You need to [download ChromeDriver](http://chromedriver.storage.googleapis.com/index.html) and add a chrome.driver.path property to `/work/eclipse-run-selected-test.properties`, pointing to the ChromeDriver binary (NOT the Chrome binary).
  86. **IE11**: You need to [download IEDriverServer according to the selenium version](http://selenium-release.storage.googleapis.com/index.html) and add a webdriver.ie.driver property to `/work/eclipse-run-selected-test.properties`, point to the IEDriveServer binary.
  87. **Safari**: At least on Mac, no configuration should be needed.
  88. Remember to remove `@RunLocally()` before committing the test or it will fail during the build.