[英]NIghtwatch.js File Upload with Dropzone.js
I am trying to upload a pdf file on our file upload using nightwatch.js but the thing is there are no input type="file" on our upload.我正在尝试使用 nightwatch.js 在我们的文件上传中上传 pdf 文件,但问题是我们的上传中没有 input type="file" 。 The html looks like this:
html 看起来像这样:
<form id="dropzone" action="/v2/asset/56013895934fd606006fb314" class="dropzone dz-clickable">
<div class="dz-clickable" id="dropzonePreview">
<i class="fa fa-cloud-upload main-icon initial-icon"></i>
</div>
<div class="dz-default dz-message">
<span>Drop a file here or click icon above to upload an image</span>
</div>
</form>
I tried sending keys to the form, div and i but to no avail it wont work.我尝试向表单、div 和 i 发送密钥,但无济于事。 This is my code on how i try upload the file:
这是我如何尝试上传文件的代码:
var uploadInvoice = function(browser) {
browser
.waitForElementPresent(dashboardselector.view_assignment, 20000)
.click(dashboardselector.view_assignment)
.waitForElementVisible(".fa-plus", 20000)
.click(".fa-plus")
.click(".regionBillAsset > div:nth-child(1) > a:nth-child(1)")
.setValue("#dropzone", require('path').resolve(__dirname+'/document.pdf'))
.waitForElementVisible(".after-success", 20000)
.click(".after-success")
};
the upload starts on the setvalue part of my code.上传从我的代码的 setvalue 部分开始。 The upper part are just steps to go to the upload modal.
上半部分只是进入上传模式的步骤。 Thanks in advance!!
提前致谢!!
UPDATE I have found a hidden input field on the html but even though I use setValue, it won't upload my file.更新我在 html 上发现了一个隐藏的输入字段,但即使我使用了 setValue,它也不会上传我的文件。
I am using Dropzone.js for uploading file. 我正在使用Dropzone.js上传文件。 It makes the input type="file" to hidden.
它使输入type =“ file”隐藏。 And nightwatch.js does not setValue on hidden element so we need to visible the input element before setting value.
而且nightwatch.js不会在隐藏元素上设置setValue,因此我们需要在设置value之前先查看输入元素。
Solution is 解决方法是
step 1: make that hidden input element visible 步骤1:使隐藏的输入元素可见
step 2: set value to that input element 步骤2:为该输入元素设置值
below is my functional test case for uploading image. 以下是我上传图片的功能测试用例。
'uploadin the image': (browser) => {
browser
.execute("document.querySelectorAll('input[type=file]')[0].style.display = 'block';")
.pause(100)
.setValue('//div[@id="Upload Icon"]/input[@type="file"]', require('path').resolve(__dirname + '/categoryIcon.png'))
.waitForElementVisible('//div/span[text()="Change Icon"]', 5000)
.assert.containsText('//div/span[text()="Change Icon"]', 'Change Icon')
.pause(2000)
.end();
}
execute is changing the style of input element from none to block. execute正在将输入元素的样式从无更改为块。 Here is the documentation link http://nightwatchjs.org/api#execute
这是文档链接http://nightwatchjs.org/api#execute
.execute("document.querySelectorAll('input[type=file]')[0].style.display = 'block';")
document.querySelectorAll('input[type=file]') will return the array of elements and in my case i need element on the 0th position so i added [0] at the end of querySelectorAll. document.querySelectorAll('input [type = file]')将返回元素数组,在我的情况下,我需要在第0个位置放置元素,因此我在querySelectorAll的末尾添加了[0]。
Note: You can also run this JavaScript code on the console to see if it is selecting the right element. 注意:您还可以在控制台上运行此JavaScript代码,以查看它是否选择了正确的元素。
.setValue('//div[@id="Upload Icon"]/input[@type="file"]', require('path').resolve(__dirname + '/categoryIcon.png'))
In my case there is a div with id="Upload Icon" which has input with type="file" 在我的情况下,有一个id为“上传图标”的div,其输入为type =“ file”
I'm in the same position as you. 我和你在同一位置 And as far as the hidden input, I believe nightwatch can only upload on visible elements.
至于隐藏的输入,我相信守夜只能上传可见元素。
You can make it visible like this > 您可以这样显示它>
client.execute(function(data){
document.querySelector('input[type="file"]').className="";
}, [], function(result){
console.log(result);
});
after this you can set your value for upload. 之后,您可以设置要上传的值。
as of Dropzone v3.12.0 (note: the newest version is v5.4.0) 从Dropzone v3.12.0开始(注意:最新版本为v5.4.0)
You can do this in nigthwatch.js to upload a file. 您可以在nigthwatch.js中执行此操作,以上传文件。
browser
.execute(`
// this makes the DropZone hidden input, visible
document.querySelector('input[type="file"]').style.visibility = "visible";
`)
.setValue('input.dz-hidden-input', AbsolutePathToFile)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.