QTP 自带了一个spellcheck 的VBS,如何用它实现对web page的拼写检查呢?

发布时间:2024-04-29 07:12 发布:上海旅游网

问题描述:

请高手结合这个脚本,讲解以下。

Function NumberOfSpellErrors(strText)
Dim objMsWord
Set objMsWord = CreateObject("Word.Application")
objMsWord.WordBasic.FileNew
objMsWord.WordBasic.Insert strText
NumberOfSpellErrors = objMsWord.ActiveDocument.SpellingErrors.Count
objMsWord.Documents.Close (False)
objMsWord.Quit ' close the application
Set objMsWord = Nothing' Clear object memory
End Function

' The following function uses the Spell errors function to check a specific property
' of all the objects with a given description which are under a given Parent

Sub CheckAllObjects(ParentObj, ObjDesc, PropName)
Dim ObjCol, idx, PropValue, OldReportMode
OldReportMode = Reporter.Filter
Reporter.Filter = 2 ' report only errors
If (IsNull(ParentObj)) Then
Set ObjCol = Desktop.ChildObjects(ObjDesc)
Else
Set ObjCol = ParentObj.ChildObjects(ObjDesc)
End If

For idx=0 to ObjCol.count-1
PropValue = ObjCol.Item(idx).GetROProperty(PropName)
RetVal = NumberOfSpellErrors(PropValue) ' the actual spell check
If (RetVal > 0) Then
ReportText = "Object #" & idx+1 & ": The '" & PropName & "' Property has " & RetVal & " spell errors (" & PropValue & ")"
Reporter.ReportEvent 1, "Spell Check", ReportText
End If
Next
Reporter.Filter = OldReportMode
End Sub

'''''''''''''''''''''''''''''''''''''
' An example of usage:
' Go over all the Static objects in the login window of the flight application
' and for each object verify the text for Spelling and Grammatical errors
'''''''''''''''''''''''''''''''''''''

' Go over all the links in the page and report all the ones that fails the Spell check
Set Desc = Description.Create()
Desc("nativeclass").Value = "Static"

Set Obj = Dialog("nativeclass:=#32770", "text:=Login")

' Invoke the Flight Application before calling the function
CheckAllObjects Obj, Desc, "text"

问题解答:

语法检查使用的是word的语法检查功能,
此脚本运行的前提条件就是安装word。

Function NumberOfSpellErrors(strText)
Dim objMsWord
'创建word
Set objMsWord = CreateObject("Word.Application")
'定义一个新的word文档
objMsWord.WordBasic.FileNew
'文档中插入要检查的文本
objMsWord.WordBasic.Insert strText
’语法错误的个数
NumberOfSpellErrors = objMsWord.ActiveDocument.SpellingErrors.Count
'关闭新建的word文档 不保存
objMsWord.Documents.Close (False)
’推出wordobjMsWord.Quit
' close the application
Set objMsWord = Nothing' Clear object memory
End Function

' The following function uses the Spell errors function to check a specific property
' of all the objects with a given description which are under a given Parent

Sub CheckAllObjects(ParentObj, ObjDesc, PropName)
Dim ObjCol, idx, PropValue, OldReportMode
OldReportMode = Reporter.Filter
'只在report中显示错误的操作
Reporter.Filter = 2 ' report only errors
If (IsNull(ParentObj)) Then
'获取要检查的对象
Set ObjCol = Desktop.ChildObjects(ObjDesc)
Else
Set ObjCol = ParentObj.ChildObjects(ObjDesc)
End If
’遍历要检查的对象
For idx=0 to ObjCol.count-1
PropValue = ObjCol.Item(idx).GetROProperty(PropName)
RetVal = NumberOfSpellErrors(PropValue) ' the actual spell check
If (RetVal > 0) Then
ReportText = "Object #" & idx+1 & ": The '" & PropName & "' Property has " & RetVal & " spell errors (" & PropValue & ")"
'把错误显示在report中
Reporter.ReportEvent 1, "Spell Check", ReportText
End If
Next
Reporter.Filter = OldReportMode
End Sub

热点新闻