如何正确声明 class 变量(CUIT 控件)
How to properly declare class variables (CUIT Controls)
我正在为 WPF 应用程序设置编码 UI 测试,我想使用代码方法而不是记录和生成代码方法。我想通过代码使用页面对象,我需要在页面对象中声明控件(按钮、选项卡等)变量,这些变量将被多个函数使用。
我尝试在 class 中声明变量并在构造函数中添加属性 (pendingButton1)
并创建 returns 控件的函数并分配给 class (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)
{
this.pendingButton1.SearchProperties[WpfControl.PropertyNames.AutomationId] = "Tab1Button";
}
public void clickPendingButton() {
WpfToggleButton pendingButton3 = new WpfToggleButton(_wpfWindow);
pendingButton3.SearchProperties[WpfControl.PropertyNames.AutomationId] = "Tab1Button";
WpfToggleButton pendingButton4 = Controls.Press.getPendingButton(_wpfWindow);
Mouse.Click(pendingButton1); //UITestControlNotFoundException
Mouse.Click(pendingButton2); //UITestControlNotFoundException
Mouse.Click(pendingButton3); //This works
Mouse.Click(pendingButton4); //This works
}
}
当我在 clickPendingButton() 函数之外声明 pendingButton 时,我想让它工作,因为它在多个其他函数中使用。
您想要的似乎正是 Coded UI 记录和生成工具生成的 sort 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;
此代码将按钮声明为 class 属性 PendingButton
,并带有一个初始和默认值为 null
的私有支持字段。第一次需要 属性 时,get
代码执行所需的搜索并将找到的控件保存在私有字段中。然后在 属性 的每个后续使用中返回该值。请注意,可以将 null
分配给支持字段以进行新搜索,如 this Q&A.
中所示
辅助函数 Controls.getWpfButton() return 只是按钮的属性,而不是 "real" 按钮。它必须在构造函数中使用,然后它可以在 class 中的任何地方使用。我不会说这是最佳实践,但它对我有用。
Press.cs
public partial class Press : SharedElements
{
private WpfButton pendingButton;
public Press(WpfWindow wpfWindow):base(wpfWindow)
{
pendingTab = Controls.getWpfButton(_wpfWindow, "Tab1Button");
}
public void clickPendingButton() {
Mouse.Click(pendingButton);
}
}
Controls.cs
internal static WpfButton getWpfButton(WpfWindow wpfWindow, string AutomationId)
{
WpfButton button = new WpfButton(wpfWindow);
button.SearchProperties[WpfControl.PropertyNames.AutomationId] = AutomationId;
return button;
}
我正在为 WPF 应用程序设置编码 UI 测试,我想使用代码方法而不是记录和生成代码方法。我想通过代码使用页面对象,我需要在页面对象中声明控件(按钮、选项卡等)变量,这些变量将被多个函数使用。
我尝试在 class 中声明变量并在构造函数中添加属性 (pendingButton1) 并创建 returns 控件的函数并分配给 class (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)
{
this.pendingButton1.SearchProperties[WpfControl.PropertyNames.AutomationId] = "Tab1Button";
}
public void clickPendingButton() {
WpfToggleButton pendingButton3 = new WpfToggleButton(_wpfWindow);
pendingButton3.SearchProperties[WpfControl.PropertyNames.AutomationId] = "Tab1Button";
WpfToggleButton pendingButton4 = Controls.Press.getPendingButton(_wpfWindow);
Mouse.Click(pendingButton1); //UITestControlNotFoundException
Mouse.Click(pendingButton2); //UITestControlNotFoundException
Mouse.Click(pendingButton3); //This works
Mouse.Click(pendingButton4); //This works
}
}
当我在 clickPendingButton() 函数之外声明 pendingButton 时,我想让它工作,因为它在多个其他函数中使用。
您想要的似乎正是 Coded UI 记录和生成工具生成的 sort 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;
此代码将按钮声明为 class 属性 PendingButton
,并带有一个初始和默认值为 null
的私有支持字段。第一次需要 属性 时,get
代码执行所需的搜索并将找到的控件保存在私有字段中。然后在 属性 的每个后续使用中返回该值。请注意,可以将 null
分配给支持字段以进行新搜索,如 this Q&A.
辅助函数 Controls.getWpfButton() return 只是按钮的属性,而不是 "real" 按钮。它必须在构造函数中使用,然后它可以在 class 中的任何地方使用。我不会说这是最佳实践,但它对我有用。
Press.cs
public partial class Press : SharedElements
{
private WpfButton pendingButton;
public Press(WpfWindow wpfWindow):base(wpfWindow)
{
pendingTab = Controls.getWpfButton(_wpfWindow, "Tab1Button");
}
public void clickPendingButton() {
Mouse.Click(pendingButton);
}
}
Controls.cs
internal static WpfButton getWpfButton(WpfWindow wpfWindow, string AutomationId)
{
WpfButton button = new WpfButton(wpfWindow);
button.SearchProperties[WpfControl.PropertyNames.AutomationId] = AutomationId;
return button;
}