我点击上传按钮,打开一个Windows文件选择器。我无法使用量角器在该文件选择器对话框中写入文件的路径。
我尝试将文本复制到剪贴板并通过在量角器中传递ctrl + v keypress将其粘贴到文件上传。能够复制文本但粘贴不起作用。
现在我打算使用robotjs或其他工具来实现这一目标。
任何想法我们怎样才能在量角器中做到这一点?
答案 0 :(得分:8)
你试过这个吗?
// set file detector
var remote = require('../../node_modules/protractor/node_modules/selenium-webdriver/remote');
browser.setFileDetector(new remote.FileDetector());
var fileToUpload = '../sample.txt';
var absolutePath = path.resolve(__dirname, fileToUpload);
var fileElem = element(by.css('input[type="file"]'));
// Unhide file input
browser.executeScript("arguments[0].style.visibility = 'visible'; arguments[0].style.height = '1px'; arguments[0].style.width = '1px'; arguments[0].style.opacity = 1", fileElem.getWebElement());
fileElem.sendKeys(absolutePath);
// take a breath
browser.driver.sleep(100);
// click upload button
element(by.css('button[data-ng-click="uploadFile(file)"]')).click(); // does post request
希望这会有所帮助。 :)
答案 1 :(得分:3)
假设没有可能的方法使输入[type =“file]元素可见(请参阅优质产品的答案),因为javascript层上的图层可以使用robotjs通过文件浏览器上传文件。
您需要更改为以下代码片段以适应您的具体情况,但这就是我能够让robotjs工作的方式(利用页面对象设计模式)
页面对象
var robot = require("robotjs");
var path = require("path");
var MediaPO = function() {
...
...
...
this.clickBrowseFileLink = function() {
return browseFileLink.click(); //returns a promise
};
this.uploadFile = function(filename) {
robot.moveMouse(648, 264) //May need to change based on desktop size
var uploadPath = path.resolve(__dirname,'../PATH/TO/UPLOAD/', filename); //change to your upload file's path
robot.setKeyboardDelay(1000);
console.log(uploadPath); //for debugging
robot.typeString(uploadPath);
robot.keyTap("enter");
browser.sleep(3000);
};
...
...
...
规范
//Note: all code but uploadFile should be replaced with the code from your project
// this is just an example
it("should upload .png files successfully", function() {
...
...
...
//your page object function for opening the file explorer
//must return a promise otherwise the string will be typed too early
mediaPO.clickBrowseFileLink().then(function() {
mediaPO.uploadFile("teddy.png");
mediaPO.clickUploadNowButton(); //replace with your method of confirming upload.
expect(mediaPO.pngFileExists()).toBeTruthy();
});
};