我想编写一个使用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");但是,我不能sendKeys到任何东西,因为当使用DropZone.js时,它们甚至不是一个输入type=“文件”。
我看到的唯一输入类型都是隐藏类型。
<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上传文档,似乎所有东西都在使用下面的脚本:
<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上有任何帮助。
我是否需要以某种方式将文件发送到上面脚本中的键哈希?
有什么想法吗?
发布于 2016-09-14 02:55:14
我不确定AWS部分,但我有一个类似的问题关于文件上传(Programmatically upload / add file via Dropzone e.g. by Selenium),以及一些潜在的解决方案。我觉得他们不是很强壮,但基本上:
方法1:使用Java机器人模拟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)。
https://stackoverflow.com/questions/20387247
复制相似问题