在testcomplete中调用jquery时VBscript失败

时间:2013-04-18 12:15:18

标签: jquery vbscript testcomplete

这发生在testcomplete版本9.20中,并针对firefox 19.0.2进行测试。

第一个脚本文件名为test,它包含以下行:

'USEUNIT CommonFunctions
Public Function test()
  Call expandTree()
End Function

另一个名为CommonFunctions的脚本文件具有以下功能:

Public Function expandTree()
  Set foo = Aliases.tree.contentDocument.Script.jQuery("li[data-nodeid='sites'] a.openClose").click()
End Function

当我运行脚本时,自动化文件会出现以下错误:

Microsoft VBscript runtime error.

Object doesn't support this property or method:"contentDocument.Script.jQuery(...).Click''

Error location:
Unit:"ContentSuite\Content\Script\CommonFunctions"
Line:3972 Coloumn2

如果我将jquery放在同一个文件中,则不会发生同样的错误。也就是说,如果我运行它,它将正常工作,点击工作正常:

Public Function test()
  Set foo = Aliases.tree.contentDocument.Script.jQuery("li[data-nodeid='sites'] a.openClose").click()
End Function

2 个答案:

答案 0 :(得分:0)

首先,您当前的代码使用DOM click方法,该方法没有返回值,因此您需要删除Set foo =

错误可能是jQuery选择器没有找到任何匹配的对象。请尝试检查length函数结果的jQuery属性:

Set links = Aliases.tree.contentDocument.Script.jQuery("li[data-nodeid='sites'] a.openClose")
If links.length > 0 Then
  links.click
Else
  Log.Error "Object not found."
End If

但实际上这里不需要使用jQuery,因为TestComplete有内置的QuerySelector方法:

Set obj = Aliases.tree.QuerySelector("li[data-nodeid='sites'] a.openClose")
If Not obj Is Nothing Then
  obj.Click
Else
  Log.Error "Object not found."
End If

答案 1 :(得分:0)

我认为问题可能与您尝试调用 jQuery 方法返回的对象的单击方法有关。由于此方法返回一个集合,因此请在单击之前尝试获取特定对象:

Public Function expandTree()
  Aliases.tree.contentDocument.Script.jQuery("li[data-nodeid='sites'] a.openClose").get(0).click()
End Function