我正在尝试使用nightwatch.js在我们的文件上传中上传pdf文件,但问题是我们的上传文件中没有输入type =“ file”。 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>
我尝试将密钥发送到div,div和i表单,但无济于事。这是我尝试上传文件的代码:
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")
};
上传开始于我代码的setvalue部分。上部只是进入上传模式的步骤。提前致谢!!
更新
我在html上找到了一个隐藏的输入字段,但是即使我使用setValue,它也不会上传我的文件。
最佳答案
我正在使用Dropzone.js上传文件。它使输入type =“ file”隐藏。而且nightwatch.js不会在隐藏元素上设置setValue,因此我们需要在设置value之前先查看输入元素。
解决方法是
步骤1:使隐藏的输入元素可见
步骤2:为该输入元素设置值
以下是我上传图片的功能测试用例。
'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正在将输入元素的样式从无更改为块。
这是文档链接http://nightwatchjs.org/api#execute
.execute("document.querySelectorAll('input[type=file]')[0].style.display = 'block';")
document.querySelectorAll('input [type = file]')将返回元素数组,在我的情况下,我需要在第0个位置放置元素,因此我在querySelectorAll的末尾添加了[0]。
注意:您还可以在控制台上运行此JavaScript代码,以查看它是否选择了正确的元素。
.setValue('//div[@id="Upload Icon"]/input[@type="file"]', require('path').resolve(__dirname + '/categoryIcon.png'))
在我的情况下,有一个id为“上传图标”的div,其输入为type =“ file”
关于javascript - 使用Dropzone.js上传NIghtwatch.js文件,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/32715922/