关于c#:如何正确声明类变量(CUIT Controls)
How to properly declare class variables (CUIT Controls)
我正在为 WPF 应用程序设置编码 UI 测试,并且我想使用代码方法而不是记录并生成代码方法。我想通过代码使用页面对象,我需要在页面对象中声明控件(按钮、选项卡等)变量,这些变量将被多个函数使用。
我尝试在类中声明变量并在构造函数中添加属性 (pendingButton1)
并创建函数,该函数返回控件并分配给类中的变量(pendingButton2)但均无效。
当我在要在(pendingButton3 和 4)中使用变量的函数中声明变量(或通过函数创建变量)时,它会起作用。
|
public partial class Press : Header
{ WpfToggleButton pendingButton1 = new WpfToggleButton(_wpfWindow); WpfToggleButton pendingButton2 = Controls.Press.getPendingButton(_wpfWindow); public Press(WpfWindow wpfWindow):base(wpfWindow) public void clickPendingButton() { WpfToggleButton pendingButton4 = Controls.Press.getPendingButton(_wpfWindow); Mouse.Click(pendingButton1); //UITestControlNotFoundException |
当我在 clickPendingButton() 函数之外声明 pendingButton 时,我想让它工作,因为它用于多个其他函数。
相关讨论
- 到目前为止,当我在桌子上使用我的鸭子时,我发现它可能会对继承做一些事情 - 我从 Header 类继承变量 _wpfWindow,因此,我应该在构造函数中使用带有 _wpfWindow 继承变量的初始化类变量。无论如何,在我看来,在类中创建变量并在构造函数中对其进行初始化然后添加搜索属性有点开销。
- 自己编码很好,但让 Coded UI 的记录和生成教你。在沙盒项目中使用它并复制您需要的位。
-
请注意,当涉及到动态添加到被测 UI 的控件时,记录和生成没有用。例如。每次开始测试时可以包含不同信息的网格行。在这种情况下,您可以在包含这些行的父(表)上使用
getChildren() 函数。
n
您想要的似乎正是 Coded UI 记录和生成工具生成的排序 f 代码。它创建了许多具有以下风格结构的代码:
|
public WpfToggleButton PendingButton
{ get { if ((this.mPendingButton == null)) { this.mPendingButton = new WpfToggleButton( ... as needed ...); this.mPendingButton.SearchProperties[ ... as needed ...] = ... as needed ...; } return this.mPendingButton; private WpfToggleButton mPendingButton; |
此代码将按钮声明为类属性
相关讨论
- 谢谢你的回答。我受到生成代码的启发,但我采用了另一种方法。我创建了一个帮助函数 getWpfButton(WpfWin, AutomationId),它只返回按钮的属性。我相信,按钮在使用时会被搜索(例如通过 Mouse.Click())。需要明确的是 - 我将挂起的按钮声明为类变量,我将它分配给构造函数中的辅助函数。无论如何,如果我遇到任何问题,我会采用这种方法。
- @MartyJaník 我没有完全关注您的评论。如果您写一个正确的答案来解释您如何解决问题,这可能对其他人有所帮助。