【问题标题】:Automation using Java Access Bridge使用 Java Access Bridge 实现自动化
【发布时间】:2013-10-04 04:15:31
【问题描述】:

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

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

使用 Java Access Bridge API 调用?

【问题讨论】:

  • 您能演示一下如何使用 JAB 捕获文本吗?

标签: java c# swing accessibility java-access-bridge


【解决方案1】:

我会将您的 GUI 组件的 AccessibleContext 子类化,并为它提供一个 accessibleAction 对象。让 AccessibleContext.getAccessibleAction() 返回您的对象。

如果它不为空,它会为屏幕阅读器提供支持的操作列表,屏幕阅读器可以通过调用 doAction 来调用这些操作。

【讨论】:

    【解决方案2】:

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

    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;
    }
    

    【讨论】:

    • 这很好用。任何想法如何单击/双击树节点?
    猜你喜欢
    • 2017-10-16
    • 1970-01-01
    • 2017-02-05
    • 2015-10-27
    • 2018-11-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多