Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Track attach file #53

Open
faqndo97 opened this issue Apr 23, 2022 · 3 comments
Open

Track attach file #53

faqndo97 opened this issue Apr 23, 2022 · 3 comments

Comments

@faqndo97
Copy link

Is there a way to track an attach file action?

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

@pascallaliberte
Copy link
Member

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

@faqndo97
Copy link
Author

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?

@pascallaliberte
Copy link
Member

@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?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants