【问题标题】:Use Fluentlenium to upload a file in dropzone.js使用 Fluentlenium 在 dropzone.js 中上传文件
【发布时间】:2013-12-21 15:22:48
【问题描述】:

我希望编写一个测试来使用 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");

但是,我无法将密钥发送到任何东西,因为在使用 DropZone.js 时它们甚至不是输入 type="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 上没有看到任何对此有帮助。

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

有什么想法吗?

【问题讨论】:

    标签: javascript selenium dropzone.js fluentlenium


    【解决方案1】:

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

    方法一:使用 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)

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-11-21
      • 1970-01-01
      • 1970-01-01
      • 2014-10-16
      • 2019-12-30
      • 2015-09-13
      • 2019-10-01
      • 1970-01-01
      相关资源
      最近更新 更多