user8750496
user8750496

Reputation: 25

Ruby Selenium Webdriver: Close file dialog on Mac Chrome

Here is HTML for me to upload an image for scanning:

<div class="capture_group">
  <div class="row wrapper capture_container">
    <input type="file" accept="image/*" capture="environment" id="camera" class="hidden" value="">
</div>
  <div class="wrapper column capture_controls">
    <label for="camera" class="btn">
      <p class="buttonBgText">Capture Passport Front</p>
    </label>
    <label for="camera" class="btn">
      <p class="buttonBgText">Capture Passport Back</p>
    </label>
  </div>
</div>

The <input> doesn't detect image even I work on it manually so I'm ignoring this element in my test. Both <label>s trigger file dialog for me to upload file and WLOG I'm using the last one to upload an image. And one restriction is that there is no button for me to submit and trigger the scan, even I'm able to upload an image (due to this restriction I couldn't test it with cypress cypress-file-upload which I could only upload but no way to go next).

At the moment the only way I could upload and go to the next page is using attach_file.

page.attach_file('/path/to/file.png') do
  page.find(:xpath, "//label[@for='camera'][2]").click
end

This is not working for me as I observe it just upload no matter what element I place into the block, and by default scan through the non-working <input>

page.find(:xpath, "//label[@for='camera'][2]").click
page.attach_file('/path/to/file.png')

This works for me for uploading the image and scan through the second <label>, however the click action triggers the file dialog which I can't close it...

I would like to see if there is any script I could close the file dialog after scanning the image successfully. Thanks.

Upvotes: 0

Views: 240

Answers (1)

Thomas Walpole
Thomas Walpole

Reputation: 49890

The file selection dialog is a system dialog so once it's open the browser no longer has control over it, so if you let it open that's pretty much it and you're out of luck.

It's difficult to understand the issue you're having with attach_file and why that's not working for you. The block accepting version of attach_file expect you to perform the action that would normally open the file selection box, and then attaches the specified file to the <input type="file"> element that would have been attached to if the file selection box had opened. As can be seen from your HTML both of your labels are connected to the same file input element (the for attribute for both specify the 'camera' id). This means in order to be able to add both a front and back image of the passport on the same form you would need to have some JS that triggers on the file inputs change event and copies the attached file to some other storage location. Assuming you have that, and that I'm understanding how your form is supposed to work, then doing something like

page.attach_file('/front_passport.jpg') do
  page.find(:label, 'Capture Passport Front').click
end

# An assertion to check that the file attachment has completed
# page.assert_text "Got front of passport" - minitest
# expect(page).to have_text("Got front of passport") - rspec
# Assert for whatever actually changes on the page when the front is added

page.attach_file('/back_passport.jpg') do
  page.find(:label, 'Capture Passport Back').click
end

should work. If it doesn't you're going to need to explain in a lot more detail exactly what your page is doing.

Upvotes: 1

Related Questions