javascript - Dropzone.js : Upload file without Browse dialog in php-webdriver integration tests

标签 javascript jquery selenium dropzone.js facebook-php-webdriver

我正在使用 dropzone.js在我的项目中,我想要做的是在不打开文件浏览器对话框的情况下手动将文件添加到队列中,dropzone 已经在页面上带有 .imageDropzone 类的元素上初始化,我正在尝试运行以下脚本来添加文件,

Dropzone.autoDiscover=false;
var myZone=Dropzone.forElement('.imageDropzone');
var fileList=$("input[accept='image/jpg,image/gif,image/png,image/jpeg']")[0].files;
fileList.name="imageUploadTestJPG.jpg";
fileList.type="image/jpeg";
fileList.size=30170,
fileList.path="http://mysite/img/imageUploadTestJPG.jpg";
fileList.mozFullPath="http://mysite/img/imageUploadTestJPG.jpg";
fileList.accept="image/jpg,image/gif,image/png,image/jpeg";

console.log(fileList);
myZone.addFile(fileList);

我为什么要这样做

1。我正在使用 php-webdriver,我需要测试上传功能,

2. 单击文件类型输入后打开的文件浏览器对话框取决于操作系统,在不同的操作系统中有所不同,我无法将控制权转移到那个窗口,所以我想跳过这个通过单击打开文件对话框并希望手动添加文件 javascript/jquery 并保持 autoProcessFiles=true 的过程所以一旦添加文件,上传过程就会开始,但我无法解决它。

当我尝试调用 Dropzone.addFile() 时我收到以下内容

TypeError: Argument 2 of FormData.append does not implement interface Blob

我尝试了另一种方式,即

1。将文件路径添加到初始化 dropzone 的文件输入,因为 dropzone 绑定(bind)了 change eventlistener所有file inputs这是用 dropzone 初始化的,一旦提供了文件路径 change event listener触发并开始上传文件,但尝试使用初始化的 dropzone 修改文件输入的值会引发安全异常。

2。此外 <input type=file>dropzone.js 隐藏了初始化时的脚本和 php-webdriver 不允许与隐藏元素交互,所以我坚持这个,任何帮助或指导将不胜感激。

最佳答案

完成,

问题出在提供给 myZone.addFile() 的 FileList 对象的格式上。如果您打开 dropzone.js 文件并转到 Dropzone.prototype.init 函数在那里你会看到一个检查

if (this.clickableElements.length) {

在此检查 dropzone 中创建并配置隐藏文件输入,然后将该输入元素附加到正文 document.body.appendChild(_this.hiddenFileInput); 并在此行之后 dropzone 添加 change 事件监听器为创建的文件类型输入,只要我们通过浏览文件窗口提供文件就会触发。

返回 _this.hiddenFileInput.addEventListener("改变", function() {

当我们提供文件时,它会触发并创建 FileList 对象,请参阅

files = _this.hiddenFileInput.files;

如果您尝试在控制台 console.log(files) 中记录它,您将看到一个已创建的 FileList FileList { 0=File, length=1, item=item(), more...} 在 firebug 中单击此对象,您将在下面看到详细信息

0          File { size=21789, type="image/png", name="1-7-2013 6-19-44 PM.png", more...}
length      1
__proto__   FileListPrototype { item=item(), @@iterator=@@iterator()}

现在我创建文件列表对象的方式结果如下

_removeLink -----  a.dz-remove javascrip...defined; 
accept      -----  "image/jpg,image/gif,image/png,image/jpeg"   
accepted    -----  true 
mozFullPath -----  "http://mysite/img/imageUploadTestJPG.jpg"   
name        -----  "imageUploadTestJPG.jpg" 
path        -----  "http://mysite/img/imageUploadTestJPG.jpg"   
previewElement --   div.dz-preview  
previewTemplate --- div.dz-preview  
processing    ----- true    
size                30170   
status        ----- "uploading"
type                "image/jpeg"    
upload        -----  Object { progress=0, total=30170, bytesSent=0}
xhr            XMLHttpRequest { readyState=1, timeout=0, withCredentials=false, more...}
length             0
__proto__          FileListPrototype { item=item(), @@iterator=@@iterator()}

请注意索引 0 在第一个包含文件详细信息的详细信息中,而第二个是我自定义构建的 FileList 对象的结果,主要包含文件的所有详细信息而不是比在索引 0 内。

所以要创建类似的对象我必须先

  • 通过向图片发送xmlHttpRequest请求获取blob
  • 指定arraybuffer的响应类型
  • 获取图像数据的 blob URL
  • 将该响应分配给文件对象并将其分配给 input.file
  • 调用 Dropzone.addFile()

该过程的完整描述如下,您可以在不打开文件浏览窗口的情况下上传文件,并使用 dropzone.jsselenium

// Simulate a call to  service that can
// return an image as an ArrayBuffer.
var xhr = new XMLHttpRequest();

// Use JSFiddle logo as a sample image to avoid complicating
// this example with cross-domain issues.
xhr.open( "GET", "http://localhost/path/to/my/image.jpg", true );

// Ask for the result as an ArrayBuffer.
xhr.responseType = "arraybuffer";

xhr.onload = function( e ) {
    // Obtain a blob: URL for the image data.
    var arrayBufferView = new Uint8Array( this.response );
    var blob = new Blob( [ arrayBufferView ], { type: "image/jpeg" } );
    var urlCreator = window.URL || window.webkitURL;
    var imageUrl = urlCreator.createObjectURL( blob );

    var parts = [blob, new ArrayBuffer()];

    file = new File(parts, "imageUploadTestFile", {
        lastModified: new Date(0), // optional - default = now
        type: "image/jpeg" 
    });

    $("input[accept=\'image/jpg,image/gif,image/png,image/jpeg\']").files = [file];
    myzone = Dropzone.forElement(".imageDropzone");
    myzone.addFile(file);
};
xhr.send();

关于javascript - Dropzone.js : Upload file without Browse dialog in php-webdriver integration tests,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27785721/

相关文章:

Python Selenium 等待 innerHTML

java - org.openqa.selenium.WebDriverException 错误的根本原因是什么?

python - 如何从unittest框架中的另一个测试调用测试方法?

javascript - 禁用视频元素上的上下文菜单

javascript - Plaid 的 LinkTokenCreate 在 node.js 中给出 400 错误

javascript - 从表中读取数据(HTML、JAVAscript)

javascript - 处理 jquery ajax 请求的数据返回

javascript - 使用 CSS 选择器调用 Javascript

javascript - 在 Ace Cloud 9 编辑器中自动调整内容高度

javascript - JQuery 会覆盖现有的内联事件处理程序吗?