javascript - 使用 Fluentlenium 在 dropzone.js 中上传文件

标签 javascript selenium dropzone.js fluentlenium

我正在寻找使用 Fluentlenium 和 DropZone.js ( http://www.dropzonejs.com/ ) 编写一个上传文件的测试。 Dropzone.js 在模式下工作,然后您可以以正常方式拖放或上传。

一旦您点击上传测试就会崩溃,因为您不再在浏览器中。

我发现很多帖子使用以下方法在 Selenium 中使用它:

WebElement fileInput = driver.findElement(By.xpath("//input[@type='file']"));
fileInput.sendKeys("C:/path/to/file.jpg");

但是我不能将 key 发送给任何东西,因为在使用 DropZone.js 时它们甚至不是输入类型 ="file"。

我看到的唯一输入类型都是隐藏类型。

<input type="hidden" name="key" value="temp/${filename}">
<input type="hidden" name="AWSAccessKeyId" value="secret">
<input type="hidden" name="acl" value="private">
<input type="hidden" name="success_action_redirect" value="">
<input type="hidden" name="policy" value="secret=">
<input type="hidden" name="signature" value="secret">
<input type="hidden" name="Content-Type" value="application">

我们也使用 Amazon Web Server 上传文档,似乎一切都在使用以下脚本:

<script id="hiddenKeyPairs" type="text/javascript">
  var hiddenKeyPairs = {
    key:  'temp/${filename}',
    AWSAccessKeyId: 'secret',
    acl: 'private',
    "success_action_redirect": '',
    policy: 'secret',
    signature: 'secret/secret',
    "Content-Type": 'application'
  };

  var formAction = 'https://secret.com/';

</script>

位于我的页面上。

我在 https://github.com/FluentLenium/FluentLenium#driver 上没有看到任何有帮助的东西|为此。

我是否需要以某种方式将文件发送到上述脚本中的 key 散列?

有什么想法吗?

最佳答案

我不确定 AWS 部分,但我有一个关于文件上传 ( Programmatically upload / add file via Dropzone e.g. by Selenium ) 的类似问题,以及一些可能的解决方案。我觉得它们不是很健壮,但基本上:

方法 1:使用 Java Robot 模拟 GUI 操作 -

    // this opens the file browser window
    driver.findElement(By.id("uploadDropzone")).click();

    // put the file path in clipboard, paste (C-V) to the window, enter.
    StringSelection ss = new StringSelection("some file path");
    Toolkit.getDefaultToolkit().getSystemClipboard().setContents(ss, null);
    Robot robot = new Robot();
    Thread.sleep(2000);
    robot.keyPress(KeyEvent.VK_CONTROL);
    robot.keyPress(KeyEvent.VK_V);
    robot.keyRelease(KeyEvent.VK_V);
    robot.keyRelease(KeyEvent.VK_CONTROL);
    Thread.sleep(5000);    // need some wait for GUI action to work...
    robot.keyPress(KeyEvent.VK_ENTER);
    robot.keyRelease(KeyEvent.VK_ENTER)

方法 2:在代码中完成所有操作(hacky...)- 是的,有一个文件输入元素,但仅在 Dropzone.js 本身中定义,可以使用 $ 选择(".dz-hidden-input")。但是您还必须使其可见(因为 Selenium 只能作用于可见元素),然后可以对其调用 sendKeys。之后,再次在 Javascript 中,从该元素中检索 File 对象,然后传递给 Dropzone 对象上的 addFile(file)

关于javascript - 使用 Fluentlenium 在 dropzone.js 中上传文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20387247/

相关文章:

python - Gecko驱动问题

javascript - ASP.NET MVC 中的拖放区错误消息

javascript - 当我设置上传图片的限制数量时,dropzone.js 应该如何考虑预加载图片?

javascript - 为什么 CSS 悬停动画与 Firefox 的工作方式不同?

javascript - 使用 jQuery slideToggle() 不显示 :none?

javascript - 测试 oAuth 登录流程

javascript - Dropzone.js 提交多个 id

javascript - Node.js - 我将如何转发 HTTP 流量(反向代理)?

c# - 无法在 http ://localhost 上启动驱动程序服务

java - Eclipse 在报告生成中忽略 CucumberOptions