Urriel
Urriel

Reputation: 11

Protractor Zip file uploading

I have been working on a protractor script to upload files.

function upload(params) {
   return _waitForElm(params.xpath)
      .then(() => {
          var absPath = path.resolve(__dirname, '../../../assets/' + params.filePath.match(/[^\\\/]+$/)[0]);
          return $(params.xpath).sendKeys(absPath);
      }).catch(err => {
          console.error(jasmine.getEnv().currentSpec.description, 'Upload file', params.xpath);
          throw err;
      });
}

params.xpath is actually a valid CSS selector (old variable name).

/**
* Wait for an element to be present and scroll to it if not displayed.
*
* @param xpath
* @returns {Promise<U>|IPromise<U>}
*/
function waitForElm(xpath) {
  return browser.wait(() => {
    return element.all(by.css(xpath)).then(items => {
        return items.length > 0;
    });
}, 10000)
    .then(() => {
        return $(xpath).isDisplayed().then(isDisplayed => {
            if (!isDisplayed) {
                return _scrollToElm(xpath);
            }
        });
    });
}

/**
* Scroll to the element in the page.
*
* @param xpath
* @returns {Promise<U>|IPromise<U>}
* @private
*/
function _scrollToElm(xpath) {
   let elm = $(xpath);
   return browser.executeScript('arguments[0].scrollIntoView();', elm.getWebElement());
}

The upload function work perfectly for my other files (jpg, png and PDF) but does not upload zip files and does not throw any exception when failing. Also the input file is accepting zip files since I can do it manually.

If you have encounter any issue like this one, I would be grateful to get any advice.

Upvotes: 1

Views: 322

Answers (2)

Urriel
Urriel

Reputation: 11

Sorry for the late reply, the issue was in protractor. I have updated the package to the latest version and it worked.

Upvotes: 0

AdityaReddy
AdityaReddy

Reputation: 3645

Your code looks correct and I just checked uploading a ZIP file ,partially re-using your code and it looks good.

    var filePath = 'documents.zip';
    var absPath = path.resolve(__dirname, '../Resources/' + filePath.match(/[^\\\/]+$/)[0]);
    console.log(absPath); // C:\Users\<<user>>\Project\abc\Resources\documents.zip
    filespage.upload(absPath);

Upvotes: 1

Related Questions