翻譯|使用教程|編輯:莫成敏|2019-11-14 11:58:55.940|閱讀 410 次
概述:TestComplete是一款自動化功能測試平臺。本文描述了在光學字符識別教程中,如何使用光學字符識別來檢查您的測試應用程序在屏幕上呈現的文本內容第三部分——等待文本顯示在屏幕上。
# 界面/圖表報表/文檔/IDE等千款熱門軟控件火熱銷售中 >>
相關鏈接:
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正版授權,或了解更多產品信息請點擊
本站文章除注明轉載外,均為本站原創或翻譯。歡迎任何形式的轉載,但請務必注明出處、不得修改原文相關鏈接,如果存在內容上的異議請郵件反饋至chenjj@fc6vip.cn
文章轉載自: