TestComplete教程:光學字符識別(七)獲取沒有文本內(nèi)容的控件
TestComplete是一款具有人工智能的自動UI測試工具,利用自動化測試工具和人工智能支持的混合對象識別引擎,輕松檢測和測試每個桌面,Web和移動應(yīng)用程序。本文介紹了如何通過附近控件的文本識別沒有文本內(nèi)容的控件。
在測試中,您可以使用光學字符識別(OCR)引擎通過其文本內(nèi)容來標識經(jīng)過測試的UI元素。但是,您可能會遇到這樣的情況,您需要在UI元素上模擬用戶操作,該UI元素沒有任何可用來識別它的文本內(nèi)容。例如,申請表上的空白文本框或備注字段。
在這種情況下,您可以通過其旁邊的文本來標識此類UI元素。例如,您可以通過標簽來標識文本框:
為此,您需要獲取經(jīng)過測試的UI元素旁邊的文本塊,指定目標屏幕區(qū)域并在其上模擬用戶操作。
在關(guān)鍵字測試中
1、將OCR操作操作添加到測試中。
2、選擇包含所需的UI元素的屏幕對象或屏幕區(qū)域。確保選定的對象或區(qū)域還包括一個文本片段,您將通過該文本片段標識UI元素。TestComplete將識別所選控件或區(qū)域中的所有文本。
3、選擇一個文本片段:
4、選擇要在您測試的UI元素上模擬的操作:
ClickNextTo ——模擬點擊(對于桌面和Web應(yīng)用程序)。
TouchNextTo ——模擬觸摸(用于移動應(yīng)用程序)。
SendKeys ——模擬鍵盤輸入。
5、指定測試的UI元素相對于所選文本塊的位置:
6、指定要模擬用戶操作的區(qū)域與所選文本塊的邊框之間的距離:
在腳本中
1、獲取包含所需的UI元素的屏幕對象或屏幕區(qū)域。確保選定的對象或區(qū)域還包括文本片段,您將通過該文本片段來標識UI元素。
2、使用該OCR.Recognize方法識別對象或屏幕區(qū)域內(nèi)的文本。
3、使用Block屬性或BlockByText方法獲取要用來標識UI元素的文本片段。
4、要模擬用戶對UI元素的操作,請調(diào)用適當?shù)姆椒?,并指定測試的UI元素相對于文本片段的位置以及它們之間的距離:
ClickNextTo ——模擬點擊(對于桌面和Web應(yīng)用程序)。
TouchNextTo ——模擬觸摸(用于移動應(yīng)用程序)。
SendKeys ——模擬鍵盤輸入。
下面的示例演示如何模擬在其標簽識別的文本框中的單擊,然后在文本框中鍵入文本:
JavaScript, JScript
function GetControlByNearbyText() { // Recognize the text of the tested application's main window var wnd = Sys.Process("myApp").Window("Main"); var obj = OCR.Recognize(wnd); // Find the "Customer Name" label var t = obj.BlockByText("Customer Name:"); // Enter "John Smith" in the text box to the right of the found label t.ClickNextTo(toRight, 25); t.SendKeys("John Smith", toRight, 25); … }
Python
def GetControlByNearbyText(): # Recognize the text in the tested application wnd = Sys.Process("myApp").Window("Main") obj = OCR.Recognize(wnd) # Find the "Customer Name" label t = obj.BlockByText("Customer Name:") # Enter the text to the text box to the right of the found label t.ClickNextTo(toRight, 25) t.SendKeys("John Smith", toRight, 25)
VBScript
Sub GetControlByNearbyText ' Recognize the text of the tested application's main window Set wnd = Sys.Process("myApp").Window("Main") Set obj = OCR.Recognize(wnd) ' Find the "Customer Name" label Set t = obj.BlockByText("Customer Name:") ' Enter "John Smith" in the text box to the right of the found label Call t.ClickNextTo(toRight, 25) Call t.SendKeys("John Smith", toRight, 25) … End Sub
DelphiScript
procedure GetControlByNearbyText(); var wnd, obj, t; begin // Recognize the text of the tested application's main window wnd := Sys.Process('myApp').Window('Main'); obj := OCR.Recognize(wnd); // Find the 'Customer Name' label t := obj.BlockByText('Customer Name:'); // Enter "John Smith" in the text box to the right of the found label t.ClickNextTo(toRight, 25); t.SendKeys('John Smith', toRight, 25); … end;
C++Script, C#Script
function GetControlByNearbyText() { // Recognize the text of the tested application's main window var wnd = Sys["Process"]("myApp")["Window"]("Main"); var obj = OCR["Recognize"](wnd); // Find the "Customer Name" label var t = obj["BlockByText"]("Customer Name:"); // Enter "John Smith" in the text box to the right of the found label t["ClickNextTo"](toRight, 25); t["SendKeys"]("John Smith", toRight, 25); … }
下面的示例演示如何模擬在移動應(yīng)用程序中由其標簽識別的文本框上的觸摸,然后在文本框中鍵入文本:
JavaScript, JScript
function GetControlByNearbyText_Mobile() { // Recognize the text of the tested application's main window var p = Mobile.Device("MyDevice").Process("smartbear.tctests.myapp").RootLayout("").Layout("layoutTop").WebView("webview").Page("*test*.html"); var obj = OCR.Recognize(p); // Find the "Customer Name" label var t = obj.BlockByText("Customer Name:"); // Enter "John Smith" in the text box to the right of the found label t.TouchNextTo(toRight, 25); t.SendKeys("John Smith", toRight, 25); … }
Python
def GetControlByNearbyText_Mobile(): # Recognize the text in the tested application p = Mobile.Device("MyDevice").Process("smartbear.tctests.myapp").RootLayout("").Layout("layoutTop").WebView("webview").Page("*test*.html") obj = OCR.Recognize(p) # Find the "Customer Name" label t = obj.BlockByText("Customer Name:") # Enter the text to the text box to the right of the found label t.TouchNextTo(toRight, 25) t.SendKeys("John Smith", toRight, 25)
VBScript
Sub GetControlByNearbyText_Mobile() ' Recognize the text of the tested application's main window Set p = Mobile.Device("MyDevice").Process("smartbear.tctests.myapp").RootLayout("").Layout("layoutTop").WebView("webview").Page("*test*.html") Set obj = OCR.Recognize(p) ' Find the "Customer Name" label Set t = obj.BlockByText("Customer Name:") ' Enter "John Smith" in the text box to the right of the found label Call t.TouchNextTo(toRight, 25) Call t.SendKeys("John Smith", toRight, 25) … End Sub
DelphiScript
procedure GetControlByNearbyText_Mobile(); var p, obj, t; begin // Recognize the text of the tested application's main window p := Mobile.Device('MyDevice').Process('smartbear.tctests.myapp').RootLayout('').Layout('layoutTop').WebView('webview').Page('*test*.html'); obj := OCR.Recognize(p); // Find the "Customer Name" label t := obj.BlockByText('Customer Name:'); // Enter "John Smith" in the text box to the right of the found label t.TouchNextTo(toRight, 25); t.SendKeys('John Smith', toRight, 25); … end;
C++Script, C#Script
function GetControlByNearbyText_Mobile() { // Recognize the text of the tested application's main window var p = Mobile["Device"]("MyDevice")["Process"]("smartbear.tctests.myapp")["RootLayout"]("")["Layout"]("layoutTop")["WebView"]("webview")["Page"]("*test*.html"); var obj = OCR["Recognize"](p); // Find the "Customer Name" label var t = obj["BlockByText"]("Customer Name:"); // Enter "John Smith" in the text box to the right of the found label t["TouchNextTo"](toRight, 25); t["SendKeys"]("John Smith", toRight, 25); … }
相關(guān)內(nèi)容推薦:
想要購買TestComplete正版授權(quán),或了解更多產(chǎn)品信息請點擊“咨詢在線客服”