Hoe een bestand te uploaden in angularjs e2e gradenboogtest

Ik wil het uploaden van bestanden testen met een angularjs e2e-test. Hoe doe je dit in e2e testen? Ik voer mijn testscript door grunt karma.


Antwoord 1, autoriteit 100%

Dit is hoe ik het doe:

var path = require('path');
it('should upload a file', function() {
  var fileToUpload = '../some/path/foo.txt',
      absolutePath = path.resolve(__dirname, fileToUpload);
  element(by.css('input[type="file"]')).sendKeys(absolutePath);    
  element(by.id('uploadButton')).click();
});
  1. Gebruik de module pathom het volledige pad op te lossen van het bestand dat u wilt uploaden.
  2. Stel het pad naar het element input type=”file”in.
  3. Klik op de uploadknop.

Dit werkt niet in Firefox. Gradenboog zal klagen omdat het element niet zichtbaar is. Om te uploaden in Firefox moet je de invoer zichtbaar maken. Dit is wat ik doe:

browser.executeAsyncScript(function(callback) {
  // You can use any other selector
  document.querySelectorAll('#input-file-element')[0]
      .style.display = 'inline';
  callback();
});
// Now you can upload.
$('input[type="file"]').sendKeys(absolutePath);    
$('#uploadButton').click();

Antwoord 2, autoriteit 11%

U kunt niet rechtstreeks.

Om veiligheidsredenen kunt u een gebruiker niet simuleren die een bestand op het systeem kiest binnen een functionele testsuite zoals ngScenario.

Met Protractor, omdat het is gebaseerd op WebDriver, zou het mogelijk moeten zijn om deze truc

V: Ondersteunt WebDriver bestandsuploads? A: Ja.

U kunt niet rechtstreeks communiceren met het native OS-bestandsbrowserdialoogvenster,
maar we doen wat magie zodat als je belt
WebElement#sendKeys(“/path/to/file”) op een bestanduploadelement, wel
het juiste ding. Zorg ervoor dat u niet WebElement#klik() op het bestand
upload-element, anders loopt de browser waarschijnlijk vast.

Dit werkt prima:

$('input[type="file"]').sendKeys("/file/path")

Antwoord 3, autoriteit 3%

Hier is een combinatie van het advies van Andres D en davidb583 die me zou hebben geholpen tijdens het verwerken…

Ik probeerde gradenboogtests uit te laten voeren tegen de flowjs-besturingselementen.

   // requires an absolute path
    var fileToUpload = './testPackages/' + packageName + '/' + fileName;
    var absolutePath = path.resolve(__dirname, fileToUpload);
    // Find the file input element
    var fileElem = element(by.css('input[type="file"]'));
    // Need to unhide flowjs's secret file uploader
    browser.executeScript(
      "arguments[0].style.visibility = 'visible'; arguments[0].style.height = '1px'; arguments[0].style.width = '1px'; arguments[0].style.opacity = 1",
      fileElem.getWebElement());
    // Sending the keystrokes will ultimately submit the request.  No need to simulate the click
    fileElem.sendKeys(absolutePath);
    // Not sure how to wait for the upload and response to return first
    // I need this since I have a test that looks at the results after upload
    //  ...  there is probably a better way to do this, but I punted
    browser.sleep(1000);

Antwoord 4

var imagePath = 'http://placehold.it/120x120&text=image1';
element(by.id('fileUpload')).sendKeys(imagePath);

Dit werkt voor mij.

Other episodes