【问题标题】:Numeric text box UpDown control AutomationID access with WinAppDriver数字文本框 UpDown 控件使用 WinAppDriver 访问 AutomationID
【发布时间】:2020-02-25 22:43:04
【问题描述】:

我一直在测试一个桌面应用程序 (WPF)。使用中:C#、Appium、WinAppDriver。 一个菜单中有多个数字文本框。 我在这里遇到的问题是我无法访问此页面上特定文本框的 UpButton,因为所有 Up/Down 按钮都具有相同的 ID“PART_IncreaseButton”。

它是一个数字文本框,带有内置的上下控制。一个菜单中有几个。 textbox

我使用 inspect.exe 来识别对象。 检查器中的树: inspect screenshot

所以在自定义下是文本框“编辑”、“按钮”、“按钮”的 3 个控件 使用 “Root_0_Blue_AutomationId” 我可以访问文本框,例如在框中写一些东西。

但如果我检查特定文本框的向上按钮,它的自动化 ID “Part_IncreaseButton”。并且其他文本框的 upcontrols 具有相同的 ID。 只有rootID的AutomationID不同,upcontrols的ID保持不变,例如:

根 ID(文本框):"Root_0_Blue_AutomationId" UpControl ID:"Part_Increasebutton"

根 ID(第二个文本框,绿色通道不同): “Root_0_Green_AutomationId” UpControl ID:“Part_Increasebutton”

如何管理它以访问第二个文本框的 UpControl?

【问题讨论】:

  • 嗨@VanGoghh,欢迎来到 StackOverflow!请提供complete, minimal, reproducible example 您在代码中尝试过的内容、确切的错误消息(如果适用)以及您期望的结果!
  • 同意@IvanGarcíaTopete,在这里非常欢迎您,但如果您期望得到高质量的答案,您的问题将需要满足上面评论中已经提到的标准。但也许,我可以帮助您自己解决问题:-)。我可以从您的问题中得出向上/向下按钮始终包含在文本框中吗?在这种情况下,如果文本框有一个唯一的自动化 ID,那么您就有了向上/向下按钮的相对坐标。
  • 非常感谢您的欢迎,很抱歉没有提供更多信息。我会努力在未来变得更好:)。我已经编辑了我的问题,我希望现在更容易理解。谢谢
  • 我已经发布了我的答案。我看到你已经打败了我;-)。既然已经写好了,就决定跟大家分享一下。如果您决定使用我的代码,请随意添加反馈。

标签: c# selenium appium winappdriver


【解决方案1】:

页面上的多个元素完全有可能拥有相同的自动化 ID。

当这种情况发生时,可以做 3 件事来识别和定位单个元素。

  1. 重构应用程序,使每个元素都有不同的自动化 ID。
  2. 使用替代定位策略来唯一标识元素。
  3. 找到一个仅包含 1 个具有目标 ID 的元素的父元素,然后在其子元素中搜索您要定位的唯一元素。

如果您正在使用设计良好的应用,则建议使用选项 3。它允许您使用通用逻辑与任何向上/向下按钮进行交互,同时通过包含它们的文本框唯一地识别它们。

【讨论】:

    【解决方案2】:

    非常感谢您的支持。

    我是按以下方式完成的(2个步骤):

    先找到父元素:

    WindowsElement TB1 = Editor.FindElementByAccessibilityId("Root_0_Blue_AutomationId");

    然后找到其他控件:

    AppiumWebElement TB1UpControl = TB1.FindElementByAccessibilityId("PART_IncreaseButton");

    点击UpControl:

    Actions builder = new Actions(Editor);

    builder.Click(TB1UpControl).Perform();

    最好的问候!

    【讨论】:

      【解决方案3】:

      这是我的实现。为了简单起见,我没有添加错误处理。代码可以编译,但我还没有测试过。向上/向下按钮和文本框包含在一个对象中,因此它与您的其他代码很好地分开。

      using OpenQA.Selenium.Appium;
      using OpenQA.Selenium.Appium.Windows;
      using System.Collections.ObjectModel;
      using System.Linq;
      
      namespace Example
      {
          class SpinControl
          {
              public int Value {
                  get
                  {
                      //call _textBox here to get the value from your control.
                      return 0;
                  }
              }
      
          private readonly AppiumWebElement _increaseButton;
          private readonly AppiumWebElement _decreaseButton;
          private readonly AppiumWebElement _textBox;
      
          public SpinControl(string automationID, WindowsDriver<WindowsElement> Driver)
          {
              WindowsElement customControl = Driver.FindElementByAccessibilityId(automationID);
              ReadOnlyCollection<AppiumWebElement> customControlChildren = customControl.FindElementsByXPath(".//*");
      
              _increaseButton = customControlChildren.First(e => e.FindElementByAccessibilityId("Part_IncreaseButton").Id == "Part_IncreaseButton");
              _decreaseButton = customControlChildren.First(e => e.FindElementByAccessibilityId("Part_DecreaseButton").Id == "Part_DecreaseButton");
              _textBox = customControlChildren.First(e => e != _increaseButton && e != _decreaseButton);
          }
      
          public void Increase()
          {
              //call _increaseButton here
          }
      
          public void Decrease()
          {
              //call _decreaseButton here
          }
      
          public void Input(int number)
          {
              //call _textBox here
          }
      }
      

      }

      可以这样使用:

              SpinControl sp = new SpinControl("customControlAutomationID", Driver);
      
              sp.Increase();
              sp.Decrease();
              sp.Input(5);
              int value = sp.Value;
      

      【讨论】:

        猜你喜欢
        • 2010-10-24
        • 2013-05-08
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-08-31
        • 1970-01-01
        • 2014-02-19
        • 1970-01-01
        相关资源
        最近更新 更多