没有找到合适的产品?
联系客服协助选型:023-68661681
提供3000多款全球软件/控件产品
针对软件研发的各个阶段提供专业培训与技术咨询
根据客户需求提供定制化的软件开发服务
全球知名设计软件,显著提升设计质量
打造以经营为中心,实现生产过程透明化管理
帮助企业合理产能分配,提高资源利用率
快速打造数字化生产线,实现全流程追溯
生产过程精准追溯,满足企业合规要求
以六西格玛为理论基础,实现产品质量全数字化管理
通过大屏电子看板,实现车间透明化管理
对设备进行全生命周期管理,提高设备综合利用率
实现设备数据的实时采集与监控
利用数字化技术提升油气勘探的效率和成功率
钻井计划优化、实时监控和风险评估
提供业务洞察与决策支持实现数据驱动决策
翻译|使用教程|编辑:龚雪|2024-04-10 10:57:26.967|阅读 116 次
概述:本文将为大家展示如何使用UI自动化在Visual Studio 2022中编写简单/高级UI测试,欢迎下载相关组件体验!
# 界面/图表报表/文档/IDE等千款热门软控件火热销售中 >>
DevExpress WinForm拥有180+组件和UI库,能为Windows Forms平台创建具有影响力的业务解决方案。DevExpress WinForm能完美构建流畅、美观且易于使用的应用程序,无论是Office风格的界面,还是分析处理大批量的业务数据,它都能轻松胜任!
UI自动化测试利用特定的工具/框架来模拟用户与界面的交互,并帮助确保应用程序满足相关的最终用户需求。当与其他测试方法(API测试、单元测试等)结合使用时,UI自动化可以提高应用程序的稳定性,减少花在手工测试上的时间,当然还可以提高用户满意度。在本文中,我们将向您展示如何使用UI自动化在Visual Studio 2022中编写简单/高级UI测试。
在开始之前,我们先看看UI测试的优势:
DevExpress技术交流群9:909157416 欢迎一起进群讨论
在上文中(),我们为大家介绍了UI测试自动化是如何工作的、开始创建UI自动化测试等,本文将继续介绍如何创还能UI自动化测试。
在进行测试之前,我想澄清几点:
AutomationElement logInFormElement = AutomationElement.RootElement.FindFirst(TreeScope.Children, new PropertyCondition(AutomationElement.NameProperty, "CRM Log In Form"));
FindFirst方法有两个参数,第一个参数(scope)指定搜索的范围,第二个参数(条件)指定要匹配的标准(在我的例子中,这是一个AutomationName = "CRM Log In Form"的表单)。
UI控件可以根据其他设置(例如,Text)自动“计算”AutomationName。
如果需要,您可以显式地设置AutomationName属性或处理DXAccessible.QueryAccessibleInfo事件,来向DevExpress UI元素提供可访问性信息。
public static class AutomationElementExtensions {
public static AutomationElement FindFirstWithTimeout(this AutomationElement @this,
TreeScope scope, Condition condition, int timeoutMilliseconds = 1000) {
Stopwatch stopwatch = new Stopwatch();
stopwatch.Start();
do {
var result = @this.FindFirst(scope, condition);
if (result != null)
return result;
Thread.Sleep(100);
}
while (stopwatch.ElapsedMilliseconds < timeoutMilliseconds);
return null;
}
}
下面的测试将执行以下操作:
[Test]
public void NonExistingUsernameLoginTest() {
afterLogInAction = CheckErrorLabel;
LogIn("TestNonExistingUser", "123456");
}
void CheckErrorLabel() {
AutomationElement errorLabelElement = loginForm.FindFirstByNameWithTimeout(
TreeScope.Children,
"Invalid User or Password",
10000);
Assert.IsNotNull(errorLabelElement);
}
void LogIn(string username, string password) {
// Finds the LogIn form and its main UI elements.
loginForm = AutomationElement.RootElement.FindFirstByNameWithTimeout(
TreeScope.Children,
logInFormAccessbleName,
10000);
AutomationElement usernameElement = loginForm.FindFirstByNameWithTimeout(TreeScope.Children, usernameAccessbleName, 10000);
AutomationElement passwordElement = loginForm.FindFirstByNameWithTimeout(TreeScope.Children, passwordAccessbleName, 10000);
AutomationElement logInButtonElement = loginForm.FindFirstByNameWithTimeout(TreeScope.Children, logInButtonAccessbleName, 10000);
// Gets automation patterns to fill "UserName" and "Password" inputs (editors).
ValuePattern usernameValuePattern = (ValuePattern)usernameElement.GetCurrentPattern(ValuePattern.Pattern);
ValuePattern passwordValuePattern = (ValuePattern)passwordElement.GetCurrentPattern(ValuePattern.Pattern);
InvokePattern invokePattern = (InvokePattern)logInButtonElement.GetCurrentPattern(InvokePattern.Pattern);
// Sets editor values. Fills in username and password input fields.
usernameValuePattern.SetValue(username);
passwordValuePattern.SetValue(password);
invokePattern.Invoke();
// Performs an action after a log in attempt.
afterLogInAction?.Invoke();
}
正如您所看到的,编写测试可以归结为获取一个AutomationElement并调用它的模式方法。
让我们考虑一个更复杂的情况,并在DevExpress WinForm数据网格(GridControl)中测试数据编辑。DevExpress数据网格包含一个带有布尔值的“Is Modified”未绑定列,该列的值表示用户是否修改了“Name”列的值。
我使用TablePattern与网格控件一起工作,下面的例子展示了如何编写一个测试来修改我们的WinForms Grid中的客户名称,并检查“Is Modified”列中的值是否从false变为true:
[Test]
public void ModifiedCustomerTest() {
LogIn(testExistingUserLogin, testExistingUserPassword);
// Finds the GridControl and gets its TablePattern.
customersForm = AutomationElement.RootElement.FindFirstByNameWithTimeout(
TreeScope.Children,
customersFormAccessbleName,
10000);
AutomationElement customersGrid = customersForm.FindFirstByIdWithTimeout(
TreeScope.Children,
customersGridAutomationID,
10000);
TablePattern customersTablePattern = (TablePattern)customersGrid.GetCurrentPattern(TablePattern.Pattern);
// Activates a cell within the GridControl.
AutomationElement cellToUpdate = customersTablePattern.GetItem(1, 1);
InvokePattern testCellInvokePattern = (InvokePattern)cellToUpdate.GetCurrentPattern(InvokePattern.Pattern);
testCellInvokePattern.Invoke();
// Modifies the cell's value.
AutomationElement editingControl = customersGrid.FindFirstByNameWithTimeout(TreeScope.Descendants, "Editing control", 1000);
ValuePattern editedCellValuePattern = (ValuePattern)editingControl.GetCurrentPattern(ValuePattern.Pattern);
editedCellValuePattern.SetValue("Value updated!");
Thread.Sleep(1000); // Sets a delay for demonstration purposes.
// Selects the next data row.
AutomationElement nextRowCell = customersTablePattern.GetItem(2, 1);
SelectionItemPattern selectionItemPattern = (SelectionItemPattern)TreeWalker.ControlViewWalker.GetParent(nextRowCell).GetCurrentPattern(SelectionItemPattern.Pattern);
selectionItemPattern.Select();
Thread.Sleep(1000);
// Checks if the value in the "Is Modified" column has changed.
int isModiedColumnIndex = customersTablePattern.Current.GetColumnHeaders().ToList().FindIndex(h => h.Current.Name == "Is Modified");
AutomationElement isModifiedCell = customersTablePattern.GetItem(1, isModiedColumnIndex);
ValuePattern isModifiedCellValuePattern = (ValuePattern)isModifiedCell.GetCurrentPattern(ValuePattern.Pattern);
Assert.AreEqual(isModifiedCellValuePattern.Current.Value, "Checked");
}
要运行我刚刚创建的测试,将用tests展开项目(“TestRunner”),右键单击*.cs文件来调用上下文菜单,然后单击"Run Tests"。
本站文章除注明转载外,均为本站原创或翻译。欢迎任何形式的转载,但请务必注明出处、不得修改原文相关链接,如果存在内容上的异议请邮件反馈至chenjj@ke049m.cn
文章转载自:慧都网



注意: Cogent DataHub 软件 v11 包含一些新功能,您的目标操作系统可能不支持这些功能。
本教程主要为大家介绍如何使用DevExpress WinForms数据网格控件进行数据排序的基础知识,欢迎下载最新版组件体验!
在使用Parasoft C/C++test执行BugDetective数据流分析时,可能会遇到用户自定义的资源API,那在这种情况下,若要判断是否存在资源问题,如资源泄露等,则需要手动配置测试配置。
大型SaaS系统的自动化测试常常受制于界面变化快、结构复杂、加载机制多变等因素。从元素识别到脚本管理,SmartBear TestComplete帮助Salesforce建了可靠的自动化测试体系。
相关产品
优秀的界面控件开发包,帮助企业构建卓越应用!
DevExpress DXperience Subscription高性价比的企业级.NET用户界面套包,助力企业创建卓越应用!
DevExpress WinForms Subscription为Windows Forms平台创建具有影响力的业务解决方案,高性价比WinForms界面控件套包。
最新文章 MORE
半岛外围网上直营相关的文章 MORE
服务电话
重庆/ 023-68661681
华东/ 13452821722
华南/ 18100878085
华北/ 17347785263
客户支持
技术支持咨询服务
服务热线:400-700-1020
邮箱:sales@ke049m.cn
关注我们
地址 : 重庆市九龙坡区火炬大道69号6幢
半岛外围网上直营 