gpt4 book ai didi

c# - 如何使用编码的 UI 测试通过自定义 HTML 属性搜索元素

转载 作者:太空狗 更新时间:2023-10-30 00:23:58 27 4
gpt4 key购买 nike

我在 GridView 中有一堆 input[type=submit] 按钮。这些按钮的 idname 虽然可以预测,因为它们使用索引号,但不太适合使用 SpecFlow 和编码的 UI 测试进行自动化。我发现很难搜索这些按钮元素。

传送到浏览器的 HTML 片段:

<input type="submit" id="abc_xyz_0" data-task-id="123" value="Apply">
<input type="submit" id="qrs_tuv_0" data-task-id="345" value="Renew">

按钮文本在每一行中都是通用的(“Apply”和“Renew”),但是 data-task-id 属性是唯一的。我想使用此属性和值来标识要单击的按钮。我正在尝试使用 SearchPropertiesFilterProperties 但我不断收到异常:

System.NotSupportedException: The property DataTaskId is not supported for this control.

我是如何尝试找到控件的:

HtmlInputButton button = new HtmlInputButton(document);

button.SearchProperties["data-task-id"] = "123";
// or button.SearchProperties["DataTaskId"] = "123";

一些额外的细节:

  • Windows 7
  • Internet Explorer 11
  • localhost 传送并在 Internet 选项安全设置中标记为“受信任”的网站

更新:感谢 marcel de vries 和 AfroMogli 的回答。 Marcel 使用 JavaScript 来查找元素,而 AfroMogli 使用纯 C# 和 CodedUI API。这两个答案同样有效,我没有注意到任何一个之间的性能差异。两个同样好的解决方案。

最佳答案

您可以通过将 ControlDefinition 作为属性名进行搜索来完成此操作。以下代码行对我有用:

HtmlInputButton button = new HtmlInputButton(document);
button.SearchProperties.Add(new PropertyExpression(HtmlControl.PropertyNames.ControlDefinition, "data-task-id=\"123\"", PropertyExpressionOperator.Contains));

允许您传入任何属性名称和值的示例方法:

public HtmlInputButton InputButton(string attributeName, string attributeValue, UITestControl container = null)
{
string controlDefinition = string.Format("{0}=\"{1}\"", attributeName, attributeValue);
HtmlInputButton button = new HtmlInputButton(container ?? document);
button.SearchProperties.Add(new PropertyExpression(HtmlControl.PropertyNames.ControlDefinition, controlDefinition, PropertyExpressionOperator.Contains));

return button;
}

编辑:

或者如果属性名称没有改变,请执行以下操作?

public PropertyExpression GetByTaskId(string value) 
{
return new PropertyExpression(
HtmlControl.PropertyNames.ControlDefinition,
$"data-task-id=\"{value}\"",
PropertyExpressionOperator.Contains
);
}

HtmlInputButton button = new HtmlInputButton(document);
button.SearchProperties.Add(GetByTaskId("123"));

关于c# - 如何使用编码的 UI 测试通过自定义 HTML 属性搜索元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31733910/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com