![Page 1: Introduction to Functional Testing with SWTBot and … · Introduction to Functional Testing with SWTBot and ... Do I really need a running Eclipse to test this and that? ... Code](https://reader030.vdocument.in/reader030/viewer/2022013107/5b312ca47f8b9a744a8b9c12/html5/thumbnails/1.jpg)
Introduction to Functional Testing with SWTBot and Maven/Tycho
Lorenzo Bettini, DISIA, Univ. Florence, Italy
Mickael Istria, Red Hat Inc.
![Page 2: Introduction to Functional Testing with SWTBot and … · Introduction to Functional Testing with SWTBot and ... Do I really need a running Eclipse to test this and that? ... Code](https://reader030.vdocument.in/reader030/viewer/2022013107/5b312ca47f8b9a744a8b9c12/html5/thumbnails/2.jpg)
What is SWTBot?
SWTBot is a simple Java API to interact with SWT widgets -and other typical Eclipse RCP UI artifacts-, using the usual vocabulary of high level UI operations.
![Page 3: Introduction to Functional Testing with SWTBot and … · Introduction to Functional Testing with SWTBot and ... Do I really need a running Eclipse to test this and that? ... Code](https://reader030.vdocument.in/reader030/viewer/2022013107/5b312ca47f8b9a744a8b9c12/html5/thumbnails/3.jpg)
Functional Testing● “UI logic” testing● Testing the behavior of the application with respect to user interactions
![Page 4: Introduction to Functional Testing with SWTBot and … · Introduction to Functional Testing with SWTBot and ... Do I really need a running Eclipse to test this and that? ... Code](https://reader030.vdocument.in/reader030/viewer/2022013107/5b312ca47f8b9a744a8b9c12/html5/thumbnails/4.jpg)
Software/Application Under Test (SUT/AUT)SWTBot is good for (and only for) SWT-based applications:
● with plain Java/SWT or● Eclipse RCP.
ie. No SWT => No SWTBot
![Page 5: Introduction to Functional Testing with SWTBot and … · Introduction to Functional Testing with SWTBot and ... Do I really need a running Eclipse to test this and that? ... Code](https://reader030.vdocument.in/reader030/viewer/2022013107/5b312ca47f8b9a744a8b9c12/html5/thumbnails/5.jpg)
What isn’t SWTBot?
SWTBot is not a test framework.
It can be used to write tests in whatever test framework, orchestrators (JUnit, TestNG...) and runtime (PDE Tests, Tycho surefire...)
![Page 6: Introduction to Functional Testing with SWTBot and … · Introduction to Functional Testing with SWTBot and ... Do I really need a running Eclipse to test this and that? ... Code](https://reader030.vdocument.in/reader030/viewer/2022013107/5b312ca47f8b9a744a8b9c12/html5/thumbnails/6.jpg)
Dont’s● SWTBot does NOT replace plain JUnit Tests● Always ask yourself “Do I need functional testing?”
○ It takes much longer to execute (you need a running Eclipse)○ Do I really need a running Eclipse to test this and that?
![Page 7: Introduction to Functional Testing with SWTBot and … · Introduction to Functional Testing with SWTBot and ... Do I really need a running Eclipse to test this and that? ... Code](https://reader030.vdocument.in/reader030/viewer/2022013107/5b312ca47f8b9a744a8b9c12/html5/thumbnails/7.jpg)
Target SWTBot usersSWTBot APIs mainly target SWT Developers who want an efficient way to write UI Tests, in a grain that’s close to the user actions.
The Test Recorder and Generator,
● may be used directly by end-users to easily record what happens and share it with developers as “Steps to Reproduce”.
● might also be considered in order to implement some macro support in Eclipse RCP applications, although it’s not its target.
![Page 8: Introduction to Functional Testing with SWTBot and … · Introduction to Functional Testing with SWTBot and ... Do I really need a running Eclipse to test this and that? ... Code](https://reader030.vdocument.in/reader030/viewer/2022013107/5b312ca47f8b9a744a8b9c12/html5/thumbnails/8.jpg)
An SWTBot test● SWTBot provides a fluent Java API
○ Hides most of the complexity of SWT and Eclipse○ Wraps SWT components into Java classes○ Methods for
■ Simulating user interaction■ Accessing SWT widgets
● Content-assist is your friend!
![Page 9: Introduction to Functional Testing with SWTBot and … · Introduction to Functional Testing with SWTBot and ... Do I really need a running Eclipse to test this and that? ... Code](https://reader030.vdocument.in/reader030/viewer/2022013107/5b312ca47f8b9a744a8b9c12/html5/thumbnails/9.jpg)
Custom runners
A screenshot will be automatically taken and saved for each failing test
![Page 10: Introduction to Functional Testing with SWTBot and … · Introduction to Functional Testing with SWTBot and ... Do I really need a running Eclipse to test this and that? ... Code](https://reader030.vdocument.in/reader030/viewer/2022013107/5b312ca47f8b9a744a8b9c12/html5/thumbnails/10.jpg)
Setup and Teardown:
![Page 11: Introduction to Functional Testing with SWTBot and … · Introduction to Functional Testing with SWTBot and ... Do I really need a running Eclipse to test this and that? ... Code](https://reader030.vdocument.in/reader030/viewer/2022013107/5b312ca47f8b9a744a8b9c12/html5/thumbnails/11.jpg)
What it looks like?
SWTBot generates and sends to the AUT/SUT corresponding SWT events for the UI operations.
TODO: Code snippet
![Page 12: Introduction to Functional Testing with SWTBot and … · Introduction to Functional Testing with SWTBot and ... Do I really need a running Eclipse to test this and that? ... Code](https://reader030.vdocument.in/reader030/viewer/2022013107/5b312ca47f8b9a744a8b9c12/html5/thumbnails/12.jpg)
SWTBot understands the “workbench” grainViews, perspectives… You can also use them rather than widgets.
![Page 13: Introduction to Functional Testing with SWTBot and … · Introduction to Functional Testing with SWTBot and ... Do I really need a running Eclipse to test this and that? ... Code](https://reader030.vdocument.in/reader030/viewer/2022013107/5b312ca47f8b9a744a8b9c12/html5/thumbnails/13.jpg)
DEMO Time
![Page 14: Introduction to Functional Testing with SWTBot and … · Introduction to Functional Testing with SWTBot and ... Do I really need a running Eclipse to test this and that? ... Code](https://reader030.vdocument.in/reader030/viewer/2022013107/5b312ca47f8b9a744a8b9c12/html5/thumbnails/14.jpg)
SWTBot also has APIs for● Nebula NatTable, Gallery...● GEF: Edit Parts
![Page 15: Introduction to Functional Testing with SWTBot and … · Introduction to Functional Testing with SWTBot and ... Do I really need a running Eclipse to test this and that? ... Code](https://reader030.vdocument.in/reader030/viewer/2022013107/5b312ca47f8b9a744a8b9c12/html5/thumbnails/15.jpg)
✘Long operations?
Don’t wait blindly: Thread.sleep(20000)
Explicitly wait for what you want!
Searching, timeout, and wait conditions
✔
![Page 16: Introduction to Functional Testing with SWTBot and … · Introduction to Functional Testing with SWTBot and ... Do I really need a running Eclipse to test this and that? ... Code](https://reader030.vdocument.in/reader030/viewer/2022013107/5b312ca47f8b9a744a8b9c12/html5/thumbnails/16.jpg)
Other Dont’s● Example
○ For your tests, you need to get rid of the Welcome page (if present)○ Don’t do that like that:
■ It’s not part of your functional test■ You waste time due to timeouts
✘
![Page 17: Introduction to Functional Testing with SWTBot and … · Introduction to Functional Testing with SWTBot and ... Do I really need a running Eclipse to test this and that? ... Code](https://reader030.vdocument.in/reader030/viewer/2022013107/5b312ca47f8b9a744a8b9c12/html5/thumbnails/17.jpg)
Do that programmaticallyYou won’t waste time for timeouts
✔
![Page 18: Introduction to Functional Testing with SWTBot and … · Introduction to Functional Testing with SWTBot and ... Do I really need a running Eclipse to test this and that? ... Code](https://reader030.vdocument.in/reader030/viewer/2022013107/5b312ca47f8b9a744a8b9c12/html5/thumbnails/18.jpg)
You need the Java perspective......for testing your views?
● Don’t open that simulating user interactions● That is not part of your functional test
✘
![Page 19: Introduction to Functional Testing with SWTBot and … · Introduction to Functional Testing with SWTBot and ... Do I really need a running Eclipse to test this and that? ... Code](https://reader030.vdocument.in/reader030/viewer/2022013107/5b312ca47f8b9a744a8b9c12/html5/thumbnails/19.jpg)
Do that programmaticallyRecall that SWTBot runs on a non-UI thread, thus PlatformUI.getWorkbench() will result with a NullPointerException
✔
![Page 20: Introduction to Functional Testing with SWTBot and … · Introduction to Functional Testing with SWTBot and ... Do I really need a running Eclipse to test this and that? ... Code](https://reader030.vdocument.in/reader030/viewer/2022013107/5b312ca47f8b9a744a8b9c12/html5/thumbnails/20.jpg)
Hands off!● While an SWTBot is running, keep your hands off!● Otherwise SWTBot will not be able to interact correctly with the AUT.● The AUT must have the focus.
TIP: in Linux you can run SWTBot tests in another DISPLAY
● Use Xephyr and a Window Manager inside (e.g., icewm, or similar)
![Page 21: Introduction to Functional Testing with SWTBot and … · Introduction to Functional Testing with SWTBot and ... Do I really need a running Eclipse to test this and that? ... Code](https://reader030.vdocument.in/reader030/viewer/2022013107/5b312ca47f8b9a744a8b9c12/html5/thumbnails/21.jpg)
Execution & AutomationAs it relies on regular test frameworks, SWTBot tests can very easily be run with usual build technologies such as PDE/Build and Tycho. Just DON’T RUN IN THE UI THREAD
![Page 22: Introduction to Functional Testing with SWTBot and … · Introduction to Functional Testing with SWTBot and ... Do I really need a running Eclipse to test this and that? ... Code](https://reader030.vdocument.in/reader030/viewer/2022013107/5b312ca47f8b9a744a8b9c12/html5/thumbnails/22.jpg)
Test Recorder and GeneratorPrinciple: the other way round, estimate what is the user action for a chain of SWT events.
Usage:
1. Make sure you have the recorder feature Installed2. File > New > SWTBot > Test from recorder (TODO)3. Decide of your AUT: current workbench or Run Configuration4. Ok5. Start recording6. Reuse code
![Page 23: Introduction to Functional Testing with SWTBot and … · Introduction to Functional Testing with SWTBot and ... Do I really need a running Eclipse to test this and that? ... Code](https://reader030.vdocument.in/reader030/viewer/2022013107/5b312ca47f8b9a744a8b9c12/html5/thumbnails/23.jpg)
DEMO Time
![Page 24: Introduction to Functional Testing with SWTBot and … · Introduction to Functional Testing with SWTBot and ... Do I really need a running Eclipse to test this and that? ... Code](https://reader030.vdocument.in/reader030/viewer/2022013107/5b312ca47f8b9a744a8b9c12/html5/thumbnails/24.jpg)
Recorder - caveatsSuboptimal “addressing”
Generated code is pretty basic, (doesn’t use intermediary variables nor general software best practices) => you usually have to refactor it.
Generator doesn’t know nor add wait conditions.
So…
Recorder is good to get a 1st iteration of a test, but some SWT developer almost always have to rework it.
![Page 25: Introduction to Functional Testing with SWTBot and … · Introduction to Functional Testing with SWTBot and ... Do I really need a running Eclipse to test this and that? ... Code](https://reader030.vdocument.in/reader030/viewer/2022013107/5b312ca47f8b9a744a8b9c12/html5/thumbnails/25.jpg)
SWTBot in CI environmentsNeed Window Manager (icewm, ...)
sudo: false language: java jdk: oraclejdk7 cache: directories: - $HOME/.m2 env: DISPLAY=:99.0 install: true addons: apt: packages: - metacity before_script: - sh -e /etc/init.d/xvfb start - metacity --sm-disable --replace 2> metacity.err & script: - export - mvn -f swtbot.example.parent/pom.xml clean verify
![Page 26: Introduction to Functional Testing with SWTBot and … · Introduction to Functional Testing with SWTBot and ... Do I really need a running Eclipse to test this and that? ... Code](https://reader030.vdocument.in/reader030/viewer/2022013107/5b312ca47f8b9a744a8b9c12/html5/thumbnails/26.jpg)
Contributinghttps://wiki.eclipse.org/SWTBot/Contributing
● Oomph setup to easily get you started● Push to Gerrit for review
○ Automatically triggers Hudson build job
![Page 27: Introduction to Functional Testing with SWTBot and … · Introduction to Functional Testing with SWTBot and ... Do I really need a running Eclipse to test this and that? ... Code](https://reader030.vdocument.in/reader030/viewer/2022013107/5b312ca47f8b9a744a8b9c12/html5/thumbnails/27.jpg)
References● https://wiki.eclipse.org/SWTBot● https://jaxenter.com/introduction-functional-testing-swtbot-123449.html● https://github.com/LorenzoBettini/swtbot-example