TestComplete教程:光學(xué)字符識(shí)別(六)等待文本顯示在屏幕上
TestComplete是一款具有人工智能的自動(dòng)UI測(cè)試工具,利用自動(dòng)化測(cè)試工具和人工智能支持的混合對(duì)象識(shí)別引擎,輕松檢測(cè)和測(cè)試每個(gè)桌面,Web和移動(dòng)應(yīng)用程序。本文描述了在光學(xué)字符識(shí)別教程中,如何使用光學(xué)字符識(shí)別來檢查您的測(cè)試應(yīng)用程序在屏幕上呈現(xiàn)的文本內(nèi)容第三部分——等待文本顯示在屏幕上。
測(cè)試應(yīng)用程序時(shí),您可能需要延遲測(cè)試運(yùn)行,直到屏幕上出現(xiàn)預(yù)期的文本:
1、使用OCR.Recognize.FullText屬性捕獲在屏幕區(qū)域中呈現(xiàn)的文本。
2、檢查捕獲的文本是否包含預(yù)期的文本片段。例如,您可以使用該aqString.Find方法或您認(rèn)為合適的任何其他字符串比較方法。
3、循環(huán)重復(fù)步驟1和2,直到出現(xiàn)預(yù)期的文本。
注:
循環(huán)中的文本識(shí)別和字符串比較可能會(huì)降低測(cè)試性能。為避免可能的問題,您可以在循環(huán)中添加延遲。
為避免無限循環(huán),您可以添加條件以更早退出循環(huán)。
在腳本中
下面的代碼包含CheckTextContents獲取屏幕上對(duì)象和字符串的例程,并驗(yàn)證對(duì)象的文本是否包含該字符串。例程將第三個(gè)參數(shù)用作布爾值,該布爾值指定檢查是區(qū)分大小寫還是不區(qū)分大小寫。為了獲取屏幕上對(duì)象的文本,例程使用OCR.Recognize.FullText屬性。為了驗(yàn)證文本是否包含字符串,例程使用aqString.Find方法。如果該字符串存在,則例程返回True。否則,它返回False。
在WaitForText下面調(diào)用示例代碼程序CheckTextContents的循環(huán),直到例行CheckTextContents程序返回true,也就是說,直到測(cè)試應(yīng)用程序呈現(xiàn)預(yù)期的文本:
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 … }
在關(guān)鍵字測(cè)試中
1、將CheckTextContents功能代碼從上面的示例復(fù)制到TestComplete中的測(cè)試項(xiàng)目中的腳本單元。
2、在關(guān)鍵字測(cè)試中,循環(huán)調(diào)用CheckTextContents函數(shù),直到該函數(shù)返回True。
要調(diào)用例程,可以使用Run Code Snippet或Run Script Routine操作。要在關(guān)鍵字測(cè)試中創(chuàng)建循環(huán),請(qǐng)使用While循環(huán)操作。
本文內(nèi)容到這里就完結(jié)了,敬請(qǐng)期待后續(xù)內(nèi)容“獲取沒有文本內(nèi)容的控件”,感興趣的朋友可以繼續(xù)關(guān)注我們哦~或者您下載TestComplete試用版進(jìn)行免費(fèi)評(píng)估~
相關(guān)內(nèi)容推薦:
TestComplete教程:光學(xué)字符識(shí)別(一)處理UI元素
TestComplete教程:光學(xué)字符識(shí)別(二)識(shí)別屏幕上文本須滿足的要求
TestComplete教程:光學(xué)字符識(shí)別(三)模擬用戶操作
TestComplete教程:光學(xué)字符識(shí)別(四)驗(yàn)證文字內(nèi)容
TestComplete教程:光學(xué)字符識(shí)別(五)高級(jí)文本內(nèi)容驗(yàn)證
想要購買TestComplete正版授權(quán),或了解更多產(chǎn)品信息請(qǐng)點(diǎn)擊“咨詢?cè)诰€客服”