博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
桌面应用程序接口测试_如何为桌面应用程序唯一命名元素并自动执行测试
阅读量:2530 次
发布时间:2019-05-11

本文共 4532 字,大约阅读时间需要 15 分钟。

桌面应用程序接口测试

by Vinicius de Melo Rocha

由Vinicius de Melo Rocha

如何为桌面应用程序唯一命名元素并自动执行测试 (How to uniquely name your elements and automate tests for your desktop app)

动机 (Motivation)

Writing automated tests for Desktop applications is not an easy task. Especially when it uses Windows Presentation Foundation (WPF). This allows so many possibilities of nested control and complex grids and menus.

为桌面应用程序编写自动化测试并非易事。 特别是当它使用Windows Presentation Foundation(WPF)时。 这提供了嵌套控件以及复杂的网格和菜单的多种可能性。

Here at , we use a tool to automate desktop tests that rely on the WPF property to interact with the application. Because we need to create dynamic controls based on collection data, they can end up with the same name for multiple UI elements.

在 ,我们使用一种工具来自动化依赖WPF 属性与应用程序进行交互的桌面测试。 因为我们需要基于集合数据创建动态控件,所以对于多个UI元素,它们最终可以使用相同的名称。

For example, the following code generates a menu based on a collection of panels.

例如,以下代码根据面板集合生成菜单。

Inspecting the element tree, we would see that we now have multiple elements with the same name: “MenuBtn”.

检查元素树,我们将看到现在有多个具有相同名称的元素:“ MenuBtn”。

To avoid this situation and have unique names for each button, we came up with four different approaches.

为了避免这种情况并为每个按钮使用唯一的名称,我们提出了四种不同的方法。

  • Using the Code-Behind

    使用代码隐藏
  • Using data binding

    使用数据绑定
  • Using attached properties

    使用附加属性
  • Using collection indexes

    使用集合索引

使用代码隐藏 (Using the Code-Behind)

Assuming that we have access to some unique ID on the elements data context. The easiest approach is to use the Loaded event of FrameworkElement to set a unique name using the code-behind model.

假设我们可以访问元素数据上下文中的某些唯一ID。 最简单的方法是使用代码隐藏模型使用FrameworkElementLoaded事件设置唯一名称。

Now, when we check the element tree, we will see that we have unique names for each button.

现在,当我们检查元素树时,我们将看到每个按钮都有唯一的名称。

使用数据绑定 (Using data binding)

Using data binding makes our code much cleaner, as well as easier to read and understand. If we try a similar approach using data binding we might end up with source code like the following:

使用数据绑定使我们的代码更简洁,也更易于阅读和理解。 如果我们尝试使用数据绑定的类似方法,则最终可能会得到如下的源代码:

Unfortunately, if we try to build this code, we will get a compilation error with the message:

不幸的是,如果我们尝试构建此代码,则会出现以下消息,提示编译错误:

MarkupExtensions are not allowed for Uid or Name property values, so ‘{Binding Panel.PanelType, StringFormat=’MenuBtn{0}’}’ is not valid.
Uid或Name属性值不允许使用MarkupExtensions,因此'{Binding Panel.PanelType,StringFormat ='MenuBtn {0}'}'无效。

This restriction prevents us from binding directly to the Name property.

此限制使我们无法直接绑定到Name属性。

使用附加属性 (Using attached properties)

To overcome the limitation of the previous attempt we can define a new property that would set the name for us. To add new properties to existing controls we can use .

为了克服先前尝试的局限性,我们可以定义一个新属性,为我们设置名称。 要将新属性添加到现有控件中,我们可以使用“ 。

The OnValueChanged event triggers every time the value of our property changes. When that happens, we get the new value and set it to be the FrameworkElement name. We are giving our attached property the name Name. It could be anything we want, like CustomName or TestName.

每当属性值更改时,就会触发OnValueChanged事件。 发生这种情况时,我们将获得新值并将其设置为FrameworkElement名称。 我们将附加属性命名为Name 。 它可以是我们想要的任何东西,例如CustomNameTestName

To use the new property, we need to add a namespace to the XAML and attach the property to our button.

要使用new属性,我们需要向XAML添加名称空间,并将该属性附加到我们的按钮上。

Our code will now compile without any problems, and we will have unique names for each element.

现在,我们的代码可以毫无问题地进行编译,并且每个元素都有唯一的名称。

使用集合索引 (Using collection indexes)

In the previous example, we created unique names by appending the property Id. There are other scenarios where we don’t have an ID on the item to create a unique element name. For that, we can instead use the collection index.

在前面的示例中,我们通过附加属性Id创建了唯一的名称。 在其他情况下,我们在项目上没有ID即可创建唯一的元素名称。 为此,我们可以改用集合索引。

Let’s try to bind our button collection to a list of strings.

让我们尝试将按钮集合绑定到字符串列表。

To achieve that, we can use the same AttachedProperty with a converter. It will look for the index of the element inside the collection.

为此,我们可以将相同的AttachedProperty与转换器一起使用。 它将在集合内寻找元素的索引。

In the XAML, we will now use because we need both the element and the collection.

在XAML中,我们现在将使用因为我们需要元素和集合。

Looking at the element tree we can see that our buttons are named MenuBtn00, MenuBtn01 and so on.

查看元素树,我们可以看到我们的按钮名为MenuBtn00MenuBtn01 ,依此类推。

摘要 (Summary)

Generating unique names for dynamically created WPF controls can be done in an elegant way by using Attached Properties and using the multi-binding with a custom converter.

可以通过使用附加属性并使用带有自定义转换器的多重绑定,以一种优雅的方式为动态创建的WPF控件生成唯一的名称。

翻译自:

桌面应用程序接口测试

转载地址:http://lbkzd.baihongyu.com/

你可能感兴趣的文章
第五天站立会议内容
查看>>
ATMEGA16 IOport相关汇总
查看>>
面试题5:字符串替换空格
查看>>
[Codevs] 线段树练习5
查看>>
Amazon
查看>>
hMailServer搭建简单邮件系统
查看>>
从零开始学习jQuery
查看>>
opacity半透明兼容ie8。。。。ie8半透明
查看>>
CDOJ_24 八球胜负
查看>>
Alpha 冲刺 (7/10)
查看>>
一款jQuery打造的具有多功能切换的幻灯片特效
查看>>
SNMP从入门到开发:进阶篇
查看>>
@ServletComponentScan ,@ComponentScan,@Configuration 解析
查看>>
unity3d 射弹基础案例代码分析
查看>>
thinksns 分页数据
查看>>
os模块
查看>>
最短路径(SP)问题相关算法与模板
查看>>
js算法之最常用的排序
查看>>
Python——交互式图形编程
查看>>
经典排序——希尔排序
查看>>