SwtBot: Unit Testing Made Easy

5,640 views

Published on

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.

Published in: Technology
  • Be the first to like this

SwtBot: Unit Testing Made Easy

  1. 1. SWTBot: Unit Testing Made Easy<br />Eclipse Day India 2011<br />Ankit Goel<br />6th May, 2011<br />
  2. 2. Agenda<br />PART I – About SWTBot<br />Introduction to SWTBot<br />SWTBot Features<br />Supported Widgets<br />Test Executions<br />PART II - Integrating SWTBot tests execution with continuous build system<br />What more?<br />Limitations<br />
  3. 3. PART I – About SWTBot<br />
  4. 4. Introduction – What is SWTBot?<br />Java based UI/functional testing tool for testing SWT and Eclipse based applications<br />Set of API’s to access and manipulate widgets<br />Cross platform<br />Free and open source<br />Still in Incubation Phase in Eclipse<br />
  5. 5. Introduction – Why SWTBot?<br />Why not QTP? Why not PDE Junit? Why not…<br />Provides an intuitive way to access and test UI components in Eclipse<br />UI Testing covers all layers of software<br />Easy to learn if someone is already familiar with Java and JUnit<br />Can integrate very well within the IDE and provides excellent support for integration with Ant<br />
  6. 6. Introduction – Who is it for?<br />Developers<br />Unit tests<br />Integration with build ensures quality builds<br />QA<br />Test automation<br />Quality product<br />
  7. 7. Setting up Eclipse environment<br />SWTBot update site<br />http://www.eclipse.org/swtbot/downloads.php<br />Create a plugin project and setup dependencies<br />org.eclipse.ui<br />org.eclipse.core.runtime<br />org.eclipse.swtbot.eclipse.finder<br />org.eclipse.swtbot.junit4_x<br />org.eclipse.swtbot.swt.finder<br />org.junit4<br />org.hamcrest<br />Or, create a SWTBot test plugin<br />
  8. 8. How does it work?<br />SWTBot = bot which acts on SWT components<br />Entry point: org.eclipse.swtbot.eclipse.finder.SWTWorkbenchBot<br />Helper classes for widgets<br />To test:<br />Instantiate bot<br />SWTWorkbenchBotbot = new SWTWorkbenchBot();<br />Click on button<br />SWTBotButton button = bot.button("Hello, World!“);<br />button.click();<br />Congrats! You have just written your first SWTBOT test <br />
  9. 9. SWTBot Features<br />Finding controls based on visual cues<br />Support for simulated mouse and keyboard input<br />Querying widgets for state<br />UI specific assertions like: assertEnabled(Widget)<br />Investigating test failures<br />
  10. 10. SWTBot Features – Finding Controls<br />Controls can be found based on visual cues like:<br />Text<br />Label<br />Group<br />Tooltip<br />Ids<br />Combination of all or any of the above (Using matchers)<br />Eg:<br />Find textbox that has a label 'Username:‘<br />SWTBotText username = bot.textWithLabel("Username:"); <br />Identify button using tooltip <br />SWTBotToolbarButton save = bot.toolbarButtonWithTooltip("Save");<br />
  11. 11. SWTBot Features – Using matchers<br />Simple matchers<br />withText(“Finish”)<br />withLabel(“Username:”)<br />withRegex(“Proceed to step * ”)<br />widgetOfType(Button.class)<br />withStyle(SWT.ARROW, “SWT.ARROW”)<br />Combination of matchers<br />allOf(matchers)<br />anyOf(matchers)<br />not(matchers)<br />
  12. 12. Filtering controls using Matchers<br />Match : widgets of type 'Label' <br /> with regular expression 'Welcome, <USERNAME>' <br />Matcher matcher = allOf( <br />widgetOfType(Label.class), <br />withRegex("Welcome, .*") ); <br />Get the label that matches the matcher <br />SWTBotLabel label = <br /> new SWTBotLabel((Label) bot.widget(matcher));<br />
  13. 13. SWTBot Features – Simulate input <br />SWTBot provides API to simulate actions on widgets<br />Eg:<br />Click a button<br />button.click(); <br />Select a item in combobox<br />comboBox.select(“Option 12"); <br />Type in a textbox<br />text.typeText(“This is a demo string"); <br />Expand tree<br />tree.expandNode("MyProject", "src", "com", "example", "MyClass.java");<br />
  14. 14. SWTBot Features – Querying widgets<br />Provides operations to query the state of widgets<br />Some generic queries like: getText(), isEnabled(), isVisible() are available on all widgets<br />Eg:<br />Check state of a checkbox<br />booleanchecked = checkbox.isChecked();<br />Check if a radio button is selected<br />boolean selected = radio.isSelected();<br />
  15. 15. SWTBot Features – Investigating test failures<br />Stack trace in Junit view<br />Capturing screen shot when test case fails<br />@RunWith(SWTBotJunit4ClassRunner.class)<br />publicclassMessageCreateTest{<br />...<br /> …<br />}<br />
  16. 16. Supported Widgets<br />Support for most SWT controls<br />Support for most UI operations on SWT controls<br />Support for Eclipse based contributions:<br />Views<br />Text Editors (autocompletion, typing, etc)<br />View Toolbars and View Menus<br />
  17. 17. SWT Test Execution<br />By launching them from launch configuration (same as Junit execution)<br />From command line, using shell script or ant<br />
  18. 18. Demo<br />A small SWTBot example illustrating the simplicity of SWTBot<br />Your first SWTBot<br />
  19. 19. PART II – Integrating SWTBot tests execution with continuous build system<br />
  20. 20. Integration of test execution with continuous build<br />Build plugins to be tested<br />Build SWTBot test plugins<br />Install plugins to be tested, test plugins and headless testing framework (http://www.eclipse.org/swtbot/downloads.php) into eclipse<br />Start SWTBot test case execution<br />Format the generated report and mail if required<br />
  21. 21. Pre-requisite<br />Each test plugin must contain test.xml<br />Set appropriate properties<br />Delegate execution of test to a library file<br />Test plugins should always be ‘unpacked’<br />
  22. 22. Under the covers…<br />For each test plugin<br />Invoke test.xml which will<br />Set required properties like class name, plugin name, directory to use as temp workspace etc<br />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<br />Invoke target ‘collect’ of library file located in ‘org.eclipse.swtbot.eclipse.junit4.headless’ plugin which will consolidate test case results in one file<br />Format the generated results file (xml file) into desired format (usually html)<br />
  23. 23. Snippet – test.xml<br /><anttarget="swtbot-test"antfile="${library-file}"dir="${eclipse-home}"><br /> <property name="data-dir"value="${temp-workspace}" /><br /> <propertyname="plugin-name"value="${plugin-name}" /><br /> <propertyname="classname”value=“${class-name}" /><br /> <propertyname="vmargs"value=" -Xms128M -Xmx368M - XX:MaxPermSize=256M" /><br /></ant><br />
  24. 24. Still confused ?<br />This would surely help<br />
  25. 25. What more?<br />Parse results file and fail build if any of the test fails – ensuring only quality builds go to QA<br />Code Coverage – write more tests to test uncovered code<br />
  26. 26. Limitations<br />Less documentation available<br />Some SWT widgets not supported yet!<br />No support for other UI toolkits – GEF, Nebula etc<br />

×