免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 1513 | 回复: 0
打印 上一主题 下一主题

0.10 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2009-09-06 19:18 |只看该作者 |倒序浏览
package abbot.easytest;
import java.awt.Button;import java.awt.Component;import java.awt.Frame;import java.awt.TextField;import java.lang.reflect.Method;
import javax.swing.JButton;import javax.swing.JOptionPane;import javax.swing.JTextField;
import abbot.script.ComponentReference;import abbot.tester.ComponentTester;import abbot.tester.Robot;
import junit.extensions.abbot.ComponentTestFixture;
/** * 该程序是进行UI测试的通用框架,基于abbot的基础之上 *  * @author 吴笑笑 *  */public class SwingEasyFixture extends ComponentTestFixture {
        private ComponentTester testBasic = null;
        public SwingEasyFixture(String name) {                super(name);        }
        /**         * 启动应用程序         *          * @param frameclass         *            要测试的窗口         * @param methodName         *            启动窗口时候的方法         * @throws Exception         */        public void init(Class frameclass, String methodName)                        throws Exception {                // 如果方法为空的话,就新建一个对象,否则就执行这个方法                if (methodName == null) {                        frameclass.newInstance();                } else {                        Method method = frameclass.getMethod(methodName);                        method.invoke(frameclass.newInstance());                }        }
        /**         * 获得窗口上的控件,主要是根据该控件的name属性来获取         *          * @param id         *            Desired ID for the reference. Only used if this reference is         *            to be passed as the parent, window, or invoker of another.         * @param component         *            想要获得的控件的类型         * @param componentName         *            Name of the component, or null         * @param tag         *            Tag as returned by ComponentTester.getTag(Component)         * @return         * @throws Exception         */        private Component getComponent(String id,                        Class component, String componentName,                        String tag) throws Exception {                ComponentReference ref = new ComponentReference(id, component,                                componentName, tag);                return getFinder().findComponent(ref);
        }
        /**         * 点击按钮         *          * @param component         *            按钮的类型(Button or JButton)         * @param buttonName         *            按钮的名字         * @throws Exception         */        public void pressButton(String id, Component component, String buttonName,                        String tag) throws Exception {                // 获得button的测试者                testBasic = new ComponentTester();                // 如果是JButton如下进行                if (component instanceof JButton) {                        JButton jbutton = (JButton) getComponent(id, JButton.class,                                        buttonName, tag);                        testBasic.actionClick(jbutton);                        // 如果是Button如下进行                } else if (component instanceof Button) {                        Button button = (Button) getComponent(id, Button.class, buttonName,                                        tag);                        testBasic.actionClick(button);                }        }
        /**         * 向文本框中输入字符         *          * @param component         *            文本框的类型(TextField or JTextField)         * @param textName         *            文本框的名字         * @param text         *            要输入的字符串         */        public void enterText(String id, Component component, String textName,                        String tag, String text) throws Exception {                // 如果是JTextField如下进行                if (component instanceof JTextField) {                        JTextField jtextfield = (JTextField) getComponent(id,                                        JTextField.class, textName, tag);                        jtextfield.setText(text);                        // 如果是TextField如下进行                } else if (component instanceof TextField) {                        TextField textfield = (TextField) getComponent(id, TextField.class,                                        textName, tag);                        textfield.setText(text);                }        }
        /**         * 获得文本框中的内容,一下参数的解释和上面的一样         *          * @param id         * @param component         * @param textName         * @param tag         * @return         * @throws Exception         */        public String getText(String id, Component component, String textName,                        String tag) throws Exception {                // 如果是JTextField如下进行                if (component instanceof JTextField) {                        JTextField jtextfield = (JTextField) getComponent(id,                                        JTextField.class, textName, tag);                        return jtextfield.getText();                        // 如果是Button如下进行                } else if (component instanceof TextField) {                        TextField textfield = (TextField) getComponent(id, TextField.class,                                        textName, tag);                        return textfield.getText();                }                return null;        }
        /**         * 获得JOptionPane对话框的Message,下面的参数解释和上面的一样         *          * @param id         * @param name         * @param tag         * @return         * @throws Exception         */        public String getJOptionPaneMessage(String id, String name, String tag)                        throws Exception {                JOptionPane joptionpane = (JOptionPane) getComponent(id,                                JOptionPane.class, name, tag);                return (String) joptionpane.getMessage();        }
        /**         * 执行的动作之间停顿的时间         */        public void stopAction(int time) {                (new Robot()).delay(time);        }}
package abbot.easytest;
import java.awt.Button;import java.awt.Frame;import java.awt.TextField;import junit.extensions.abbot.ComponentTestFixture;import abbot.script.ComponentReference;import abbot.tester.ComponentTester;
public class TestAbbot {                SwingEasyFixture test =null;        public TestAbbot() throws Exception {                test = new SwingEasyFixture("PlusFrame");                test.init(PlusFrame.class,"showFrame");        }
//        class GuiTest extends SwingEasyFixture // From Abbot's JUnit extensions//        {//                public GuiTest(String name) {//                        super(name);//                }////                public void setUp() throws Exception {//                        //启动程序窗口//                        init(PlusFrame.class, "showFrame");//                        //向第一个文本框中输入内容//                        super.enterText("bbbbb",new TextField(),"bbbbb","firsttextfield","0");//                        //向第二个文本框中输入内容//                        super.enterText("ccccc",new TextField(),"ccccc","secondtextfield","10");//                }//        }
        public String value() throws Exception {                //向第一个文本框中输入内容                test.enterText("bbbbb",new TextField(),"bbbbb","firsttextfield","0");                test.stopAction(1000);                //向第二个文本框中输入内容                test.enterText("ccccc",new TextField(),"ccccc","secondtextfield","10");                test.stopAction(1000);        test.pressButton("equal",new Button(),"equal","equalsbutton");        test.stopAction(1000);                return test.getText("ddddd",new TextField(),"ddddd","threetextfield");        }
        public static void main(String args[]) {                try {                        TestAbbot a = new TestAbbot();
                        System.out.println(a.value());                } catch (Exception e) {                        e.printStackTrace();                }        }}
package abbot.easytest;
import java.awt.Button;import java.awt.FlowLayout;import java.awt.Frame;import java.awt.Label;import java.awt.TextField;import java.awt.event.ActionEvent;import java.awt.event.ActionListener;import java.awt.event.WindowAdapter;import java.awt.event.WindowEvent;import javax.swing.JOptionPane;
public class PlusFrame extends Frame {        private static final long serialVersionUID = 1L;        // 定义三个输入框        TextField num1, num2, num3;        // 定义一个=按钮        Button btnEquals;        // 定义+标签        Label lblPlus;        // 加法运算结果        Integer n3;
        // 构造函数,初始化界面上的组件以及响应界面上的X按钮        public PlusFrame() {                num1 = new TextField(10);                num1.setName("bbbbb");                num2 = new TextField(10);                num2.setName("ccccc");                num3 = new TextField(15);                num3.setName("ddddd");                btnEquals = new Button("=");                btnEquals.setName("equal");                btnEquals.addActionListener(new PlusListener());                lblPlus = new Label("+");
                // 响应界面上的X关闭按钮                addWindowListener(new WindowAdapter() {                        public void windowClosing(WindowEvent e) {                                System.exit(0);                        }                });
                //this.showFrame();        }
        // 显示整个界面        public void showFrame() {                setLayout(new FlowLayout());                add(num1);                add(lblPlus);                add(num2);                add(btnEquals);                add(num3);                pack();                setVisible(true);        }
        // 判断输入的数字是否合法        public boolean numIsRight(TextField num1, TextField num2) {                if (num1.getText().equals("") || num2.getText() == null) {                        JOptionPane.showMessageDialog(null, "输入的数字必须在0-10之间", "数字验证出错",                                        JOptionPane.ERROR_MESSAGE);                        return false;                }                int n1 = Integer.parseInt(num1.getText());                if (n1 > 10 || n1                         JOptionPane.showMessageDialog(null, "输入的数字必须在0-10之间", "数字验证出错",                                        JOptionPane.ERROR_MESSAGE);                        return false;                }                if (num2.getText().equals("") || num2.getText() == null) {                        JOptionPane.showMessageDialog(null, "输入的数字必须在0-10之间", "数字验证出错",                                        JOptionPane.ERROR_MESSAGE);                        return false;                }                int n2 = Integer.parseInt(num2.getText());                if (n2 > 10 || n2                         JOptionPane.showMessageDialog(null, "输入的数字必须在0-10之间", "数字验证出错",                                        JOptionPane.ERROR_MESSAGE);                        return false;                }                n3 = n1 + n2;                return true;        }
        // =按钮的响应动作        class PlusListener implements ActionListener {                public void actionPerformed(ActionEvent ev) {
                        if (!numIsRight(num1, num2)) {                                return;                        }                        num3.setText("" + n3);                }        }}

       
        文件:abbot-0.10.0.zip
        大小:3036KB
        下载:
下载
       
               
               
               
               

本文来自ChinaUnix博客,如果查看原文请点:http://blog.chinaunix.net/u2/76927/showart_2046713.html
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP