0

I use Selenium to test web app behaviour on file upload page. The goal of test is to check application validations on file selection.

I use webDriver.SendKeys (C#) on file input to simulate file upload, file is selected successfully, but fileInput.files[0].size (js) is always 0 (or undefined?) and app shows error "Do not upload empty files". File size is correct on manual testing.

Any suggestions, on how to solve the problem?

UPDATE Code details:

HTML

<input id="file-path" type="file" data-bind="attr: { accept: mimeTypes }, event: { change: onFileSelect }" />
<small id="file-path-error" class="field-validation-error" data-bind="validationMessage: file"></small>

JS (file edit model uses knockout)

this.file = ko.observable().extend({
    validation: [
      {
        validator: function(file) {
          // valid if no file or file size < 5Mb
          return !file || file.size < 5242880;
        },
        message: 'File size should be less than 5Mb'
      }, {
        validator: function(file) {
          // valid if no file or file size > 0
          return !file || file.size > 0;
        },
        message: 'Do not upload empty files'
      }
    ]
  });
this.onFileSelect = function(model, event) {
    var fileInput;    
    fileInput = event.target;
    if ((fileInput.files != null) && fileInput.files.length > 0) {
      _this.file(fileInput.files[0]);          
    } else {
      _this.file(null);
    }
  };

C#

webDriver.FindElement(By.Id("file-path")).SendKeys("C:\testFile.txt");
Assert.AreEqual(webDriver.FindElement(By.Id("file-path-error").Text,"");

Assertion always fails because file-path-error contains "Do not upload empty files" - that is the problem.

blazkovicz
  • 732
  • 8
  • 18

1 Answers1

0

I was misleaded by answers on How to automate file upload in Internet Explorer using Selenium?

Discusstion on How to set a value to a file input in HTML? says that actually we can not upload files programmatically.

So there was no sence in expecting decent behaviour for such test.

Community
  • 1
  • 1
blazkovicz
  • 732
  • 8
  • 18