Thursday, December 3, 2009

SWTBot test for RCP application in Eclipse

We are going to develop SWTBot test package to test eclipse RCP application. You can refer previous post for RCP application for which We are going to write test package.


INPUT POSTS(Create simple eclipse RCP)


Requirement:: SWTBot for Eclipse 3.5 Galileo

for the installation of SWTBot as eclipse plugin you can refer (http://www.eclipse.org/swtbot/downloads.php -> http://download.eclipse.org/technology/swtbot/galileo/dev-build/update-site)


Create test package:


Add source folder src/test/java to project and create package ‘com.sam.rcp.client.example’ in this folder. Create class ViewTest in this package.


Double click on plugìn.xml and click on dependencies tab. Add following required plugins


org.hamcrest;bundle-version="1.1.0",

org.eclipse.swtbot.eclipse.finder;bundle-version="2.0.0",

org.eclipse.swtbot.junit4_x;bundle-version="2.0.0",

org.eclipse.swtbot.swt.finder;bundle-version="2.0.0",

org.junit4;bundle-version="4.5.0"

Java Source Code:

ViewTest.java

package com.sam.rcp.client.example;

import junit.framework.TestCase;

import org.eclipse.swtbot.eclipse.finder.SWTWorkbenchBot;

import org.eclipse.swtbot.swt.finder.widgets.SWTBotTable;

import org.eclipse.swtbot.swt.finder.widgets.SWTBotTree;

import org.eclipse.swtbot.swt.finder.widgets.SWTBotTreeItem;

public class ViewTest extends TestCase {

public SWTWorkbenchBot bot;

public ViewTest() {

bot = new SWTWorkbenchBot();

}

public void testChangeView() throws Exception {

if (bot.viewByTitle("Welcome") != null) {

bot.viewByTitle("Welcome").close();

}

bot.menu("Window").menu("Show View").menu("Other...").click();

SWTBotTree tree = bot.tree();

for (SWTBotTreeItem item : tree.getAllItems()) {

if ("Other".equalsIgnoreCase(item.getText())) {

item.expand();

for (SWTBotTreeItem element : item.getItems()) {

if ("View".equals(element.getText())) {

element.select();

}

}

}

}

bot.button("OK").click();

SWTBotTable table = bot.table();

//System.out.println(table);

Thread.sleep(2000);

int rowCount = table.rowCount();

String[] expectation = { "One", "Two", "Three" };

int currentRow = 0;

for (currentRow = 0; currentRow <>

assertEquals(expectation[currentRow], table

.getTableItem(currentRow).getText());

}

}

}

Above test method will launch an eclipse and perform all operation defined in java code. It will open required view and get the value of elements. These values will be compared against given values.

Run RCP test case:

Right click on ViewTest.java -> Run As -> SWTBot Test. This will launch a new eclipse RCP and
perform all operations. After completion window will close automatically. You can see the test
result in JUnit widow


You can analyze all operation visually, this will give you great feel.

1 comment: