【问题标题】:Upload file in test E2E with Protractor使用量角器在测试 E2E 中上传文件
【发布时间】:2020-02-13 03:00:36
【问题描述】:

在我的测试中,我有一个按钮,用于浏览我的光盘并选择一个文件。

我想使用function(el: WebElement),但出现错误:

import { ElementFinder, browser, by, element } from 'protractor';

SyntaxError: 不能在模块外使用 import 语句

我在 fic_test.js 中的代码:

it("Upload file",function(){
        let filePath = "../../../PJ/a.jpg";
        let fpath = path.resolve(__dirname,filePath);     
        browser.get("...");
        browser.sleep(5000);
        browser.findElement(by.xpath('//button[contains(.," Télécharger un autre document ")]')).then(function(el: WebElement){
            browser.executeScript("arguments[0].scrollIntoView(true);",el);
            el.sendKeys(fpath);
        });

我的项目中的文件:

  • conf/conf.js
  • tests/fic_test.js

【问题讨论】:

    标签: angular protractor node-modules


    【解决方案1】:

    我认为您将密钥发送到带有输入标签的元素,至少,这是它对我有用的唯一方法,并且从您的 xpath 我不认为您将 fpath 发送到正确的元素。 另外,看看my reply to a similar post,可能会有帮助

    更新回答您的评论: 抱歉,我在工作的地方使用了这段代码,所以分享整个代码是不行的。 这就是我如何编写您在问题中提到的测试:

    const EC = ExpectedConditions;
    
    it('Upload file', () => {
     const filePath = '../../../PJ/a.jpg';
     const absolutePath = require('path').resolve(__dirname, filePath);
      browser.get("...");
      // maybe add here a little wait
      browser.wait(EC.presenceOf($('cssSelector-of-ur-input-el')), 5000, 'ERROR: Input el. not found');
      $('cssSelector-of-ur-input-el').sendKeys(absolutePath);
    });
    

    我发现没有真正需要将页面滚动到正在使用的元素,因为它的存在是量角器唯一需要的东西......希望它有所帮助:)

    【讨论】:

    • 我在另一篇文章中看到了你的答案,但它仍然是 KO。你能展示你的完整代码吗?用于测试
    猜你喜欢
    • 1970-01-01
    • 2014-02-13
    • 2016-07-11
    • 1970-01-01
    • 1970-01-01
    • 2014-03-23
    • 1970-01-01
    • 2016-07-28
    • 2015-05-07
    相关资源
    最近更新 更多