I am using a WebBrowser control for some automated testing. The problem is that occasionally - not all the time - when I am testing uploading images, the file upload dialog box does not close and the the program just "hangs" and waits for manual input, which defeats the purpose of the whole automated process. What I want to do is to "force" a close of the dialog box, but have been unable to figure this out. Any help or direction would be much appreciated.
The thing to realize is that this code works some of the time, but NOT all of the time. I need help figuring out how to make this code work ALL of the time.
Here is the code:
async Task PopulateInputFile(System.Windows.Forms.HtmlElement file, string fname)
{
file.Focus();
// delay the execution of SendKey 500ms to let the Choose File dialog show up
var sendKeyTask = Task.Delay(5000).ContinueWith((_) =>
{
// this gets executed when the dialog is visible
//SendKeys.Send(fname + "{ENTER}");
//PressKey(Keys.Space, false);
SendKeys.SendWait(fname);
PressKey(Keys.Enter, false);
}, TaskScheduler.FromCurrentSynchronizationContext());
file.InvokeMember("Click"); // this shows up the dialog
await sendKeyTask;
// delay continuation 500ms to let the Choose File dialog hide
await Task.Delay(5000);
}
async Task Populate(string fname)
{
var elements = webBrowser.Document.GetElementsByTagName("input");
foreach (System.Windows.Forms.HtmlElement file in elements)
{
if (file.GetAttribute("name") == "file")
{
this.Activate();
this.BringToFront();
file.Focus();
await PopulateInputFile(file, fname);
file.RemoveFocus();
}
}
}