Upload file with Selenide

Dilpreet Johal
2 min readSep 12, 2022

--

In this post, we will take a look at how to upload file with Selenide. We will do that using 2 different ways -

  1. Upload file with visible input field
  2. Upload file with hidden input field

Upload file (visible input)

When the input field is visible and intractable, we can simply use the .uploadFile command to upload the file -

// upload file
$("#file-upload").uploadFile(new File("src/test/data/sample.png"));

In the code above, we are selecting the element with input[type=file] and then doing an upload on that.

Upload file (hidden input)

When the input field is hidden and not intractable, it makes it difficult sometimes to upload file. In this case, we need to do some Javascript DOM manipulation to make the input field visible and intractable again -

// execute js code
executeJavaScript("document.getElementById(\"upfile_1\").classList.remove(\"file_input_hidden\")");
// upload file
$("#upfile_1").uploadFile(new File("src/test/data/sample.png"));

In the code above, we are first executing Javascript code to remove the class that is making the input field hidden. Once the input field is visible, we are uploading the file using the standard way.

To learn more about how to upload file in Selenide, check out the video below –

📧 Subscribe to my mailing list to get access to more content like this as well as be part of amazing free giveaways.

👍 You can follow my content here as well -

I love coffees! And, if this post helped you out and you would like to support my work, you can do that by buying me a cup of coffee.

--

--

Dilpreet Johal
Dilpreet Johal

Written by Dilpreet Johal

SDET Architect | YouTuber | Tech Blogger | Love to explore new tools and technologies. Get access to all the courses— https://sdetunicorns.com/

Responses (1)