我想使用 Protractor 测试图像上传功能,但我编写的脚本不起作用。 This is the image dialogue box.当我们单击对话框图像时,它会打开窗口以选择所需的图像。选择图像后,框的行为如下 image .我想写一个脚本,通过它我可以上传一张图片,然后点击“保存”按钮。 This is the css of dialogue box. 下面给出的是我试过但不起作用的脚本。遇到的错误信息是this .
var path = require('path');
var fileToUpload = '../new image.jpeg';
var absolutePath = path.resolve('__dirname', fileToUpload);
console.log(absolutePath);
var fileElem=element(by.css('label[for="cropper-file-input"]'));
browser.wait(EC.presenceOf(fileElem), 2000,);
fileElem.sendKeys(absolutePath);
最佳答案
试试下面的方法。希望对您有所帮助。
fileUpload(element: ElementFinder, fileElem: ElementFinder, filePath: string): promise.Promise<void> {
//We are using the setFileDetector method to let WebDriver know that we are uploading files from a local directory to a remote server.
//Having this configuration resolves the "Failed: invalid argument: File not found" error that occurs otherwise.
browser.driver.setFileDetector(new utils.remote.FileDetector);
return browser.executeScript(`arguments[0].style.visibility = 'visible'; arguments[0].style.height = '1px'; arguments[0].style.width = '1px'; arguments[0].style.opacity = 1`, element)
.then(() => fileElem.sendKeys(filePath));
}
关于css - Protractor 图像没有在对话框中上传?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52289091/