• 【自动化__GUI自动化】__java__案例__java应用程序计算器__执行测试的代码


    一、代码如下

    package com.woniuxy.auto;
    
    import java.awt.Component;
    import java.awt.Frame;
    import java.lang.reflect.Method;
    
    import javax.swing.JButton;
    import javax.swing.JComboBox;
    import javax.swing.JFrame;
    import javax.swing.JLabel;
    import javax.swing.JTextField;
    
    public class JavaCalcTest {
    
    	public static void main(String[] args) {
    		JavaCalcTest calcTest = new JavaCalcTest();
    		calcTest.startApp();
    		calcTest.doTest();
    	}
    	
    	public void doTest() {
    		JFrame jf = this.getFrame("JavaSwing演示程序");
    		JTextField numberx = (JTextField)this.getComponent(jf, "txtNumberX");
    		numberx.setText("100");
    		JTextField numbery = (JTextField)this.getComponent(jf, "txtNumberY");
    		numbery.setText("200");
    		
    		JComboBox type = (JComboBox)this.getComponent(jf, "calcType");
    		type.setSelectedItem("乘法");
    		JButton btnCalc = (JButton)this.getComponent(jf, "btnCalc");
    		btnCalc.doClick();
    
    		double expect = 20000;
    		JLabel result = (JLabel)this.getComponent(jf, "lblResult");
    		double actual = Double.parseDouble(result.getText());
    		if (actual == expect) {
    			System.out.println("乘法运算:成功.");
    		}
    		else {
    			System.out.println("乘法运算:失败.");
    		}
    	}
    	
    	public void startApp() {
    		try {
    //			Class<?> clazz = Class.forName("com.woniuxy.auto.JavaSwingCalc");
    //			Method main = clazz.getMethod("main", String[].class);
    //			main.invoke(clazz, new String[1]);
    			
    			JavaSwingCalc.main(new String[0]);
    			
    //			JavaSwingCalc calc = new JavaSwingCalc();
    //			calc.windowInit();
    //			calc.buttonInvoke();
    			
    //			String userDir = System.getProperty("user.dir");
    //			String jarFile = userDir + "\lib\JavaSwingCalc.jar";
    //			Runtime.getRuntime().exec("java -jar " + jarFile);
    	
    			Thread.sleep(5000);
    		} catch (Exception e) {
    			e.printStackTrace();
    		}
    	}
    	
    	public JFrame getFrame(String title) {
    		Frame[] frames = JFrame.getFrames();
    		JFrame jf = null;
    		for (int i=0; i<frames.length; i++) {
    			if (frames[i].getTitle().equals(title)) {
    				jf = (JFrame)frames[i];
    			}
    		}
    		return jf;
    	}
    
    	public Component getComponent(JFrame jf, String name) {
    		Component[] controls = jf.getContentPane().getComponents();
    		Component control = null;
    		for (int i=0; i<controls.length; i++) {
    			if (controls[i].getName() != null &&
    					controls[i].getName().equals(name)) {
    				control = controls[i];
    			}
    		}
    		return control;
    	}
    }
    
  • 相关阅读:
    Building Seam 2.0 Application with NetBeans 6.1
    Better Builds with Maven学习笔记
    NetBeans Globel Translation Team Tshirt!
    Participate in MySQLGlassFish Student Contest and Win $500
    NetBeans Globel Translation Team Tshirt!
    Better Builds with Maven学习笔记
    Building Seam 2.0 Application with NetBeans 6.1
    Maven2 的新特性
    Participate in MySQLGlassFish Student Contest and Win $500
    数据库设计及数据缓存
  • 原文地址:https://www.cnblogs.com/wujianbo123/p/7487032.html
Copyright © 2020-2023  润新知