SwtBot: Unit Testing Made Easy

Post on 08-Feb-2015

5.028 views 4 download

description

SWTBot is a open source UI testing tool for SWT and Eclipse based applications. It requires bare minimum learning since it is Java based and integrates well with the JUnit framework.An ideal unit testing tool for SWT and Eclipse based applications developers.

Transcript of SwtBot: Unit Testing Made Easy

SWTBot: Unit Testing Made Easy

Eclipse Day India 2011

Ankit Goel6th May, 2011

© 2011 Progress Software Corporation. All rights reserved.2

Agenda

PART I – About SWTBot• Introduction to SWTBot• SWTBot Features• Supported Widgets• Test Executions

PART II - Integrating SWTBot tests execution with continuous build system

What more?

Limitations

PART I – About SWTBot

© 2011 Progress Software Corporation. All rights reserved.4

Introduction – What is SWTBot?

Java based UI/functional testing tool for testing SWT and Eclipse based applications

Set of API’s to access and manipulate widgets

Cross platform

Free and open source

Still in Incubation Phase in Eclipse

© 2011 Progress Software Corporation. All rights reserved.5

Introduction – Why SWTBot?

Why not QTP? Why not PDE Junit? Why not…

Provides an intuitive way to access and test UI components in Eclipse

UI Testing covers all layers of software

Easy to learn if someone is already familiar with Java and JUnit

Can integrate very well within the IDE and provides excellent support for integration with Ant

© 2011 Progress Software Corporation. All rights reserved.6

Introduction – Who is it for?

Developers• Unit tests• Integration with build ensures quality builds

QA• Test automation• Quality product

© 2011 Progress Software Corporation. All rights reserved.7

Setting up Eclipse environment

SWTBot update site• http://www.eclipse.org/swtbot/downloads.php

Create a plugin project and setup dependencies• org.eclipse.ui• org.eclipse.core.runtime• org.eclipse.swtbot.eclipse.finder• org.eclipse.swtbot.junit4_x• org.eclipse.swtbot.swt.finder• org.junit4• org.hamcrest

Or, create a SWTBot test plugin

© 2011 Progress Software Corporation. All rights reserved.8

How does it work?

SWTBot = bot which acts on SWT components

Entry point: org.eclipse.swtbot.eclipse.finder.SWTWorkbenchBot

Helper classes for widgets

To test:• Instantiate bot

SWTWorkbenchBot bot = new SWTWorkbenchBot();• Click on button

SWTBotButton button = bot.button("Hello, World!“);

button.click();

Congrats! You have just written your first SWTBOT test

© 2011 Progress Software Corporation. All rights reserved.9

SWTBot Features

Finding controls based on visual cues

Support for simulated mouse and keyboard input

Querying widgets for state

UI specific assertions like: assertEnabled(Widget)

Investigating test failures

© 2011 Progress Software Corporation. All rights reserved.10

SWTBot Features – Finding Controls

Controls can be found based on visual cues like:• Text• Label• Group• Tooltip• Ids• Combination of all or any of the above (Using matchers)

Eg:• Find textbox that has a label 'Username:‘

SWTBotText username = bot.textWithLabel("Username:");

• Identify button using tooltip

SWTBotToolbarButton save = bot.toolbarButtonWithTooltip("Save");

© 2011 Progress Software Corporation. All rights reserved.11

SWTBot Features – Using matchers

Simple matchers• withText(“Finish”)• withLabel(“Username:”)• withRegex(“Proceed to step * ”)• widgetOfType(Button.class)• withStyle(SWT.ARROW, “SWT.ARROW”)

Combination of matchers• allOf(matchers)• anyOf(matchers)• not(matchers)

© 2011 Progress Software Corporation. All rights reserved.12

Filtering controls using Matchers

Match : widgets of type 'Label'

with regular expression 'Welcome, <USERNAME>'

Matcher matcher = allOf(

widgetOfType(Label.class),

withRegex("Welcome, .*") );

Get the label that matches the matcher

SWTBotLabel label =

new SWTBotLabel((Label) bot.widget(matcher));

© 2011 Progress Software Corporation. All rights reserved.13

SWTBot Features – Simulate input

SWTBot provides API to simulate actions on widgets

Eg:• Click a button

button.click();

• Select a item in combobox

comboBox.select(“Option 12");

• Type in a textbox

text.typeText(“This is a demo string");

• Expand tree

tree.expandNode("MyProject", "src", "com", "example", "MyClass.java");

© 2011 Progress Software Corporation. All rights reserved.14

SWTBot Features – Querying widgets

Provides operations to query the state of widgets

Some generic queries like: getText(), isEnabled(), isVisible() are available on all widgets

Eg:• Check state of a checkbox

boolean checked = checkbox.isChecked();

• Check if a radio button is selected

boolean selected = radio.isSelected();

© 2011 Progress Software Corporation. All rights reserved.15

SWTBot Features – Investigating test failures

Stack trace in Junit view

Capturing screen shot when test case fails@RunWith(SWTBotJunit4ClassRunner.class)

public class MessageCreateTest {

...

}

© 2011 Progress Software Corporation. All rights reserved.16

Supported Widgets

Support for most SWT controls

Support for most UI operations on SWT controls

Support for Eclipse based contributions:• Views• Text Editors (autocompletion, typing, etc)• View Toolbars and View Menus

Button Checkbox Coolbar Shell Toolbar

Button Push CTabItem StyledText ToolItem

Button Radio DateTime TabItem Tray

Button Toggle Group Table Tree

CCombo Label TableColumn TreeItem

CLabel List TableItem

Combo Menu Text

© 2011 Progress Software Corporation. All rights reserved.17

SWT Test Execution

By launching them from launch configuration (same as Junit execution)

From command line, using shell script or ant

© 2011 Progress Software Corporation. All rights reserved.18

Demo

A small SWTBot example illustrating the simplicity of SWTBot

Your first SWTBot

PART II – Integrating SWTBot tests execution with continuous build system

© 2011 Progress Software Corporation. All rights reserved.20

Integration of test execution with continuous build

Build plugins to be tested

Build SWTBot test plugins

Install plugins to be tested, test plugins and headless testing framework (http://www.eclipse.org/swtbot/downloads.php) into eclipse

Start SWTBot test case execution

Format the generated report and mail if required

© 2011 Progress Software Corporation. All rights reserved.21

Pre-requisite

Each test plugin must contain test.xml• Set appropriate properties• Delegate execution of test to a library file

Test plugins should always be ‘unpacked’

© 2011 Progress Software Corporation. All rights reserved.22

Under the covers…

For each test plugin• Invoke test.xml which will

– Set required properties like class name, plugin name, directory to use as temp workspace etc

– Invoke target ‘swtbot-test’ of library file located in ‘org.eclipse.swtbot.eclipse.junit4.headless’ plugin which will then launch and execute all test cases

– Invoke target ‘collect’ of library file located in ‘org.eclipse.swtbot.eclipse.junit4.headless’ plugin which will consolidate test case results in one file

• Format the generated results file (xml file) into desired format (usually html)

© 2011 Progress Software Corporation. All rights reserved.23

Snippet – test.xml

<ant target="swtbot-test" antfile="${library-file}" dir="${eclipse-home}">

<property name="data-dir" value="${temp-workspace}" />

<property name="plugin-name" value="${plugin-name}" />

<property name="classname” value=“${class-name}" />

<property name="vmargs" value=" -Xms128M -Xmx368M -XX:MaxPermSize=256M" />

</ant>

© 2011 Progress Software Corporation. All rights reserved.24

Still confused ?

This would surely help

© 2011 Progress Software Corporation. All rights reserved.25

What more?

Parse results file and fail build if any of the test fails – ensuring only quality builds go to QA

Code Coverage – write more tests to test uncovered code

© 2011 Progress Software Corporation. All rights reserved.26

Limitations

Less documentation available

Some SWT widgets not supported yet!

No support for other UI toolkits – GEF, Nebula etc

© 2011 Progress Software Corporation. All rights reserved.27