Introduction to Functional Testing with SWTBot and … · Introduction to Functional Testing with...

Preview:

Citation preview

Introduction to Functional Testing with SWTBot and Maven/Tycho

Lorenzo Bettini, DISIA, Univ. Florence, Italy

Mickael Istria, Red Hat Inc.

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.

Functional Testing● “UI logic” testing● Testing the behavior of the application with respect to user interactions

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

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...)

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?

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.

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!

Custom runners

A screenshot will be automatically taken and saved for each failing test

Setup and Teardown:

What it looks like?

SWTBot generates and sends to the AUT/SUT corresponding SWT events for the UI operations.

TODO: Code snippet

SWTBot understands the “workbench” grainViews, perspectives… You can also use them rather than widgets.

DEMO Time

SWTBot also has APIs for● Nebula NatTable, Gallery...● GEF: Edit Parts

✘Long operations?

Don’t wait blindly: Thread.sleep(20000)

Explicitly wait for what you want!

Searching, timeout, and wait conditions

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

Do that programmaticallyYou won’t waste time for timeouts

You need the Java perspective......for testing your views?

● Don’t open that simulating user interactions● That is not part of your functional test

Do that programmaticallyRecall that SWTBot runs on a non-UI thread, thus PlatformUI.getWorkbench() will result with a NullPointerException

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)

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

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

DEMO Time

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.

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

Contributinghttps://wiki.eclipse.org/SWTBot/Contributing

● Oomph setup to easily get you started● Push to Gerrit for review

○ Automatically triggers Hudson build job