標籤:time() content try roc tac boa message except while
一、代碼如下
package www.woniu.gui.one;import java.awt.AWTException;import java.awt.Robot;import java.awt.Toolkit;import java.awt.datatransfer.Clipboard;import java.awt.datatransfer.DataFlavor;import java.awt.datatransfer.Transferable;import java.awt.datatransfer.UnsupportedFlavorException;import java.awt.event.KeyEvent;import java.io.BufferedReader;import java.io.IOException;import java.io.InputStream;import java.io.InputStreamReader;import org.xvolks.jnative.JNative;import org.xvolks.jnative.Type;import org.xvolks.jnative.exceptions.NativeException;public class CalcJava {static int BM_CLICK= 0x00F5;static int WM_CLOSE= 0x0010;public static void main(String[] args) {// TODO Auto-generated method stubCalcJava cj= new CalcJava();cj.calTest();}private void calTest() {// TODO Auto-generated method stubCalcJava cj= new CalcJava();try {//Process process= runtime.exec("dir d:");//Process process= runtime.exec("ping www.woniuxy.com");//Process process= runtime.exec("ipconfig");//InputStream is= process.getInputStream();//InputStreamReader isr= new InputStreamReader(is, "GBK");//BufferedReader br= new BufferedReader(isr);//String line= "", body="";//while((line=br.readLine()) != null) {//body +=line;//body +=line + "\n";//}//System.out.println(body);//被測應用程式的啟動Runtime runtime= Runtime.getRuntime();Process process= runtime.exec("tasklist");//檢查被測應用程式是否啟動InputStreamReader isr= new InputStreamReader(process.getInputStream());BufferedReader br= new BufferedReader(isr);String line= "";boolean isOpen= false;while((line=br.readLine()) != null) {if(line.indexOf("calc.exe") >= 0) {isOpen= true;break;}}if(!isOpen) {runtime.exec("D:\\calcen.exe");//強子指定的計算機Thread.sleep(2000);}//int parentHandle= Thread.sleep(2000);int window= this.findWindow("sciCalc", "Calculator");System.out.println("應用程式編號為:"+window);System.out.println("=========");int control= 0;control= this.findWindowEx(window, "Button", "3");this.sendMessage(control, BM_CLICK);System.out.println(control);Thread.sleep(1000);control= this.findWindowEx(window, "Button", "+");this.sendMessage(control, BM_CLICK);Thread.sleep(1000);control= this.findWindowEx(window, "Button", "2");this.sendMessage(control, BM_CLICK);Thread.sleep(1000);control= this.findWindowEx(window, "Button", "=");this.sendMessage(control, BM_CLICK);Thread.sleep(1000);//將結果進行複製 Ctrl+CRobot robot= new Robot();robot.keyPress(KeyEvent.VK_CONTROL);robot.keyPress(KeyEvent.VK_C);robot.keyRelease(KeyEvent.VK_CONTROL);robot.keyRelease(KeyEvent.VK_C);Thread.sleep(2000);//從剪貼簿取出複製的值,同預期結果進行比較,斷言String content= "";Clipboard clip= Toolkit.getDefaultToolkit().getSystemClipboard();Transferable clipT= clip.getContents(null);if(clipT != null) {if(clipT.isDataFlavorSupported(DataFlavor.stringFlavor)) {content= (String) clipT.getTransferData(DataFlavor.stringFlavor);}}System.out.println(content);if(content.equals("5")) {System.out.println("測試成功");} else {System.out.println("測試失敗");}this.sendMessage(window, WM_CLOSE);} catch (IOException e) {// TODO Auto-generated catch block e.printStackTrace();} catch (InterruptedException e) {// TODO Auto-generated catch block InterruptedExceptione.printStackTrace();} catch (AWTException e) {// TODO Auto-generated catch block InterruptedException AWTException e.printStackTrace();} catch (UnsupportedFlavorException e) {// TODO Auto-generated catch block InterruptedException AWTException UnsupportedFlavorExceptione.printStackTrace();}}private int findWindowEx(int parentHandle, String controlClass, String windowCaption) {// TODO Auto-generated method stubint result= 0;try {JNative jnative= new JNative("user32.dll", "FindWindowExA");jnative.setRetVal(Type.INT);jnative.setParameter(0, Type.INT, String.valueOf(parentHandle));jnative.setParameter(1, Type.INT, "0");jnative.setParameter(2, Type.STRING, controlClass);jnative.setParameter(3, Type.STRING, windowCaption);jnative.invoke();result= Integer.parseInt(jnative.getRetVal());} catch (NativeException e) {// TODO Auto-generated catch block IllegalAccessExceptione.printStackTrace();}catch (IllegalAccessException e) {// TODO Auto-generated catch block IllegalAccessExceptione.printStackTrace();}return result;}private int findWindow(String windowClass, String windowCaption ) {// TODO Auto-generated method stubint result= 0;try {JNative jnative= new JNative("user32.dll", "FindWindowA");jnative.setRetVal(Type.INT);jnative.setParameter(0, Type.STRING, windowClass);jnative.setParameter(1, Type.STRING, windowCaption);jnative.invoke();result= Integer.parseInt(jnative.getRetVal());} catch (NativeException e) {// TODO Auto-generated catch block IllegalAccessExceptione.printStackTrace();}catch (IllegalAccessException e) {// TODO Auto-generated catch block IllegalAccessExceptione.printStackTrace();}return result;}private void sendMessage(int parentHandle, int message) {// TODO Auto-generated method stubtry {JNative jnative= new JNative("user32.dll", "SendMessageA");jnative.setRetVal(Type.VOID);jnative.setParameter(0, Type.INT, String.valueOf(parentHandle));jnative.setParameter(1, Type.INT, String.valueOf(message));jnative.setParameter(2, Type.STRING, "0");jnative.setParameter(3, Type.STRING, "0");jnative.invoke();Thread.sleep(2000);} catch (NativeException e) {// TODO Auto-generated catch block IllegalAccessException InterruptedExceptione.printStackTrace();} catch (IllegalAccessException e) {// TODO Auto-generated catch block IllegalAccessExceptione.printStackTrace();} catch (InterruptedException e) {// TODO Auto-generated catch block IllegalAccessExceptione.printStackTrace();}}}
【自動化__GUI自動化】__java__Windows應用程式識別__計算機