提问者:小点点

数字输入框UpDown控件AutomationID访问WinAppDriver


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

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

我使用检查器. exe来识别对象。检查器中的树:检查屏幕截图

因此,在自定义下是文本框的3个控件“编辑”,“按钮”,“按钮”使用“Root_0_Blue_AutomationId”我可以访问文本框,例如将某些内容写入框中。

但是,如果我检查特定文本框的向上按钮,它具有autoationID"Part_IncreaseButton"。其他文本框的向上控件具有相同的ID。只有rootID的AutomationID不同,向上控件的ID保持不变,例如:

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

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

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


共3个答案

匿名用户

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

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

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

如果您使用的是设计良好的应用程序,建议使用选项3。它允许您使用通用逻辑与任何上/下按钮进行交互,同时通过包含它们的输入框唯一识别它们。

匿名用户

非常感谢您的支持。

我通过以下方式做到了(2步):

首先找到父元素:

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

然后找到其他控件:

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

点击UpControl:

动作生成器=新动作(编辑器);

builder.Click(TB1UpControl)。执行();

最好的问候!

匿名用户

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

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;