有没有更快的方法来从UI自动化导航返回结果? [英] Is there a faster way to return results from UI Automation navigation?

查看:58
本文介绍了有没有更快的方法来从UI自动化导航返回结果?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,

关于UI自动化,我是一个新手,所以我希望这个问题不太明显.我开发了一个简单的类库,该类库获取三个参数(元素所在的窗口,具有元素和元素本身的后代).然后 根据元素的boundingRectangle属性计算坐标,并返回这些结果.我的问题是此过程花费的时间太长(取决于树的复杂性).到目前为止,我一直在使用FindFirst方法来查找那些 并将Treescope设置为后代". (这是一段获取该元素所在的桌面"窗口的代码):

I am a newbie when it comes to UI Automation so I hope that this question isn't too obvious. I developed a simple class library that gets three arguments (window where the element is located, descendant that has the element and the element itself). It then calculates coordinates based on the element's boundingRectangle property and returns those results. My problem is that this process takes too long (depending on the complexity of the tree). So far, I have been using the FindFirst method in order to find those elements and set the Treescope as "decendants" (here's a piece of the code that gets the Desktop window where the element is located in):

 

 

private void GetDesktopWindow()
     {
      if (windowName == "" || descendantName == "" || elementName == "")
      {
        Console.WriteLine("All elements must have values");
      }
      else
      {
        Condition cond = new PropertyCondition(AutomationElement.NameProperty, windowName);
        desktopWinResult = AutomationElement.RootElement.FindFirst(TreeScope.Children, cond);

        if (desktopWinResult != null)
        {
          GetDescendants();
        }
        else
        {
          Console.WriteLine("There is no window by the name of " + windowName);
        }
      }
    }

这是获取元素所在子代的另一部分(获取元素的编码方式完全相同:

Here is another part that gets the descendant where the element is located in (getting the element is coded exactly the same way:

    private void GetDescendants()
    {
      Condition cond = new PropertyCondition(AutomationElement.NameProperty, descendantName);
      descendantResult = desktopWinResult.FindFirst(TreeScope.Descendants, cond);
      
      if (descendantResult != null)
      {
        GetElements();
      }
      else
      {
        Console.WriteLine("There is no descendant by the name of " + descendantName);
      }
    }

 

最后是计算坐标的代码:

And finally the code that calculates the coordinates:

    private void returnCoords() //Returns the coordinates by using the boundingrectange property
    {
      try
      {
        top = elementResult.Current.BoundingRectangle.Top;
        bottom = elementResult.Current.BoundingRectangle.Bottom;
        left = elementResult.Current.BoundingRectangle.Left;
        right = elementResult.Current.BoundingRectangle.Right;
        y = Convert.ToInt32((top + bottom) / 2);
        x = Convert.ToInt32((left + right) / 2);
        Console.WriteLine("X:" + x.ToString() + "\n" + "Y:" + y.ToString());

      }
      catch(Exception e)
      {
        Console.WriteLine(e.Message);
      }
    }

就像我之前说过的那样,获取后代或元素所需的时间取决于树的复杂程度.我只是想知道是否有一种方法可以使过程更快地进行,从而使用户不必等待太久.任何建议都很大 赞赏.

Like I said before, the time it takes to get the descendants or element is dependent on how complex the tree is. I was just wondering if there was a way to make the process move faster so that the user wouldn't have to wait for too long. Any advice is greatly appreciated.

 

推荐答案

阿莫尔,

这里有三个建议:

1.从RootElement开始,范围为Descendants的FindFirst将搜索整个世界,从根到最低按钮.如果您可以使用子级范围和逐级降序来构造搜索,则通常可以 获得更好的结果.我见过的许多测试自动化都使用这种技术.您的第一个功能似乎使用了此功能,但第二个功能没有使用.

1. FindFirst with a scope of Descendants, starting from RootElement, is going to search the entire world, from the root to the lowliest button.  If you can structure the searches using Children scope and descending level by level, you can generally get better results.  Much Test Automation that I have seen uses this technique.  Your first function seems to use this, but not your second.

2. elementResult.CurrentBoundingRectangle是用于检索当前矩形的函数调用(并且可能是跨进程调用),您执行了四次.尝试执行一次,将矩形存储为局部矩形,然后获取上/下/左/右".

2. elementResult.CurrentBoundingRectangle is a function call to retrieve the current rectangle (and probably a cross-process call), and you do it four times.  Try doing it once, storing the rectangle as a local, and then getting Top/Bottom/Left/Right.

3.尝试使用Win7中引入的用于UI自动化的COM API.在某些情况下速度更快,这可能是其中一种.

3. Try using the COM API for UI Automation, introduced in Win7.  It is faster for certain scenarios, and this may be one of them.

谢谢,
迈克尔

Thanks,
Michael

 


这篇关于有没有更快的方法来从UI自动化导航返回结果?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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