使用TestNG-abbot测试GUI

一、需要类库
abbot库:abbot-1.0.0.rc5.zip
TestNG库:testng-5.5.zip
TestNG-Abbot库:testng-abbot-0[1].3.2-030807.zip
TestNG-Eclipse的插件:testng-eclipse-5.5.0.0.zip
 
二、测试类代码
import java.awt.FlowLayout;
import java.awt.GridLayout;
import java.awt.HeadlessException;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
public class WordFinder extends JFrame implements ActionListener {
 private static final long serialVersionUID = 1L;
 private JLabel labWord = new JLabel("Word");
 private JTextField textWord = new JTextField(10);
 private JLabel lblResult = new JLabel();
 JButton button = new JButton("Find Word");
 
 public WordFinder() throws HeadlessException {
  super("Word Finder");
  setLayout(new GridLayout(3,1,10,10));
  
  //
  JPanel p = new JPanel();
  p.setLayout(new FlowLayout(FlowLayout.LEFT,10,10));
  p.add(labWord);
  textWord.setName("textWord");
  p.add(textWord);
  add(p);
  
  //
  lblResult.setName("lblResult");
  add(lblResult);
  
  //
  button.setName("button");
  button.addActionListener(this);
  add(button);
  
  //
  addWindowListener(new WindowAdapter() {
   public void windowClosing(WindowEvent evt) {
    System.exit(0);
   }
  });
 
 }
 public static void main(String[] args) {
  WordFinder frame = new WordFinder();
  frame.pack();
  frame.setVisible(true);
 }
 public void actionPerformed(ActionEvent arg0) {
  lblResult.setText("Enter " + textWord.getText());
  textWord.requestFocus();
 }
}
三、测试代码
import java.awt.Dimension;
import org.testng.abbot.AbbotFixture;
import org.testng.abbot.fixture.JButtonFixture;
import org.testng.abbot.fixture.JLabelFixture;
import org.testng.abbot.fixture.JTextComponentFixture;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Test;

public class WordFinderGUITest {
 public AbbotFixture fixture;
 
 @BeforeMethod
 public void initializeGUI() {
  fixture = new AbbotFixture();
  fixture.showWindow(new WordFinder(),new Dimension(200,200));
 }
 
 @AfterMethod
 public void tearDownGUI() {
  fixture.cleanUp();
 }
 
 @Test
 public void assertDefinitionPresent() {
  String text = "MCY";
  JTextComponentFixture text1 = new JTextComponentFixture(this.fixture,"textWord");
  text1.enterText(text);
  
  JButtonFixture bfix = new JButtonFixture(this.fixture, "button");
  bfix.click();
  
  JLabelFixture lfix = new JLabelFixture(this.fixture, "lblResult");
  lfix.shouldHaveThisText("Enter " + text);
 }
}

相当漂亮的功能

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值