【问题标题】:Facing issue while uploading document through protractor通过量角器上传文件时面临问题
【发布时间】:2019-07-16 12:41:55
【问题描述】:

我正在尝试通过量角器将文档上传到我的 Angular 应用程序中,但在单击“上传”按钮后出现错误。 我正在尝试通过以下代码: 常量 absPath = path.resolve(_dirname, PathofFile); return $('input[type="file"]').sendkeys(absPath);

量角器版本:5.4.2 Npm 版本:3.10.10 节点版本:v6.11.1

事实是,手动它工作正常。拖动文件并单击上传按钮,正在上传文件。

谁能帮我解决这个问题。

我想补充一点,我尝试在量角器脚本中休眠并手动执行上传操作,它工作正常。我认为它没有将上传文件算作正确的文件或文件本身。

【问题讨论】:

  • 文件名文本框是否可见?也就是说(手动),你可以输入文件名还是只拖动?一旦我需要 const bCom1 = 'arguments[0].style = ""; arguments[0].style.display = "block"; arguments[0].style.visibility = "可见";'; const bCom2 = 'arguments[0].style.opacity = 1;';等待 browser.executeScript(bCom1, element(by.id('fileButton')).getWebElement());等待 browser.executeScript(bCom2, element(by.id('fileButton')).getWebElement()); await browser.executeScript('arguments[0].scrollIntoView(true);', element(by.id('fileButton')).getWebElement());然后将密钥发送到 fileButton
  • 我没有尝试上述步骤,但在我的脚本中发现了问题,即在错误文件中使用了 _dirname。量角器在应用程序中创建实例并显示正确的文件名,但是在单击“上传”按钮后文件没有上传,因为它无法在错误的位置找到文件。所以我 console.log 并能够找到错误的路径,纠正它并繁荣!它奏效了。
  • 很高兴你能解决它。

标签: typescript file-upload protractor


【解决方案1】:

试试这个:

    const path = require('path');
    const fileToUpload = '../image/dummy.jpg'; // image folder is present in e2e folder of angular project
    const absolutePath = path.resolve(__dirname, fileToUpload);
    const fileElem = element(by.css('input[type="file"]'));
    fileElem.sendKeys(absolutePath);

【讨论】:

  • 这个和上面一样,只是定义css的方式不同。我也试过你的方法,但它不起作用。
【解决方案2】:

可能,如果您在远程机器上运行(例如 selenium grid 或 selenoid,或在容器内),您将需要使用 FileDetector,我在这里准备了一个小脚本来演示:

https://gist.github.com/Xotabu4/13c81465e959974429e2bba9a3408b71

此代码实际上会通过 http 将您的文件发送到浏览器所在的机器

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-06-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-01-17
    • 1970-01-01
    相关资源
    最近更新 更多