我正在为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)
{
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时,我想让它工作,因为它在多个其他函数中使用。
发布于 2019-01-12 06:30:48
您想要的似乎正是编码的UI记录和生成工具生成的代码。它创建了许多具有以下样式的结构的代码:
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;此代码将按钮声明为具有初始值和默认值null的私有支持字段的类属性PendingButton。第一次需要该属性时,get代码执行所需的搜索,并将找到的控件保存在私有字段中。然后,每次使用该属性时都会返回该值。请注意,如this Q&A中所示,可以将null分配给支持字段以进行新的搜索。
发布于 2019-01-14 19:34:48
助手函数Controls.getWpfButton()只返回按钮的属性,而不是“真正的”按钮。它必须在构造函数中使用,然后才能在类中的任何地方使用。我不会说它是最佳实践,但它对我来说是有效的。
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;
}https://stackoverflow.com/questions/54145871
复制相似问题