TestComplete教程:光学字符识别(五)高级文本内容验证
TestComplete是一款具有人工智能的自动UI测试工具,利用自动化测试工具和人工智能支持的混合对象识别引擎,轻松检测和测试每个桌面,Web和移动应用程序。使用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 Function
Sub 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 SubDelphiScript
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中的测试项目中的脚本单元。
从关键字测试调用此函数。为此,您可以使用“运行代码片段”或“运行脚本例程”操作。
获取函数结果。根据它们执行所需的操作。

本文内容到这里就完结了,敬请期待后续内容“等待文本显示在屏幕上”,感兴趣的朋友可以继续关注我们哦~或者您下载TestComplete试用版进行免费评估~
相关内容推荐:
TestComplete教程:光学字符识别(一)处理UI元素
TestComplete教程:光学字符识别(二)识别屏幕上文本须满足的要求
TestComplete教程:光学字符识别(三)模拟用户操作
TestComplete教程:光学字符识别(四)验证文字内容
想要购买TestComplete正版授权,或了解更多产品信息请点击


QQ交谈
在线咨询

渝公网安备
50010702500608号

客服热线