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

Use Fluentlenium to upload a file in dropzone.js

本文关键字:文件 js Fluentlenium dropzone 使用      更新时间:2024-03-07

我想写一个测试来使用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">

我们也在使用亚马逊网络服务器上传文件,看起来一切都在按照下面的脚本进行:

<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为此。

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

有什么想法吗?

我不确定AWS的部分,但我对文件上传(通过Dropzone编程上传/添加文件,例如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)