我想为GUI编写测试用例。我想知道如何模拟JButton的单击,或者如何提取JTable的元素。
为此,我构建了一个简单的GUI,如果单击按钮并且JTextfield为空,则将计数增加1,但是如果提供了数字,则计数将被JTextfield中的整数代替。当然,我想使用Regex来确保输入到JTextfield中的文本实际上是整数,但是让我们假设用户不会四处乱逛并输入非整数。另外,当JTable添加新行时,JLabel将更新当前计数。
这是代码:
import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import javax.swing.GroupLayout; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JPanel; import javax.swing.JScrollPane; import javax.swing.JTable; import javax.swing.JTextField; import javax.swing.SwingUtilities; import javax.swing.table.DefaultTableModel; public class sampleGUI extends JFrame implements ActionListener { private Integer previous_count; private Integer current_count; private JButton Button; private JTable table; private JTextField text; private DefaultTableModel model; private JScrollPane scroll; private JLabel label; public sampleGUI() { previous_count = null; current_count = 0; JFrame frame = new JFrame("Sample"); JPanel panel = new JPanel(); GroupLayout layout = new GroupLayout(panel); panel.setLayout(layout); label = new JLabel("Current Count: " + Integer.toString(current_count)); text = new JTextField(15); Button = new JButton("Change the Count!"); model = new DefaultTableModel(); model.addColumn("Previous Count"); model.addColumn("Current Count"); table = new JTable(model); scroll = new JScrollPane(table); layout.setHorizontalGroup(layout .createParallelGroup(GroupLayout.Alignment.LEADING) .addGroup( layout.createSequentialGroup().addComponent(label) .addComponent(text).addComponent(Button)) .addComponent(scroll)); layout.setVerticalGroup(layout .createSequentialGroup() .addGroup( layout.createParallelGroup( GroupLayout.Alignment.BASELINE) .addComponent(label).addComponent(text) .addComponent(Button)).addComponent(scroll)); Button.addActionListener(this); frame.add(panel); frame.pack(); frame.setVisible(true); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); } @Override public void actionPerformed(ActionEvent e) { if (e.getSource() == Button) { if (text.getText().equals("")) { previous_count = current_count; current_count++; label.setText("Current Count: " + Integer.toString(current_count)); model.addRow(new Object[] { current_count, previous_count }); } else { previous_count = current_count; current_count = Integer.parseInt(text.getText()); label.setText("Current Count: " + Integer.toString(current_count)); text.setText(""); model.addRow(new Object[] { current_count, previous_count }); } table.changeSelection(table.getRowCount() - 1, 0, false, false); } } public static void main(final String[] args) { SwingUtilities.invokeLater(new Runnable() { public void run() { sampleGUI gui = new sampleGUI(); } }); } }
假设我要模拟打开GUI,然后单击一次按钮而不输入任何文本,然后输入1234并单击按钮,然后单击按钮而不输入任何文本,JTable应该具有3列:{{1,0 },{1234、1},{1235、1234}}。我该如何编写测试?谢谢!
Java SE随附了用于执行此操作的标准工具,即Robot类。我只用它来编写游戏机器人并通过套接字服务器/客户端对远程控制单独的计算机,但是它实际上是用于自动测试的,因此它应该对您有用。基本格式很简单:
Robot bot = new Robot(); bot.mouseMove(10,10); bot.mousePress(InputEvent.BUTTON1_MASK); //add time between press and release or the input event system may //not think it is a click try{Thread.sleep(250);}catch(InterruptedException e){} bot.mouseRelease(InputEvent.BUTTON1_MASK);
当然,您也可以使用适当的keyPress / keyRelease方法以类似的方式模拟键盘事件。我有时发现使用机械手类的screenCapture方法以及在屏幕上查找图像并确定单击位置很有用。
注意:这并不要求您要测试的窗口是基于awt / swing构建的,但是它确实要求您使用的java实现支持awt。