Testing SWT application with SWTBot:
I came across one requirement, where I need to test an SWT application using SWTBot. I had no idea how to start with SWTBot, after referring few blogs I can setup the SWTBot using eclipse. And also I found that most of the tutorials describes the testing of an Eclipse plug-in but I couldn't find anything regarding my requirement (like testing third party SWT application). I don't even know whether it is possible or not.
My Requirement - I have one JNLP file and after running this JNLP file SWT application will open. Once application opens I have to login to the application using UserID and Pwd. I am trying with below code bur its trowing an exception "WidgetNotFoundException".
Code
import java.io.IOException;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swtbot.swt.finder.SWTBot;
import org.eclipse.swtbot.swt.finder.utils.SWTBotPreferences;
import org.eclipse.swtbot.swt.finder.waits.Conditions;
import org.eclipse.swtbot.swt.finder.widgets.SWTBotButton;
import org.eclipse.swtbot.swt.finder.widgets.SWTBotText;
import org.junit.Test;
public class TestLogin {
@Test
public void verifyLogin() throws IOException, InterruptedException {
// Set the timeout to 6 seconds
SWTBotPreferences.TIMEOUT = 6000;
//Launch the SWT applicaton.
Runtime.getRuntime().exec("javaws C:\\Users\\anand\\Downloads\\Testing.jnlp");
Thread.sleep(60000);
Display display = new Display();
Shell shell = new Shell(display);
SWTBot bot = new SWTBot(shell);
//"User Authentication" is the name of the login window
bot.waitUntil(Conditions.shellIsActive("User Authentication"));
bot.shell("User Authentication").activate();
bot.activeShell();
bot.textWithLabel("User ID:").setText("anand");
bot.textWithLabel("Password:").setText("anand123");
bot.button("Login").click();
/*
SWTBotText userID = bot.textWithLabel("User ID:");
SWTBotText passwordText = bot.textWithLabel("Password:");
SWTBotButton loginButton = bot.button("Login");
userID.setFocus();
userID.setText("Superman");
//assert(userID.getText().equals("Superman"));
passwordText.setFocus();
passwordText.setText("test123");
//assert(userID.getText().equals("test123"));
loginButton.setFocus();
loginButton.click();
*/
}
}
Output:
org.eclipse.swtbot.swt.finder.exceptions.WidgetNotFoundException: Could not find shell matching: with text 'User Authentication'
at org.eclipse.swtbot.swt.finder.SWTBotFactory.waitUntilWidgetAppears(SWTBotFactory.java:472)
at org.eclipse.swtbot.swt.finder.SWTBotFactory.shells(SWTBotFactory.java:116)
at org.eclipse.swtbot.swt.finder.SWTBotFactory.shell(SWTBotFactory.java:106)
at org.eclipse.swtbot.swt.finder.SWTBotFactory.shell(SWTBotFactory.java:97)
at com.fis.teller.testscripts.TestLogin.verifyLogin(TestLogin.java:27)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:606)
at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:47)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:44)
at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:271)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:70)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:50)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:238)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:63)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:236)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:53)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:229)
at org.junit.runners.ParentRunner.run(ParentRunner.java:309)
at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:50)
at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:467)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:683)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:390)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:197)
Caused by: org.eclipse.swtbot.swt.finder.widgets.TimeoutException: Timeout after: 6000 ms.: Could not find shell matching: with text 'User Authentication'
at org.eclipse.swtbot.swt.finder.SWTBotFactory.waitUntil(SWTBotFactory.java:522)
at org.eclipse.swtbot.swt.finder.SWTBotFactory.waitUntil(SWTBotFactory.java:496)
at org.eclipse.swtbot.swt.finder.SWTBotFactory.waitUntil(SWTBotFactory.java:484)
at org.eclipse.swtbot.swt.finder.SWTBotFactory.waitUntilWidgetAppears(SWTBotFactory.java:466)
... 27 more
I think that I'm not able to transfer the the control to "User Authentication" shell/Widget/window.