good example for web automation and exe automation

 

很多网友对于Windows应用程序以及Web应用程序的自动化操作颇感兴趣,在此我给出对应的两份代码,并且附上简要的说明。全文将使用C#.Net 2008来编写代码,对于偏爱VB.Net的用户请自行使用Reflector翻译。

 

一.Windows Application Automation

本例编码实现自动化操作Windows计算器

1.       启动计算器

2.       输入1+5×8=

3.       验证结果是否等于48 (此计算器不支持先乘除后加减)

代码实现

1.       启动Visual Studio 2008

2.       创建C# Console Application

3.       添加引用UIAutomationClient以及UIAutomationTypes

4.       编码

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

using System.Diagnostics;

using System.Threading;

using System.Windows.Automation;

 

namespace CalcManipulate

{

    class Program

    {

        static void Main(string[] args)

        {

            Process calc = Process.Start("calc");

            Console.WriteLine("Launch Windows Calculator");

            Thread.Sleep(2000);

 

            AutomationElement e = AutomationElement.FromHandle(calc.MainWindowHandle);

 

            e.FindFirst(TreeScope.Children, new PropertyCondition(AutomationElement.AutomationIdProperty, "125")).Invoke();

            Console.WriteLine("Click 1");

 

            e.FindFirst(TreeScope.Children, new PropertyCondition(AutomationElement.AutomationIdProperty, "92")).Invoke();

            Console.WriteLine("Click +");

 

            e.FindFirst(TreeScope.Children, new PropertyCondition(AutomationElement.AutomationIdProperty, "129")).Invoke();

            Console.WriteLine("Click 5");

 

            e.FindFirst(TreeScope.Children, new PropertyCondition(AutomationElement.AutomationIdProperty, "91")).Invoke();

            Console.WriteLine("Click *");

 

            e.FindFirst(TreeScope.Children, new PropertyCondition(AutomationElement.AutomationIdProperty, "132")).Invoke();

            Console.WriteLine("Click 8");

 

            e.FindFirst(TreeScope.Children, new PropertyCondition(AutomationElement.AutomationIdProperty, "112")).Invoke();

            Console.WriteLine("Click =");

 

            string result = e.FindFirst(TreeScope.Children, new PropertyCondition(AutomationElement.AutomationIdProperty, "403")).GetText();

            Console.WriteLine("Result is {0}", result);

 

            if(result == "48")

                Console.WriteLine("It is correct");

            else

                Console.WriteLine("It is NOT correct");

        }

    }

 

    static class UIAutomation

    {

        public static void Invoke(this AutomationElement e)

        {

            (e.GetCurrentPattern(InvokePatternIdentifiers.Pattern) as InvokePattern).Invoke();

        }

        public static string GetText(this AutomationElement e)

        {

            return (e.GetCurrentPattern(TextPatternIdentifiers.Pattern) as TextPattern).DocumentRange.GetText(-1);

        }

    }

}

                小结

1.       此例使用AutomationID属性来唯一确定待测程序中的控件位置,如按钮+AutomationID92。此属性可以通过Visual Studio来获得

 

 

二.Web Application Automation

我们以163邮件服务网站为例,实现自动登录

1.       启动IE

2.       输入http://mail.163.com

3.       输入用户名,密码

4.       点击登录

代码实现

1.       启动Visual Studio 2008

2.       创建C# Console Application,命名为WebMailManipulate

3.       添加引用Microsoft.mshtmlUIAutomationClient以及UIAutomationTypes

4.       编码

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

using System.Diagnostics;

using System.Threading;

using System.Runtime.InteropServices;

using System.ComponentModel;

using System.Windows.Automation;

 

using mshtml;

 

namespace WebMailManipulate

{

    class Program

    {

        static void Main(string[] args)

        {

            Process ie = Process.Start("iexplore", "http://mail.163.com");

            Console.WriteLine("Launch http://mail.163.com with PID {0}", ie.Id);

            Thread.Sleep(5000);

 

            // get ie main window handle

            AutomationElement e = AutomationElement.FromHandle(ie.MainWindowHandle);

 

            // get ie shell document object view handle

            e = e.FindFirst(TreeScope.Descendants, new PropertyCondition(AutomationElement.ClassNameProperty, "Internet Explorer_Server"));

 

            // get document interface from shell document object view handle

            HTMLDocumentClass document = UIAutomation.GetDomFromShellDocObjectView(e.Current.NativeWindowHandle);

 

            // set user name

            document.getElementsByName("username").Cast<IHTMLElement>().First().setAttribute("value", "stainboy", 0);

            Console.WriteLine("Set username to stainboy");

 

            // set password

            document.getElementsByName("password").Cast<IHTMLElement>().First().setAttribute("value", "111111", 0);

            Console.WriteLine("Set password to 111111");

 

            // click login button

            document.getElementsByName("登录邮箱").Cast<IHTMLElement>().First().click();

            Console.WriteLine("Click login button");

        }

    }

 

    static class UIAutomation

    {

        public static HTMLDocumentClass GetDomFromShellDocObjectView(int hWnd)

        {

            object domObj = new object();

            Guid riid = new Guid("626FC520-A41E-11CF-A731-00A0C9082637");

            string lpString = "WM_HTML_GETOBJECT";

            int wMsg = RegisterWindowMessageA(ref lpString);

            if (wMsg == 0)

            {

                throw new Exception("Unable to register windows message in getDOMFromHwnd()");

            }

            int lResutl = SendMessageA(hWnd, wMsg, 0, 0);

            if (ObjectFromLresult(

                lResutl,

                ref riid,

                0,

                out domObj) != 0)

            {

                throw new Win32Exception("Unable to get IE DOM from window message result in GetDomFromShellDocObjectView()");

            }

            return domObj as HTMLDocumentClass;

        }

 

        [DllImport("user32")]

        private static extern int RegisterWindowMessageA([MarshalAs(UnmanagedType.VBByRefStr)] ref string lpString);

        [DllImport("user32")]

        private static extern int SendMessageA(int hWnd, int wMsg, int wParam, int lParam);

        [DllImport("oleacc")]

        private static extern int ObjectFromLresult(int lResult, ref Guid riid, int wParem, [MarshalAs(UnmanagedType.Interface)] out object pAcc);

    }

}

                小结

1.       代码有很多注释,这里就不再赘述了,如有不明白,可以发邮件联系我。

 

 

附录

1.       代码:

2.       联系方式:stainboyx@hotmail.com

### 如何在 Automation Anywhere 中读取 Web 服务 为了实现与 Web 服务的交互,Automation Anywhere 提供了内置的功能来支持基于 XML 的开放标准协议。这使得客户端/应用程序能够通过诸如 WSDL(Web Services Description Language)、SOAP(Simple Object Access Protocol)和 UDDI(Universal Description Discovery and Integration)这样的技术建立完整的互操作性[^1]。 具体来说,在 Automation Anywhere 平台中读取 Web 服务通常涉及以下几个方面: #### 使用 Web Service Command 平台提供了专门用于调用 Web 服务的命令——`Web Service` 命令。此命令允许用户指定目标 URL 和请求方法(GET 或 POST),并处理返回的数据作为响应的一部分。 ```python # 创建一个新的 WebService Activity web_service_activity = aa.WebService() # 设置 Web 服务端点地址和服务动作 web_service_activity.set_endpoint("http://example.com/service") web_service_activity.set_action("GetCustomerDetails") # 执行 Web 服务调用并将结果存储在一个变量中 response_data = web_service_activity.execute() ``` #### 解析 SOAP 请求和响应 当涉及到 SOAP 协议时,可以利用 `XML Read` 和 `XML Write` 活动来进行消息体的构建与解析工作。这些活动帮助自动化流程创建符合特定模式的有效载荷,并从中提取所需的信息字段。 ```xml <!-- 示例:定义一个简单的 SOAP Envelope --> <soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:sam="http://sample.namespace"> <soapenv:Header/> <soapenv:Body> <!-- 这里放置具体的业务逻辑部分 --> </soapenv:Body> </soapenv:Envelope> ``` #### 利用 WSDL 文件描述接口细节 WSDL 文档详细说明了一个 Web 服务所提供的功能及其参数结构。借助于该文档中的信息,可以在设计阶段更加精确地配置相应的 Bot 行为以匹配远程 API 要求。 对于希望进一步探索这一主题的人士而言,深入研究官方文档和技术论坛上的案例分享将会非常有帮助。此外,掌握有关 RESTful APIs 的基础知识同样重要,因为现代应用环境中越来越多的服务采用了这种架构风格。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值