gpt4 book ai didi

javascript - Protractor 图像上传不适用于 IE 和 firefox

转载 作者:行者123 更新时间:2023-11-30 10:02:46 25 4
gpt4 key购买 nike

我正在尝试在输入类型上上传图像:使用 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com