TestComplete试用版
在测试中,您可能需要检查是否在屏幕上呈现了预期的文本,并根据检查结果执行各种操作(例如,模拟用户操作或将自定义消息发布到测试日志)。您可以通过以下方式执行此操作:
-
使用该OCR.Recognize方法在所需的屏幕区域中识别渲染的文本。
-
通过使用OCR.Recognize.FullText属性获取识别的文本。
-
检查获取的文本是否包含预期的子字符串。为此,您可以使用各种比较字符串值的操作。例如,aqString.Find方法。
-
根据检查结果,执行所需的操作。
注意:要根据模式验证文本内容并将成功/失败消息发布到测试日志,请使用OCR检查点。请参阅验证文本内容。
在脚本中
下面的代码包含CheckTextContents获取屏幕上对象和字符串的例程,并验证对象的文本是否包含该字符串。例程将第三个参数用作布尔值,该布尔值指定检查是区分大小写还是不区分大小写。为了获取屏幕上对象的文本,例程使用OCR.Recognize.FullText属性。为了验证渲染的文本是否包含字符串,例程使用该aqString.Find方法。如果存在期望的子字符串,则例程返回True;否则,它返回False。
JavaScript, JScript
function CheckTextContents(anObject, aSubstring, caseSensitive){ // Recognize the text contents of the specified onscreen object var text = OCR.Recognize(anObject).FullText; // Search for the occurrence of the specified substring in the recognized text return (aqString.Find(text, aSubstring, 0, caseSensitive) > -1)}function Main(){ var textToCheck = "substring"; // Get the onscreen object whose text will be checked var form = Sys.WaitProcess("MyApp").WaitWindow("Window", "*", -1, 3000); … // Check the text if (CheckTextContents(form, textToCheck, false)) Log.Message("The substring '" + textToCheck + "' has been found."); else Log.Warning("The substring " + textToCheck + " has not been found."); …}
Python
def CheckTextContents(anObject, aSubstring, caseSensitive=False): # Recognize the text contents of the specified onscreen object text = OCR.Recognize(anObject).FullText # Searches for the occurrence of the specified substring in the recognized text return (aqString.Find(text, aSubstring, 0 , caseSensitive) > -1)def Main(): textToCheck = "substring" # Get the onscreen object whose text will be checked form = Sys.WaitProcess("MyApp").WaitWindow("Window", "*", -1, 3000) if (form.Exists): # Check the text if (CheckTextContents(form, textToCheck, False)): Log.Message("The substring '" + aqConvert.VarToStr(textToCheck) + "' has been found.") else: Log.Warning("The " + aqConvert.VarToStr(textToCheck) + " substring was not found.")
VBScript
Function CheckTextContents(anObject, aSubstring, caseSensitive) ' Recognize the text contents of the specified onscreen object text = OCR.Recognize(anObject).FullText ' Search for the occurrence of the specified substring in the recognized text CheckTextContents = (aqString.Find(text, aSubstring, 0 , caseSensitive) > -1)End FunctionSub Main textToCheck = "substring" ' Get the onscreen object whose text will be checked Set form = Sys.WaitProcess("MyApp").WaitWindow("Window", "*", -1, 3000) … ' Check the text If CheckTextContents(form, textToCheck, false) Then Log.Message("The substring '" & textToCheck & "' has been found.") Else Log.Warning("The substring '" & textToCheck & "' has not been found.") End If …End Sub
DelphiScript
function CheckTextContents(anObject : OleVariant, aSubstring : String, caseSensitive : boolean = false);var text;begin // Recognize the text contents of the specified onscreen object text := OCR.Recognize(anObject).FullText; // Search for the occurrence of the specified substring in the recognized text result : = (aqString.Find(text, aSubstring, 0, caseSensitive) > -1);end;function Main();var textToCheck, form;begin textToCheck := 'substring'; // Get the onscreen object whose text will be checked form := Sys.WaitProcess('MyApp').WaitWindow('Window', '*', -1, 3000); … // Check the text if CheckTextContents(form, textToCheck, false) then Log.Message('The substring ''' + aqConvert.VarToStr(textToCheck) + ''' has been found.') else Log.Warning('The substring ''' + aqConvert.VarToStr(textToCheck) + ''' has not been found.'); …end;
C++Script, C#Script
function CheckTextContents(anObject, aSubstring, caseSensitive){ // Recognize the text contents of the specified onscreen object var text = OCR["Recognize"](anObject)["FullText"]; // Search for the occurrence of the specified substring in the recognized text return (aqString["Find"](text, aSubstring, 0, caseSensitive) > -1);}function Main(){ var textToCheck = "substring"; // Get the onscreen object whose text will be checked var form = Sys["WaitProcess"]("MyApp")["WaitWindow"]("Window", "*", -1, 3000); … // Check the text if (CheckTextContents(form, textToCheck, false)) Log["Message"]("The substring '" + textToCheck + "' has been found."); else Log["Warning"]("The substring '" + textToCheck + "' has not been found."); …}
上面示例中的Main例程显示了如何调用CheckTextContents例程以验证测试应用程序的主要形式是否呈现了预期的子字符串。根据结果,它将适当的消息发布到测试日志。
在关键字测试中
-
将CheckTextContents功能代码从上面的示例复制到TestComplete中的测试项目中的脚本单元。
-
从关键字测试调用此函数。为此,您可以使用“运行代码片段”或“运行脚本例程”操作。
-
获取函数结果。根据它们执行所需的操作。

相关内容推荐:
SmartBear2019专题资源>>>
TestComplete教程:光学字符识别(一)处理UI元素
TestComplete教程:光学字符识别(二)识别屏幕上文本须满足的要求
TestComplete教程:光学字符识别(三)模拟用户操作
TestComplete教程:光学字符识别(四)验证文字内容
想要购买TestComplete正版授权,或了解更多产品信息请点击“咨询在线客服”

标签:
声明:本站部分文章及图片源自用户投稿,如本站任何资料有侵权请您尽早请联系jinwei@zod.com.cn进行处理,非常感谢!