magic_test
magic_test copied to clipboard
Track attach file
Is there a way to track an attach file action?
If no we can start the discussion on how to do that
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
}
})
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:

Considerations:
- I assume that the file is in
spec/fixtures
folder. I think this should be configurable and use that configuration value on the partial. -
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. - I'm using element.id, should be check other attribute too?
Thoughts?
@faqndo97:
- I assume that the file is in
spec/fixtures
folder. I think this should be configurable and use that configuration value on the partial.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.- I'm using element.id, should be check other attribute too?
- 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 inspec/fixtures
. Is that what you're suggesting as a default (to be configurable?) -
element.files
always returns an array. Taking the first element like you're doing is legit, but if we'd like to makemagic_test
robust to selecting multiple files, I think we should? - 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?