[JAVA] Automatic file upload with old Ruby gem What to do with Watir

About this article

I was writing a script to upload a file to a website in Watir, It is a memorandum because I solved the problem that it was difficult to find information in the old environment.

environment

CentOS Linux 7  Ruby 2.0.0p648 Specifications gem is Watir The browser uses Firefox, but other browsers such as Chrome can also be used.

Thing you want to do

I want to upload a file to a website by starting an automatic script using Watir. On the upload screen, press the button called Select file, and when the file selection screen appears, select a file from it and upload it.

Stumble

The behavior of selecting a file from the upload screen is not working. When the OS standard file selection screen opens, the operation of the script does not reach whether it operates outside the browser.

What I tried

  1. Since javascript can be executed inside watir I tried to copy and paste using the clipboard etc., but in the first place the screen specifications are via the clipboard in the script It seems that the paste is not accepted, and the paste quits quietly without any error.
  2. Forcibly trigger a key event from javascript. I tried to force the Enger key and Tab key by executing the following command, but Is the browser itself in focus? It seems that the file selection screen does not accept key commands.
    browser.execute_script("
                document.dispatchEvent( new KeyboardEvent( 'keydown', { keyCode: 86 , ctrlKey: true , key: 'V' }) );
    ")

(Reference: https://ameblo.jp/personwritep/entry-12456996738.html) I tried such things as, but it does not perform operations such as going to read the target file.

Website specifications / screen image

The screen for uploading files is below, and you can press the Select file button.

image.png

Then, the following OS standard file selection screen appears, and it is designed to upload files from there. image.png

When operating by hand, you can select the file after pressing the Select file button, or you can upload the file by dragging and dropping.

done

As a result, it's done.

$browser.file_field(:id, //).set("filepath\/...\/file.png ")

Referenced articles

https://stackoverflow.com/questions/15163816/automating-a-file-upload-with-watir-in-chrome-on-osx https://www.rubydoc.info/gems/watir/1.8.1/Watir%2FContainer:file_field

One last word

No Watir.

Recommended Posts

Automatic file upload with old Ruby gem What to do with Watir
How to achieve file upload with Feign
I tried to implement file upload with Spring MVC
What to do when rails creates a 〇〇 2.rb file
File upload with Spring Boot (do not use Multipart File)
What to do if you installed Ruby with rbenv but the version does not change
What to do when you launch an application with rails
[Easy] How to automatically format Ruby erb file with vsCode
What happens to instance variables when copying an instance with ruby
iOS: File upload with SFTP
File upload with Spring Boot
[Ruby] What to do when the error "cannot load such file" appears when executing VS Code debug
What to do if an SSH key authentication error occurs during automatic deployment to EC2 with Capistrano
What to do if validation doesn't work with the update action
[Rails] What to do if you can't get parameters with form_with
How to realize huge file upload with Rest Template of Spring
What to do after Vagrant install
What to do when javax.batch.operations.JobStartException occurs
What to do if JavaMail cannot be used with OpenJDK 11 or later
[IOS] What to do when the image is filled with one color
What to do if changes are not reflected after automatic deployment to EC2
What to do when CentOS cannot be started with VirtualBox on Catalina
What you can do with ruby ​​Quick reference teaching materials (for beginners)
What to do when Blocked Host: "host name" appears in Ruby on Rails
What to do if deployment fails on Heroku (Ruby app not detected)
Notes on what to do when EC2 is set up with t2.micro
If you want to make a zip file with Ruby, it's rubyzip.