I'm new to selenium and am trying to write a test case to test a file upload in my localhost dojo application. I've tried using Selenium sendkeys command as mentioned here and here, but it does not assign to input element, nor does it call event for the binded dojo function for my data processing.
Therefore, I found a solution here that allows me to interact with the upload dialog. However, I'm unable to submit the dialog as the enter key closes it versus a mouse click on "Open" which submits it. Below is my code:
public IWebDriver driver = new InternetExplorerDriver(MY_DRIVER_LOCATION);
[DllImport("user32.dll", SetLastError = true)]
[return: MarshalAs(UnmanagedType.Bool)]
private static extern bool SetForegroundWindow(IntPtr hWnd);
[DllImport("user32.dll", EntryPoint = "FindWindow")]
public static extern IntPtr FindWindow(string lpClassName, string lpWindowName);
private void UploadFile(string filename)
{
driver.FindElement(By.CssSelector("label.search-btn")).Click(); // Opens the upload file dialog
var dialogHWnd = FindWindow(null, "Choose File to Upload"); // Title for modal. IE: "Choose File to Upload"
var setFocus = SetForegroundWindow(dialogHWnd);
if (setFocus)
{
Thread.Sleep(2000);
SendKeys.SendWait(@"C:\Users\Me\Desktop\TestFile");
SendKeys.SendWait("{DOWN}");
SendKeys.SendWait("{TAB}"); // TAB twice to move focus to "Open" button
SendKeys.SendWait("{TAB}");
SendKeys.SendWait("{ENTER}"); // <--Error here. Closes dialog instead of submits
}
}
My html for the file input
<label for="tempFile" class="search-btn" style="padding-top: 6px; padding-bottom: 6px;" data-dojo-attach-point="uploadLabel" >Browse ...</label>
<input id="tempFile" name="file" type="file" style="display:none" />
How am I able to submit the upload form dialog with the file I selected for my test case?
Note: As my machine is managed, I am unable to use programs such as AutoIt to help solve me issue.