TestComplete教程:光学字符识别(七)获取没有文本内容的控件
TestComplete是一款具有人工智能的自动UI测试工具,利用自动化测试工具和人工智能支持的混合对象识别引擎,轻松检测和测试每个桌面,Web和移动应用程序。本文介绍了如何通过附近控件的文本识别没有文本内容的控件。
在测试中,您可以使用光学字符识别(OCR)引擎通过其文本内容来标识经过测试的UI元素。但是,您可能会遇到这样的情况,您需要在UI元素上模拟用户操作,该UI元素没有任何可用来识别它的文本内容。例如,申请表上的空白文本框或备注字段。
在这种情况下,您可以通过其旁边的文本来标识此类UI元素。例如,您可以通过标签来标识文本框:

为此,您需要获取经过测试的UI元素旁边的文本块,指定目标屏幕区域并在其上模拟用户操作。
在关键字测试中
1、将OCR操作操作添加到测试中。
2、选择包含所需的UI元素的屏幕对象或屏幕区域。确保选定的对象或区域还包括一个文本片段,您将通过该文本片段标识UI元素。TestComplete将识别所选控件或区域中的所有文本。
3、选择一个文本片段:

4、选择要在您测试的UI元素上模拟的操作:
ClickNextTo ——模拟点击(对于桌面和Web应用程序)。
TouchNextTo ——模拟触摸(用于移动应用程序)。
SendKeys ——模拟键盘输入。

5、指定测试的UI元素相对于所选文本块的位置:

6、指定要模拟用户操作的区域与所选文本块的边框之间的距离:

在脚本中
1、获取包含所需的UI元素的屏幕对象或屏幕区域。确保选定的对象或区域还包括文本片段,您将通过该文本片段来标识UI元素。
2、使用该OCR.Recognize方法识别对象或屏幕区域内的文本。
3、使用Block属性或BlockByText方法获取要用来标识UI元素的文本片段。
4、要模拟用户对UI元素的操作,请调用适当的方法,并指定测试的UI元素相对于文本片段的位置以及它们之间的距离:
ClickNextTo ——模拟点击(对于桌面和Web应用程序)。
TouchNextTo ——模拟触摸(用于移动应用程序)。
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 SubDelphiScript
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);
…
}下面的示例演示如何模拟在移动应用程序中由其标签识别的文本框上的触摸,然后在文本框中键入文本:
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 SubDelphiScript
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);
…
}相关内容推荐:
想要购买TestComplete正版授权,或了解更多产品信息请点击


QQ交谈
在线咨询

渝公网安备
50010702500608号

客服热线