Uploading an image file with Nightwatch.js

試著忘記壹切 提交于 2019-11-28 07:13:27

问题


I'm running front-end tests using nightwatch.js using the Chrome Driver. I need to test that image uploading works properly, presumably through the provided file input since there are callbacks that run on a successful post.

I'm aware that this can be done using sendKeys method of the Selenium Web Driver.

How can you accomplish this using javascript and nightwatch.js? Can you access the Selenium webdriver or an interface with it?


回答1:


Use this for uploading image from local desktop

.setValue('input[type="file"]', require('path').resolve('/home/My-PC/Desktop/img.png')) 



回答2:


As someone above has mentioned, you can pass the absolute path to your input[type="file"] as if you were typing text into a field.

This uses nightwatch's setValue function. You can retrieve the current directory path using Node's __dirname global var.

For example: .setValue('#upload-input', __dirname + '\\upload.jpg')




回答3:


Use client.setValue function to set the absolute path of your image. Here is the working example in my project.

client.setValue('#editPictures .modal-body input[type="file"]', '/Users/testing/pictures/CAM00003.jpg');




回答4:


In my case there is a div with id="Upload Icon" which has input with type="file"

.setValue('//div[@id="Upload Icon"]/input[@type="file"]', require('path').resolve(__dirname + '/categoryIcon.png'))

The above code working fine for me.




回答5:


This solution worked for me:

.setValue('#file-upload', require('path').resolve('C:/Users/Mihai/Desktop/test.png'))


来源:https://stackoverflow.com/questions/24440712/uploading-an-image-file-with-nightwatch-js

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!