作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在尝试在输入类型上上传图像:使用 Protractor 的文件控件。
我正在使用下面的代码:
var basePath='../testdata/testappicons/accepted';
var randomIcon=randomIntFromInterval(1,6);
var overallPathToIcon=basePath+randomIcon+'.png';
var fileToUpload = overallPathToIcon;
console.log(fileToUpload);
var absolutePath = path.resolve(__dirname, fileToUpload)
browser.executeScript('$(\'input[type="file"]\').attr("style", "");');
$('input[type="file"]').sendKeys(absolutePath);
上面的代码在 chrome 上运行良好,我可以上传文件,但是当我在 firefox 和 IE 上使用相同的代码时。我收到以下错误:
ElementNotVisibleError: Element is not displayed
这是表单的 HTML:
div class="fileUpload" ng-click="IsInValid=true">
<label class="btn btn-white uploadBtn" title="Upload image file" for="upload-image">
<input id="uploadFile" type="text" readonly="" placeholder="Browse new app icon" value="maxsizepngfile.png">
<input id="upload-image" class="upload ng-pristine ng-untouched ng-valid ng-isolate-scope" type="file" ng-model="image" image="appicon" accept="image/*">
</label>
<label class="btn btn-warning" for="upload-image">
<span>Browse</span>
</label>
</div>
我猜这是因为它是只读的,这就是它不起作用的原因。谁能帮我解决这个问题?
这是为我工作的代码
var basePath='../testdata/testappicons/accepted';
var randomIcon=randomIntFromInterval(1,6);
var overallPathToIcon=basePath+randomIcon+'.png';
var fileToUpload = overallPathToIcon;
console.log(fileToUpload);
var absolutePath = path.resolve(__dirname, fileToUpload)
//browser.executeScript('$(\'input[type="file"]\').attr("style", "");');
var elm = $('input[type="file"]'); // protractor's shortcut to element(by.css("css"))
//browser.executeScript('var input = $("input:file"); var elm = input[0];elm.style.visibility = "visible"; elm.style.height = "1px"; elm.style.width = "1px";elm.style.opacity = 1;');
browser.executeScript('var input = $("input:file"); input[0].removeAttribute("class");');
elm.sendKeys(absolutePath);
最佳答案
这仍然是一种猜测,但有根据:不要让 jQuery 参与设置 style
:
var elm = $('input[type="file"]'); // protractor's shortcut to element(by.css("css"))
browser.executeScript('arguments[0].style = {};', elm.getWebElement());
elm.sendKeys(absolutePath);
另外,关注@dmitankin 的评论和 Does WebDriver support file uploads? FAQ 部分,尝试以这种方式使元素可见:
function makeVisible(arguments) {
var elm = arguments[0];
elm.style.visibility = 'visible';
elm.style.height = '1px';
elm.style.width = '1px';
elm.style.opacity = 1;
}
browser.executeScript(makeVisible, elm.getWebElement());
关于javascript - Protractor 图像上传不适用于 IE 和 firefox,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30724494/
我是一名优秀的程序员,十分优秀!