TestComplete教程:光學(xué)字符識(shí)別(五)高級(jí)文本內(nèi)容驗(yàn)證
TestComplete是一款具有人工智能的自動(dòng)UI測(cè)試工具,利用自動(dòng)化測(cè)試工具和人工智能支持的混合對(duì)象識(shí)別引擎,輕松檢測(cè)和測(cè)試每個(gè)桌面,Web和移動(dòng)應(yīng)用程序。使用TestComplete,可以提高測(cè)試覆蓋率并幫助提供經(jīng)過實(shí)戰(zhàn)考驗(yàn)的高質(zhì)量軟件。本文描述了在光學(xué)字符識(shí)別教程中,如何使用光學(xué)字符識(shí)別來檢查您的測(cè)試應(yīng)用程序在屏幕上呈現(xiàn)的文本內(nèi)容第二部分——高級(jí)文本內(nèi)容驗(yàn)證。
在測(cè)試中,您可能需要檢查是否在屏幕上呈現(xiàn)了預(yù)期的文本,并根據(jù)檢查結(jié)果執(zhí)行各種操作(例如,模擬用戶操作或?qū)⒆远x消息發(fā)布到測(cè)試日志)。您可以通過以下方式執(zhí)行此操作:
使用該OCR.Recognize方法在所需的屏幕區(qū)域中識(shí)別渲染的文本。
通過使用OCR.Recognize.FullText屬性獲取識(shí)別的文本。
檢查獲取的文本是否包含預(yù)期的子字符串。為此,您可以使用各種比較字符串值的操作。例如,aqString.Find方法。
根據(jù)檢查結(jié)果,執(zhí)行所需的操作。
注意:要根據(jù)模式驗(yàn)證文本內(nèi)容并將成功/失敗消息發(fā)布到測(cè)試日志,請(qǐng)使用OCR檢查點(diǎn)。請(qǐng)參閱驗(yàn)證文本內(nèi)容。
在腳本中
下面的代碼包含CheckTextContents獲取屏幕上對(duì)象和字符串的例程,并驗(yàn)證對(duì)象的文本是否包含該字符串。例程將第三個(gè)參數(shù)用作布爾值,該布爾值指定檢查是區(qū)分大小寫還是不區(qū)分大小寫。為了獲取屏幕上對(duì)象的文本,例程使用OCR.Recognize.FullText屬性。為了驗(yàn)證渲染的文本是否包含字符串,例程使用該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 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例程顯示了如何調(diào)用CheckTextContents例程以驗(yàn)證測(cè)試應(yīng)用程序的主要形式是否呈現(xiàn)了預(yù)期的子字符串。根據(jù)結(jié)果,它將適當(dāng)?shù)南l(fā)布到測(cè)試日志。
在關(guān)鍵字測(cè)試中
將CheckTextContents功能代碼從上面的示例復(fù)制到TestComplete中的測(cè)試項(xiàng)目中的腳本單元。
從關(guān)鍵字測(cè)試調(diào)用此函數(shù)。為此,您可以使用“運(yùn)行代碼片段”或“運(yùn)行腳本例程”操作。
獲取函數(shù)結(jié)果。根據(jù)它們執(zhí)行所需的操作。
本文內(nèi)容到這里就完結(jié)了,敬請(qǐng)期待后續(xù)內(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)容
想要購(gòu)買TestComplete正版授權(quán),或了解更多產(chǎn)品信息請(qǐng)點(diǎn)擊“咨詢?cè)诰€客服”