【自動化__GUI自動化】__java__Windows應用程式識別__計算機

來源:互聯網
上載者:User

標籤: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應用程式識別__計算機

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.