TestComplete教程:光学字符识别(六)等待文本显示在屏幕上

TestComplete试用版

测试应用程序时,您可能需要延迟测试运行,直到屏幕上出现预期的文本:

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 FunctionSub 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教程:光学字符识别(六)等待文本显示在屏幕上

相关内容推荐:

SmartBear2019专题资源>>>

TestComplete教程:光学字符识别(一)处理UI元素

TestComplete教程:光学字符识别(二)识别屏幕上文本须满足的要求

TestComplete教程:光学字符识别(三)模拟用户操作

TestComplete教程:光学字符识别(四)验证文字内容

TestComplete教程:光学字符识别(五)高级文本内容验证


想要购买TestComplete正版授权,或了解更多产品信息请点击“咨询在线客服”

标签:

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

上一篇 2019年10月11日
下一篇 2019年10月11日

相关推荐

发表回复

登录后才能评论
tml>