"Automation __gui Automation" __java__windows Application Recognition __ Calculator

Source: Internet
Author: User
Tags readline

One, the code is as follows
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.exe C ("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);//The Startup runtime of the application being tested Runtime= runtime.getruntime (); Process process= runtime.exec ("tasklist");//Check if the application under test starts 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"),///hadron-Specified calculator thread.sleep (2000);} int parenthandle= Thread.Sleep (+), int window= This.findwindow ("Scicalc", "Calculator"); SYSTEM.OUT.PRINTLN ("Application number:" +window); System.out.println ("========="), int control= 0;control= This.findwindowex (window, "button", "3"), This.sendmessage ( control, Bm_click); System.out.println (Control); Thread.Sleep (control=);FindWindowEx (Window, "button", "+"); This.sendmessage (control, Bm_click); Thread.Sleep, control= This.findwindowex (window, "button", "2"), This.sendmessage (Control, Bm_click); Thread.Sleep, control= This.findwindowex (window, "button", "="); This.sendmessage (control, Bm_click); Thread.Sleep (1000);//Copy the results 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);//Remove the copied value from the Clipboard, compare it with the expected result, and assert the 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 ("Test succeeded");} else {System.out.println ("Test Failed" );} 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 (Awte Xception e) {//TODO auto-generated catch block interruptedexception awtexception e.printstacktrace ()} catch (Unsupporte Dflavorexception e) {//TODO auto-generated catch block interruptedexception awtexception Unsupportedflavorexceptione.printstacktrace ();}} private int FindWindowEx (int parenthandle, string controlclass, String windowcaption) {//TODO auto-generated method Stubi NT 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 {JN ative 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 Jnati ve ("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 ();}}}

"Automation __gui Automation" __java__windows Application Recognition __ Calculator

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.