使用 Java Access Bridge 实现自动化 [英] Automation using Java Access Bridge

查看:67
本文介绍了使用 Java Access Bridge 实现自动化的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我可以使用 Java Access Bridge 事件.我该怎么办:

I can capture text from UI controls (button/Editbox/Checkbox etc) in Java Applications, using Java Access Bridge events. How can I:

  1. 在编辑框中设置文本
  2. 点击按钮

使用 Java Access Bridge API 调用?

using Java Access Bridge API calls?

推荐答案

这是我为我的项目所做的.创建一个基类 API,将所有 PInvokes 调用到 JAB WindowsAccessBridge DLL 中.如果您使用的是 64 位操作系统,请确保您的目标是正确的 DLL 名称.使用 getAccessibleContextFromHWND 函数从 Windows 句柄获取 VmID 和上下文.通过枚举子项在 Java 窗口中定位文本框或按钮.找到您要查找的控件后,TextBox 或 Button 执行操作.

Here is how I have done it for my project. Create a base class API that calls all PInvokes into JAB WindowsAccessBridge DLL. Make sure you target proper DLL Name if you are on 64 bit os. Use getAccessibleContextFromHWND function to get VmID and Context from Windows Handle. Locate the textbox or button within the Java Window by enumerating children. Once you locate the Control you are looking for in you case TextBox or Button perform the action.

1) 设置文字

public string Text
{
    get 
    {
        return GetText();
    }
    set
    {
        if (!API.setTextContents(this.VmId, this.Context, value))
            throw new AccessibilityException("Error setting text");
    }
}

private string GetText()
{
    System.Text.StringBuilder sbText = new System.Text.StringBuilder();

    int caretIndex = 0;

    while (true)
    {
        API.AccessibleTextItemsInfo ti = new API.AccessibleTextItemsInfo();
        if (!API.getAccessibleTextItems(this.VmId, this.Context, ref ti, caretIndex))
            throw new AccessibilityException("Error getting accessible text item information");

        if (!string.IsNullOrEmpty(ti.sentence))
            sbText.Append(ti.sentence);
        else               
            break;

        caretIndex = sbText.Length;

    }

    return sbText.ToString().TrimEnd();
}

2) 点击按钮

public void Press()
{
    DoAction("click");
}

protected void DoAction(params string[] actions)
{
    API.AccessibleActionsToDo todo = new API.AccessibleActionsToDo()
    {
        actionInfo = new API.AccessibleActionInfo[API.MAX_ACTIONS_TO_DO],
        actionsCount = actions.Length,
    };

    for (int i = 0, n = Math.Min(actions.Length, API.MAX_ACTIONS_TO_DO); i < n; i++)
        todo.actionInfo[i].name = actions[i];

    Int32 failure = 0;
    if (!API.doAccessibleActions(this.VmId, this.Context, ref todo, ref failure))
        throw new AccessibilityException("Error performing action");
}

核心...

public API.AccessBridgeVersionInfo VersionInfo
{
    get { return GetVersionInfo(this.VmId); }
}

public API.AccessibleContextInfo Info
{
    get { return GetContextInfo(this.VmId, this.Context); }
}

public Int64 Context
{
    get;
    protected set;
}

public Int32 VmId
{
    get;
    protected set;
}

这篇关于使用 Java Access Bridge 实现自动化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆