magic_test icon indicating copy to clipboard operation
magic_test copied to clipboard

Track attach file

Open faqndo97 opened this issue 2 years ago • 3 comments

Is there a way to track an attach file action?

If no we can start the discussion on how to do that

faqndo97 avatar Apr 23 '22 22:04 faqndo97

I'm thinking catching the input event on <input type="file">?

document.addEventListener('input', (event) => {
  const el = event.currentTarget
  if (el.tagName.toLowerCase() === 'input' && el.type === 'file) {
    // do something
  }
})

pascallaliberte avatar Apr 23 '22 23:04 pascallaliberte

Thanks for the idea @pascallaliberte!

I have something working for this:

  document.addEventListener('input', function(event){
    var element = event.target
    var tagName = element.tagName
    var action = ""
    var target = ""
    var options = ""
    if (tagName === 'INPUT' && element.type === 'file') {
      var fileName = element.files[0].name;
      target = element.id
      if (!target) return;
      action = `attach_file '${target}', Rails.root.join('spec/fixtures/${fileName}')`
    }
    var testingOutput = JSON.parse(sessionStorage.getItem("testingOutput"));
    testingOutput.push({action: action, target: target, options: options });
    sessionStorage.setItem("testingOutput", JSON.stringify(testingOutput));
  });

and I tried and seems to be working well:

Screen Shot 2022-04-24 at 9 42 34 PM

Considerations:

  1. I assume that the file is in spec/fixtures folder. I think this should be configurable and use that configuration value on the partial.
  2. element.files can return multiple files, but IDK a scenario for that, I think that the default scenario of upload a file will contain only one and then we can assume that "take the first element" that I'm doing in my code.
  3. I'm using element.id, should be check other attribute too?

Thoughts?

faqndo97 avatar Apr 25 '22 00:04 faqndo97

@faqndo97:

  1. I assume that the file is in spec/fixtures folder. I think this should be configurable and use that configuration value on the partial.
  2. element.files can return multiple files, but IDK a scenario for that, I think that the default scenario of upload a file will contain only one and then we can assume that "take the first element" that I'm doing in my code.
  3. I'm using element.id, should be check other attribute too?
  1. I don't understand the thing about spec/fixtures. Would that mean that upon capturing the user interaction that selects the files for upload, we would store the files in spec/fixtures. Is that what you're suggesting as a default (to be configurable?)
  2. element.files always returns an array. Taking the first element like you're doing is legit, but if we'd like to make magic_test robust to selecting multiple files, I think we should?
  3. I think we can rely on element.id, but I think magic_test might prefer the input's label as the identifier. Or maybe the xpath.

A lot of time has passed since you asked this question. Sorry for the delay. Where are you at right now with this?

pascallaliberte avatar Jul 05 '22 18:07 pascallaliberte