码迷,mamicode.com
首页 > 编程语言 > 详细

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

时间:2017-09-06 22:13:32      阅读:224      评论:0      收藏:0      [点我收藏+]

标签:java应用   user   doc   com   art   win   [1]   actual   thread   

 

 

一、代码如下

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;
	}
}

 

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

标签:java应用   user   doc   com   art   win   [1]   actual   thread   

原文地址:http://www.cnblogs.com/wujianbo123/p/7487032.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!