TestComplete教程:光学字符识别(六)等待文本显示在屏幕上
TestComplete是一款具有人工智能的自动UI测试工具,利用自动化测试工具和人工智能支持的混合对象识别引擎,轻松检测和测试每个桌面,Web和移动应用程序。本文描述了在光学字符识别教程中,如何使用光学字符识别来检查您的测试应用程序在屏幕上呈现的文本内容第三部分——等待文本显示在屏幕上。
测试应用程序时,您可能需要延迟测试运行,直到屏幕上出现预期的文本:
1、使用OCR.Recognize.FullText属性捕获在屏幕区域中呈现的文本。
2、检查捕获的文本是否包含预期的文本片段。例如,您可以使用该aqString.Find方法或您认为合适的任何其他字符串比较方法。
3、循环重复步骤1和2,直到出现预期的文本。
注:
循环中的文本识别和字符串比较可能会降低测试性能。为避免可能的问题,您可以在循环中添加延迟。
为避免无限循环,您可以添加条件以更早退出循环。
在脚本中
下面的代码包含CheckTextContents获取屏幕上对象和字符串的例程,并验证对象的文本是否包含该字符串。例程将第三个参数用作布尔值,该布尔值指定检查是区分大小写还是不区分大小写。为了获取屏幕上对象的文本,例程使用OCR.Recognize.FullText属性。为了验证文本是否包含字符串,例程使用aqString.Find方法。如果该字符串存在,则例程返回True。否则,它返回False。
在WaitForText下面调用示例代码程序CheckTextContents的循环,直到例行CheckTextContents程序返回true,也就是说,直到测试应用程序呈现预期的文本:
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 WaitForText() { var textToWait = "substring"; // Get the onscreen object whose text will be checked var obj = Sys.WaitProcess("MyApp").WaitWindow("Window", "*", -1, 3000); // Delay the test execution until the onscreen object text includes the expected substring while (! CheckTextContents(obj, textToWait, false)) Delay(3000); // The onscreen object contains the needed text // Simulate user actions … }
Python
def CheckTextContents(anObject, aSubstring, caseSensitive=False): # 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 return (aqString.Find(text, aSubstring, 0 , caseSensitive) > -1) def WaitForText(): textToWait = "substring" # Get the onscreen object whose text will be checked obj = Sys.WaitProcess("MyApp").WaitWindow("Window", "*", -1, 3000) # Delay the test execution until the onscreen object text includes the expected substring while not CheckTextContents(obj, textToWait, False): Delay(3000) # The onscreen object contains the needed text # Simulate user actions # ...
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 WaitForText textToWait = "substring" ' Get the onscreen object whose text will be checked Set obj = Sys.WaitProcess("MyApp").WaitWindow("WindowClass", "*", -1, 3000) ' Delay the test execution until the onscreen object text includes the expected substring While Not CheckTextContents(obj, textToWait, False) Delay(3000) Wend ' The onscreen object contains the needed text ' Simulate user actions … 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; procedure WaitForText(); var obj, textToWait; begin textToWait := 'substring'; // Get the onscreen object whose text will be checked obj := Sys.WaitProcess('MyApp').WaitWindow('Window', '*', -1, 3000); // Delay the test execution until the onscreen object text includes the expected substring while not CheckTextContents(obj, textToWait, false) do Delay(3000); // The onscreen object contains the needed text // Simulate user actions … 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 WaitForText() { var textToWait = "substring"; // Get the onscreen object whose text will be checked var obj = Sys["WaitProcess"]("MyApp")["WaitWindow"]("Window", "*", -1, 3000); // Delay the test execution until the onscreen object text includes the expected substring while (! CheckTextContents(obj, textToWait, false)) Delay(3000); // The onscreen object contains the needed text // Simulate user actions … }
在关键字测试中
1、将CheckTextContents功能代码从上面的示例复制到TestComplete中的测试项目中的脚本单元。
2、在关键字测试中,循环调用CheckTextContents函数,直到该函数返回True。
要调用例程,可以使用Run Code Snippet或Run Script Routine操作。要在关键字测试中创建循环,请使用While循环操作。
本文内容到这里就完结了,敬请期待后续内容“获取没有文本内容的控件”,感兴趣的朋友可以继续关注我们哦~或者您下载TestComplete试用版进行免费评估~
相关内容推荐:
TestComplete教程:光学字符识别(一)处理UI元素
TestComplete教程:光学字符识别(二)识别屏幕上文本须满足的要求
TestComplete教程:光学字符识别(三)模拟用户操作
TestComplete教程:光学字符识别(四)验证文字内容
TestComplete教程:光学字符识别(五)高级文本内容验证
想要购买TestComplete正版授权,或了解更多产品信息请点击