Hello,
Just started looking at SWTBot which looks great. I wondered though why
there didn't seem to be JUnit 4 test runner support? I couldn't see anything
in the source so I created a runner (see patch below) which means you can
start a test like below.
@RunWith(SWTBotRunner.class)
public class FooTest {
private final SWTBot bot = new SWTBot();
@BeforeClass
public static void startTheApplication() throws TimeoutException,
InterruptedException {
new Thread(new Runnable() {
public void run() {
Main.main();
}
}).start();
waitForDisplay(5, SECONDS);
}
@Test
public void rightClickStartsConfiguration() throws Exception {
bot.button("click me").click();
bot.button("you just clicked me!").click();
}
private static void waitForDisplay(long timeout, TimeUnit unit) throws
TimeoutException, InterruptedException {
long endTime = System.currentTimeMillis() + unit.toMillis(timeout);
while (System.currentTimeMillis() < endTime) {
try {
Display display = SWTUtils.display();
if (display != null)
return;
} catch (Exception e) {
}
Thread.sleep(100);
}
throw new TimeoutException("timed out");
}
}
Seems to work for me, does it look about right to you guys?
Cheers,
Toby
### Eclipse Workspace Patch 1.0
#P net.sf.swtbot.finder
Index: .classpath
===================================================================
--- .classpath (revision 1233)
+++ .classpath (working copy)
@@ -1,11 +1,12 @@
-<?xml version="1.0" encoding="UTF-8"?>
-<classpath>
- <classpathentry kind="con"
path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/J2SE-1.4"/>
- <classpathentry kind="con" path="org.eclipse.pde.core.requiredPlugins">
- <accessrules>
- <accessrule kind="accessible" pattern="**"/>
- </accessrules>
- </classpathentry>
- <classpathentry kind="src" path="src"/>
- <classpathentry kind="output" path="bin"/>
-</classpath>
+<?xml version="1.0" encoding="UTF-8"?>
+<classpath>
+ <classpathentry kind="con"
path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/J2SE-1.4"/>
+ <classpathentry kind="con" path="org.eclipse.pde.core.requiredPlugins">
+ <accessrules>
+ <accessrule kind="accessible" pattern="**"/>
+ </accessrules>
+ </classpathentry>
+ <classpathentry kind="src" path="src"/>
+ <classpathentry kind="lib"
path="/net.sf.swtbot.build/libs/junit-4.4.jar"/>
+ <classpathentry kind="output" path="bin"/>
+</classpath>
Index: src/net/sf/swtbot/SWTBotRunner.java
===================================================================
--- src/net/sf/swtbot/SWTBotRunner.java (revision 0)
+++ src/net/sf/swtbot/SWTBotRunner.java (revision 0)
@@ -0,0 +1,41 @@
+package net.sf.swtbot;
+
+import java.io.File;
+import java.lang.reflect.InvocationTargetException;
+import java.lang.reflect.Method;
+
+import net.sf.swtbot.utils.ClassUtils;
+
+import org.junit.internal.runners.InitializationError;
+import org.junit.internal.runners.JUnit4ClassRunner;
+import org.junit.internal.runners.TestMethod;
+
+public class SWTBotRunner extends JUnit4ClassRunner {
+
+ public SWTBotRunner(Class<?> klass) throws InitializationError {
+ super(klass);
+ }
+
+ @Override
+ protected TestMethod wrapMethod(final Method method) {
+ return new TestMethod(method, getTestClass()) {
+ @Override
+ public void invoke(Object test) throws
IllegalArgumentException, IllegalAccessException, InvocationTargetException
{
+ try {
+ method.invoke(test);
+ } catch (Throwable e) {
+ captureScreenshot();
+ throw new InvocationTargetException(e);
+ }
+ }
+ };
+ }
+
+ private void captureScreenshot() {
+ String fileName = "screenshots/screenshot-" +
ClassUtils.simpleClassName(getClass()) + "." + getName() + ".png";
+ new File("screenshots").mkdirs();
+ SWTBotTestCase.captureScreenshot(fileName);
+ }
+
+}
+
--
Toby
------------------------------------------------------------------------------
_______________________________________________
SWTBot-users mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/swtbot-users
http://swtbot.org/ - a functional testing tool for SWT/Eclipse