【发布时间】:2018-06-15 12:00:56
【问题描述】:
我是 selenium 的新手,正在尝试编写一个测试用例来测试我的 localhost dojo 应用程序中的文件上传。我试过使用 here 和 here 中提到的 Selenium sendkeys 命令,但它没有分配给输入元素,也没有为我的数据处理调用绑定的 dojo 函数的事件。
因此,我找到了一个解决方案here,它允许我与上传对话框进行交互。但是,我无法提交对话框,因为输入键关闭它而不是鼠标单击“打开”提交它。以下是我的代码:
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
}
}
我用于文件输入的 html
<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" />
我如何才能提交带有我为测试用例选择的文件的上传表单对话框?
注意:由于我的机器是托管的,我无法使用 AutoIt 等程序来帮助解决我的问题。
【问题讨论】:
标签: c# winforms selenium file-upload dojo