dotnet / dotnet/winforms

[Tracking] UI Test Dialogs Reliably

Open
#8,170 1 comment 0 reactions 0 assignees View on GitHub
test-enhancement
Dominant language
C#
Stars
4.9k
Forks
1.1k
Avg merge
1d 13m
Merged PRs (30d)
85

Description

When UI testing for dialogs such as `FolderBrowserDialog`,`OpenFileDialog`,`SaveFileDialog`, etc. we should test reliably/quickly by:

1. Avoiding hunting for HWNDs
2. Interacting with the dialog programmatically rather than via mouse or keyboard input if possible

An example of this would be the following:
```c#
[WinFormsFact]
public void OpenFileDialog_ShowDialog_Success()
{
using DialogHostForm dialogOwnerForm = new();
using OpenFileDialog dialog = new();
Assert.Equal(DialogResult.Cancel, dialog.ShowDialog(dialogOwnerForm));
}

internal class DialogHostForm : Form
{
protected override void WndProc(ref Message m)
{
if (m.MsgInternal == WM.ENTERIDLE && m.WParamInternal == (uint)MSGF.DIALOGBOX)
{
HWND dialogHandle = (HWND)m.LParamInternal;
PInvoke.PostMessage(dialogHandle, WM.CLOSE);
}

base.WndProc(ref m);
}
}
```

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.