如何将一行代码作为字符串传递而不用双引号替换每个引号? (QTP 中的 VBScript)
我正在尝试创建一个 VBScript 函数(在 QuickTest Pro 10 的上下文中),该函数能够接受一行代码作为参数,例如: JavaWindow("Export Control NOTICE").JavaButton("OK ").Click
问题是,当我尝试将该行作为字符串传递时,VBScript 会因引号而卡住,这是可以理解的。问题是:我有很多行与上面的代码类似的代码,所以我想获取该行代码并按原样传递它。我不想将这些代码行的引号加倍,更不用说 QTP 记录的每个新行了。 (查找/替换全部很容易失控)
我怎样才能通过上面的代码行? 如果将其转换为字符串是可行的方法,那么我该如何对该行进行编码,以便 VBscript 不会因引号而阻塞?
此外,我还没有找到任何方法将分隔符更改为引号以外的其他内容,以将代码行指定为字符串。找到一种更改分隔符的方法可能会解决这个问题。
或者,我也尝试将 JavaWindow("Export Control NOTICE").JavaButton("OK")
作为对象传递到函数中,并且效果良好。不幸的是,似乎没有办法将该对象转回字符串,以便将 ".Click"
(或存储为字符串的其他操作)重新附加到其末尾。我是否忽略了什么?
I am attempting to create a VBScript function (within the context of QuickTest Pro 10) that is able to take in a line of code as a parameter, such as: JavaWindow("Export Control Notice").JavaButton("OK").Click
Problem is, when I try passing that line as a string, VBScript understandably chokes on the quotes. Here is the catch: I have many lines of code similar to the one above, so I would like to take the line of code and pass it as is. I don't want to go around doubling the quotes for these lines of code, let along for every new line that QTP records. (Find/Replace All can easily go out of control)
How can I pass the above line of code?
If turning it into a string is the way to go, how can I encode that line so VBscript doesn't choke on the quote marks?
Additionally, I haven't been able to find any way to change the delimiter to something other than quote marks to assign the line of code as a string. Finding a way to change the delimiter would probably solve this issue.
Alternately, I've also tried passing JavaWindow("Export Control Notice").JavaButton("OK")
as an object into the function and that has worked. Unfortunately, there doesn't seem to be a way to turn that object back into a string in order to append ".Click"
(or some other action stored as a string) back onto the end of it. Am I overlooking something?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
根据您“拥有”这些代码行的具体方式(在您的头脑中、在文本文档中……)以及您是否喜欢键入或编程,您可以
使用正确的转义将它们放入 VBScript 代码中( “”):
使用您选择的分隔符和 Replace() 将它们放入 VBScript 代码中:
将它们放入外部文件(.txt、.xls、.xml、...)(resp.使用给定的文档);加载文件并将其解析为合适的数据结构(数组、字典等)。使用该集合来满足您的功能。
Depending on how exactly you 'have' those lines of code (in your head, in a text document, ...) and whether you prefer to type or to program you can
put them into VBScript code using the proper escape (""):
put them into VBScript code using a delimiter of your choice and Replace():
put them in an external file (.txt, .xls, .xml, ...) (resp. use the given document); load and parse the file into a suitable data structure (array, dictionary, ...). Use that collection to feed your function.
我们最终传入 QTP 对象的方式&将操作添加到函数中的目的是将 QTP 对象保留为对象,然后将操作作为字符串传递给 Select Case 进行排序。我们没有那么多操作,因此选择案例在这种情况下效果很好。
例如,以下是执行操作的代码:
Before:
After:
我们现在的操作仅占用一行,这对代码的可读性有很大帮助,但也使我们能够在一处改进错误检查/报告功能。
这是一些错误检查功能,减去报告部分。
功能:
...等等。
The way we eventually passed in QTP objects & actions into the function was to keep the QTP object as an object, then pass in the action as a string that a Select Case would sort through. We don't have that many actions, so a Select Case works well in this situation.
For example, here is the code for performing an action:
Before:
After:
We are now have the action occupying only one line, which helps immensely for code readability, but also allows us to improve the error-checking / reporting function all in one place.
Here is the some of the error-checking function, minus the reporting portion.
The Function:
... and so on.