翻譯|使用教程|編輯:莫成敏|2019-11-08 12:06:18.943|閱讀 362 次
概述:TestComplete是一款自動化功能測試平臺。本文描述了在光學字符識別教程中,如何使用光學字符識別來檢查您的測試應用程序在屏幕上呈現的文本內容第二部分——高級文本內容驗證。
# 界面/圖表報表/文檔/IDE等千款熱門軟控件火熱銷售中 >>
相關鏈接:
TestComplete是一款具有人工智能的自動UI測試工具,利用自動化測試工具和人工智能支持的混合對象識別引擎,輕松檢測和測試每個桌面,Web和移動應用程序。使用TestComplete,可以提高測試覆蓋率并幫助提供經過實戰考驗的高質量軟件。本文描述了在光學字符識別教程中,如何使用光學字符識別來檢查您的測試應用程序在屏幕上呈現的文本內容第二部分——高級文本內容驗證。
在測試中,您可能需要檢查是否在屏幕上呈現了預期的文本,并根據檢查結果執行各種操作(例如,模擬用戶操作或將自定義消息發布到測試日志)。您可以通過以下方式執行此操作:
使用該OCR.Recognize方法在所需的屏幕區域中識別渲染的文本。
通過使用OCR.Recognize.FullText屬性獲取識別的文本。
檢查獲取的文本是否包含預期的子字符串。為此,您可以使用各種比較字符串值的操作。例如,aqString.Find方法。
根據檢查結果,執行所需的操作。
注意:要根據模式驗證文本內容并將成功/失敗消息發布到測試日志,請使用OCR檢查點。請參閱驗證文本內容。
在腳本中
下面的代碼包含CheckTextContents獲取屏幕上對象和字符串的例程,并驗證對象的文本是否包含該字符串。例程將第三個參數用作布爾值,該布爾值指定檢查是區分大小寫還是不區分大小寫。為了獲取屏幕上對象的文本,例程使用OCR.Recognize.FullText屬性。為了驗證渲染的文本是否包含字符串,例程使用該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 SubDelphiScript
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例程顯示了如何調用CheckTextContents例程以驗證測試應用程序的主要形式是否呈現了預期的子字符串。根據結果,它將適當的消息發布到測試日志。
在關鍵字測試中
將CheckTextContents功能代碼從上面的示例復制到TestComplete中的測試項目中的腳本單元。
從關鍵字測試調用此函數。為此,您可以使用“運行代碼片段”或“運行腳本例程”操作。
獲取函數結果。根據它們執行所需的操作。

本文內容到這里就完結了,敬請期待后續內容“等待文本顯示在屏幕上”,感興趣的朋友可以繼續關注我們哦~或者您下載TestComplete試用版進行免費評估~
相關內容推薦:
TestComplete教程:光學字符識別(一)處理UI元素
TestComplete教程:光學字符識別(二)識別屏幕上文本須滿足的要求
TestComplete教程:光學字符識別(三)模擬用戶操作
TestComplete教程:光學字符識別(四)驗證文字內容
想要購買TestComplete正版授權,或了解更多產品信息請點擊
本站文章除注明轉載外,均為本站原創或翻譯。歡迎任何形式的轉載,但請務必注明出處、不得修改原文相關鏈接,如果存在內容上的異議請郵件反饋至chenjj@ke049m.cn
文章轉載自: