首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >数字文本框UpDown用WinAppDriver控制AutomationID访问

数字文本框UpDown用WinAppDriver控制AutomationID访问
EN

Stack Overflow用户
提问于 2019-10-30 15:22:40
回答 3查看 750关注 0票数 0

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

它是一个内置向下控件的数字文本框。一份菜单里有几份。文本框

我使用inspect.exe来标识对象。检查器中的树:检查截图

因此,在自定义下,textbox的3个控件“编辑”、“按钮”、“按钮”和“"Root__Blue_AutomationId"”,我可以访问文本框,例如,在文本框中写入一些东西。

但是,如果我检查特定文本框的up按钮,它就有automationID automationID。并且其他文本框的上控件具有相同的ID。例如,只有AutomationID的rootID不同,并且is控件的ID保持不变:

根ID (文本框的):"Root__Blue_AutomationId" UpControl ID:"Part_Increasebutton"

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

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

EN

回答 3

Stack Overflow用户

发布于 2019-10-31 17:00:59

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

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

  1. 重构应用程序,使每个元素都有一个不同的自动化id。
  2. 使用另一种位置策略来唯一地标识元素。
  3. 找到只包含一个带有目标ID的元素的父元素,然后搜索它的子元素,以查找要定位的唯一元素。

如果您正在使用设计良好的应用程序,那么推荐的方法是选项3。它允许您使用通用逻辑与任何上/下按钮交互,同时通过包含它们的文本框唯一地标识它们。

票数 1
EN

Stack Overflow用户

发布于 2019-11-05 12:31:29

非常感谢你的支持。

我这样做的方式如下(两个步骤):

首先查找父元素:

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

然后找到其他控件:

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

点击UpControl:

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

builder.Click(TB1UpControl).Perform();

诚挚的问候!

票数 1
EN

Stack Overflow用户

发布于 2019-11-05 14:01:54

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

代码语言:javascript
复制
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
    }
}

}

可以这样使用:

代码语言:javascript
复制
        SpinControl sp = new SpinControl("customControlAutomationID", Driver);

        sp.Increase();
        sp.Decrease();
        sp.Input(5);
        int value = sp.Value;
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/58628662

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档