【问题标题】:C# Selenium IE test upload file with dojo applicationC# Selenium IE 测试上传文件与 dojo 应用程序
【发布时间】:2018-06-15 12:00:56
【问题描述】:

我是 selenium 的新手,正在尝试编写一个测试用例来测试我的 localhost dojo 应用程序中的文件上传。我试过使用 herehere 中提到的 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


    【解决方案1】:

    错误不是由代码引起的,而是由 HTML 引起的。我注意到我在输入标签中缺少我的附加点属性。添加后,测试运行正常。

    <input id="tempFile" name="file" type="file" style="display:none" data-dojo-attach-point="fileNode" />
    

    【讨论】:

      猜你喜欢
      • 2011-08-18
      • 1970-01-01
      • 1970-01-01
      • 2015-07-22
      • 1970-01-01
      • 2016-07-20
      • 2023-01-20
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多