如何在FLAUI/WPF窗口中注入模拟?
在我的WPF应用中,我有一个mainwindow.xaml
和相关mainwindow.xaml.cs
。后者有一个属性databaseaccessor
访问数据库的databaseaccessor :
public partial class MainWindow : System.Windows.Window
{
public IDatabaseAccessor DatabaseAccessor { get; set; }
public MainWindow()
{
InitializeComponent();
DatabaseAccessor = new RealDatabaseAccessor();
}
// [...]
}
现在我正在使用FLAUI编写测试。在我的测试中,我正在写类似
using FlaUI.UIA3;
using Moq;
var app = FlaUI.Core.Application.Launch("myapp.exe");
using (var automation = new UIA3Automation())
{
FlaUI.Core.AutomationElements.Window window = app.GetMainWindow(automation);
//// I am looking for a way to do something like this (but no such API exists):
// MainWindow myWindow = (MainWindow) window.RealWindow;
// myWindow.DatabaseAccessor = new Mock<IDatabaseAccessor>().Object;
}
测试的东西,我不想与真实数据库进行通信,而是嘲笑它。
我如何将模拟的databaseaccessor
注入使用flatui打开的窗口(请参阅我的最后一个片段中的注释代码)?或换句话说:如何访问给定flaui.core.core.automationelements.window
的system.window.window
?
如果不可能,您将如何解决这样的问题?
In my WPF app, I have a MainWindow.xaml
and a related MainWindow.xaml.cs
. The latter has a property DatabaseAccessor
which accesses a database:
public partial class MainWindow : System.Windows.Window
{
public IDatabaseAccessor DatabaseAccessor { get; set; }
public MainWindow()
{
InitializeComponent();
DatabaseAccessor = new RealDatabaseAccessor();
}
// [...]
}
Now I'm writing a test using FlaUI. In my test I'm writing something like
using FlaUI.UIA3;
using Moq;
var app = FlaUI.Core.Application.Launch("myapp.exe");
using (var automation = new UIA3Automation())
{
FlaUI.Core.AutomationElements.Window window = app.GetMainWindow(automation);
//// I am looking for a way to do something like this (but no such API exists):
// MainWindow myWindow = (MainWindow) window.RealWindow;
// myWindow.DatabaseAccessor = new Mock<IDatabaseAccessor>().Object;
}
For my tests, I do not want to communicate with the real database but mock it, instead.
How can I inject a mocked DatabaseAccessor
into the Window opened with FlatUI (see the commented code in my last snippet)? Or in other words: How can I access the System.Windows.Window
for a given FlaUI.Core.AutomationElements.Window
?
If this is not possible, how would you approach such a problem?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您需要重新考虑您在这里做的事情。
UI 自动化测试不应该模拟依赖项。它在单独的过程中启动您的应用程序,并使用屏幕上显示的实际元素与之进行交互。
如果要在代码中测试特定组件,则应针对此组件编写单元测试。然后,您可以模拟任何外部依赖关系。
You need to rethink what you are doing here.
An UI automation test is not supposed to mock dependencies. It starts your app in a separate process and interacts with it using the actual elements that are presented on the screen.
If you want to test a specific component in your code, you should write a unit test against this component. You can then mock any external dependencies.