我目前正在为网络客户端开发测试自动化策略。在此 Web 客户端上,所有 Web 元素都有一个用于标识对象的 data-testid。 不幸的是,在某些情况下,数据测试ID不是唯一的,因此不可能清楚地识别对象,我无法更改这些数据测试ID。 因此,我尝试编写一个组件,它根据 data-testid 标识一个 web 元素,然后按下该元素的子元素按钮。 不幸的是我的代码不起作用,因为UFT无法识别我的对象,不幸的是我无法自己找到解决方案。 该元素的 HTML 如下所示:
我想查找数据测试 ID 为“Orderbeleg_Segmentumbuchung”的 Web 元素,然后单击其数据测试 ID 为“add:b”的子元素。
这是我写的代码:
Option Explicit
Dim bcName
bcName = "Webbuttonclick_add"
'---------------------------------------------------------------------------------------------
Dim pDoit, pParent, var, debug1
pParent = Parameter("parent")
pDoit = CBool(evalVarAndOffset(Parameter("doit")))
var = "data-testid:= .*" & pParent &".*"
If pDoit = True Then
Browser("index:=0").Page("index:=0").WebElement(var).Click
If Browser("index:=0").Page("index:=0").WebElement(var).exist(1) Then
Browser("index:=0").Page("index:=0").WebElement(var).ChildItem("data-testid:=
add:b").Click
wait 1
End If
End If
SQS_ActionFinish
var 是父元素的 data-testid。
最佳答案
通过阅读这个问题,我看到了一些事情(我实际上并没有尝试运行代码,所以请检查我是否正确)。
- 您正在使用
data-testid
作为测试对象属性。我认为这不起作用,因为 UFT 没有定义此属性。尝试attribute/data-testid
(further reading)。 - 您不需要使用
ChildItem
,您可以将WebElement
直接嵌套在var
元素下。您可以使用WebElement
as a container即使 UFT 默认情况下不这样做。事实上ChildItem
是Table
特定方法,如果您的意思是ChildObjects
,则不适用于此处。那么参数应该是Description
对象,而不是内联描述的字符串。 - 在
var
中,第一个.*
之前有一个空格,这是故意的吗?
要总结我的评论,请尝试以下操作:
Browser("index:=0").Page("index:=0").WebElement("属性/data-testid:=.*"&pParent&".*").WebElement("属性/data-testid:=add:b").点击
关于automated-tests - 使用 Microsotf UFT 单击 Web 元素的子级 Web 按钮,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/73373127/