简体   繁体   English

使用量角器在测试 E2E 中上传文件

[英]Upload file in test E2E with Protractor

In my test, I have a button which is used to browse my disc and select a file.在我的测试中,我有一个按钮用于浏览我的光盘并选择一个文件。

I want to use function(el: WebElement) , but I have an error:我想使用function(el: WebElement) ,但出现错误:

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

SyntaxError: Cannot use import statement outside a module语法错误:不能在模块外使用导入语句

My code in fic_test.js :我在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);
        });

Files in my project :我的项目中的文件:

  • conf/conf.js conf/conf.js
  • tests/fic_test.js测试/fic_test.js

I think you sendKeys to the element with input tag, at least, that's the only way it worked for me, and from your xpath I don't think you're sending fpath to the right element.我认为您使用 input 标签将 keys 发送到元素,至少,这是它对我有用的唯一方法,并且从您的 xpath 我不认为您将 fpath 发送到正确的元素。 Also, take a look at my reply to a similar post , it may be of help另外,看看我对类似帖子的回复,可能会有所帮助

UPDATE in answer to your comment: Sorry, I used this code where I work, so sharing the whole of it is a no go.更新以回答您的评论:抱歉,我在我工作的地方使用了此代码,因此无法共享整个代码。 This is how I'd write the test you mentioned in your question:这就是我编写您在问题中提到的测试的方式:

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);
});

I found no real need to scroll the page to the element being used, since its presence was the only thing protractor needed... hope it helps :)我发现没有真正需要将页面滚动到正在使用的元素,因为它的存在是量角器唯一需要的东西......希望它有帮助:)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM