使用 Java Access Bridge 实现自动化

sta*_*ney 3 c# java swing accessibility java-access-bridge

我可以使用Java Access Bridge事件从 Java 应用程序中的 UI 控件(按钮/编辑框/复选框等)捕获文本。我怎样才能:

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

使用 Java Access Bridge API 调用?

02A*_*ant 8

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

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();
}
Run Code Online (Sandbox Code Playgroud)

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");
}
Run Code Online (Sandbox Code Playgroud)

核...

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;
}
Run Code Online (Sandbox Code Playgroud)