问题描述
|
我想用SWTBot测试一个简单的SWT GUI应用程序。不幸的是,我不知道如何开始。有一些教程描述了Eclipse插件的测试,但是我找不到关于我的问题的任何信息。我什至不知道是否可能。
解决方法
好吧,这很有可能。请按照以下步骤操作。
下载SWTBot进行SWT测试
将其放在
<eclipsehome>/dropins
文件夹中
重新开始蚀
现在,您就可以开始使用SWTBot了。
出于演示目的,我为您编写了一个小的“登录”对话框,它看起来像这样:
码
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;
public class SampleSWTUI
{
public Shell showGUI(final Display display)
{
Shell shell = new Shell(display);
shell.setLayout(new GridLayout(3,true));
shell.setText(\"Sample SWT UI\");
new Label(shell,SWT.NONE).setText(\"User Name: \");
final Text nameText = new Text(shell,SWT.BORDER);
nameText.setText (\"\");
GridData data = new GridData(SWT.FILL,SWT.FILL,true,false);
data.horizontalSpan = 2;
nameText.setLayoutData(data);
new Label(shell,SWT.NONE).setText(\"Password: \");
final Text passwordText = new Text(shell,SWT.BORDER|SWT.PASSWORD);
passwordText.setText (\"\");
data = new GridData(SWT.FILL,false);
data.horizontalSpan = 2;
passwordText.setLayoutData(data);
Button loginButton = new Button (shell,SWT.PUSH);
loginButton.setText (\"Login\");
data = new GridData(SWT.FILL,false);
data.horizontalSpan = 3;
loginButton.setLayoutData(data);
loginButton.addSelectionListener(new SelectionAdapter(){
public void widgetSelected(SelectionEvent e)
{
String user = nameText.getText();
String password = passwordText.getText();
System.out.println(\"\\n\\n\\n\");
if(user.equals(\"Favonius\") && password.equals(\"abcd123\")){
System.out.println(\"Success !!!\");
}else {
System.err.println(\"What the .. !! Anyway it is just a demo !!\");
}
}
});
shell.pack();
shell.open();
return shell;
}
public static void main(String [] args)
{
Display display = new Display();
Shell shell = new SampleSWTUI().showGUI(display);
while (!shell.isDisposed()) {
if (!display.readAndDispatch()) display.sleep();
}
display.dispose();
}
}
现在创建一个JUnit测试用例(如果您不熟悉它,请用Google创建)。还要在类路径中添加SWTBot中存在的所有jar文件(已下载的jar文件)。
现在,首先创建一个显示(因为应用程序需要一个)。还要获取您的小部件/控件所在的容器的句柄。就我而言,这是壳牌。
SWTBot代码
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.widgets.SWTBotButton;
import org.eclipse.swtbot.swt.finder.widgets.SWTBotText;
import org.junit.Test;
public class SWTBotDemo
{
@Test
public void test()
{
SWTBotPreferences.PLAYBACK_DELAY = 100; // slow down tests...Otherwise we won\'t see anything
Display display = new Display();
Shell shell = new SampleSWTUI().showGUI(display);
SWTBot bot = new SWTBot(shell);
SWTBotButton loginButton = bot.button(\"Login\");
SWTBotText userText = bot.textWithLabel(\"User Name: \");
SWTBotText passwordText = bot.textWithLabel(\"Password: \");
userText.setFocus();
userText.setText(\"Superman\");
assert(userText.getText().equals(\"Superman\"));
passwordText.setFocus();
passwordText.setText(\"test123\");
assert(userText.getText().equals(\"test123\"));
loginButton.setFocus();
loginButton.click();
userText.setFocus();
userText.setText(\"Favonius\");
assert(userText.getText().equals(\"Favonius\"));
passwordText.setFocus();
passwordText.setText(\"abcd123\");
assert(userText.getText().equals(\"abcd123\"));
loginButton.setFocus();
loginButton.click();
while (!shell.isDisposed()) {
if (!display.readAndDispatch()) display.sleep();
}
display.dispose();
}
}
现在,所有SWTBot方法和变量都在源代码中得到了很好的定义,并且源代码捆绑在SWTBot jar中。因此,您始终可以继续操作并破解其源代码。
进一步阅读
http://wiki.eclipse.org/SWTBot/常见问题解答
http://wiki.eclipse.org/SWTBot/UsersGuide
希望这会有所帮助。