Upload a file using Selenide

Tutorial 15​​ ​​ Upload a​​ file​​ using​​ Selenide​​ 

What you will Learn​​ in this​​ blog:

  • Upload​​ a​​ file​​ (example 1)

  • Upload a file (example 2)

  • Code snippets

Upload a file (example 1)

Let us​​ now​​ see​​ how to​​ upload an existing​​ file using​​ Selenide.

We will​​ upload a​​ test​​ file​​ from our local machine​​ to​​ the below site

https://the-internet.herokuapp.com/upload​​ 

First we will​​ choose a file and click ‘Upload’.

Inspect ‘Choose File’ button

There is a ‘uploadFile’ method that​​ comes handy

Create a new file under src/test

We can now menion the relative file path

After setting the input file, let us give some wait time and than click ‘Upload’ button

We finally assert the text

The entire script looks like below:

Save​​ and execute.

Observe below that the file gets uploaded

Assertion is pass

 

Upload a file (example 2)

Go to​​ https://practice.automationbro.com/cart/​​ 

Inspect ‘Select File’ and ‘Upload File’ buttons

Once the file is uploaded, we get the below message

 

The logic would remain same

Execute, the assertion is pass

Let​​ us fail the assertion by making a typo in the file name

Execute, assertion fails

This is show​​ we can upload the file using Selenide.

Code snippet​​ (example 1)

package​​ w2a.selenide;

 

import​​ static​​ com.codeborne.selenide.Selenide.$;

import​​ static​​ com.codeborne.selenide.Selenide.open;

 

import​​ java.io.File;

import​​ java.net.MalformedURLException;

 

import​​ org.openqa.selenium.By;

import​​ org.testng.Assert;

import​​ org.testng.annotations.Test;

 

import​​ com.codeborne.selenide.Configuration;

 

public​​ class​​ ElementsTest {

@Test

public​​ void​​ searchTest()​​ throws​​ InterruptedException, MalformedURLException {

 Configuration.browser​​ =​​ "firefox";

open("https://the-internet.herokuapp.com/upload");

$(By.cssSelector("#file-upload")).uploadFile(new​​ File("src\\test\\test.pdf"));

 Thread.sleep(3000);

//Click upload

$(By.cssSelector("#file-submit")).click();

 String​​ actual_text​​ =​​ $(By.cssSelector("h3")).getText();

 Assert.assertEquals(actual_text,​​ "File Uploaded!");

 Thread.sleep(2000);

 }

}

 

Code snippet (example 2)

package​​ w2a.selenide;

 

import​​ static​​ com.codeborne.selenide.Selenide.$;

import​​ static​​ com.codeborne.selenide.Selenide.open;

 

import​​ java.io.File;

import​​ java.net.MalformedURLException;

 

import​​ org.openqa.selenium.By;

import​​ org.testng.Assert;

import​​ org.testng.annotations.Test;

 

import​​ com.codeborne.selenide.Configuration;

 

public​​ class​​ ElementsTest {

@Test

public​​ void​​ searchTest()​​ throws​​ InterruptedException, MalformedURLException {

 Configuration.browser​​ =​​ "firefox";

open("https://practice.automationbro.com/cart/");

$(By.cssSelector("#upfile_1")).uploadFile(new​​ File("src\\test\\test.pdf"));

 Thread.sleep(3000);

//Click upload

$(By.cssSelector("#upload_1")).click();

 Thread.sleep(5000);

 String​​ actual_text​​ =​​ $(By.cssSelector("#wfu_messageblock_header_1_label_1")).getText();

 Assert.assertEquals(actual_text,​​ "File test.pdf uploaded successfully");

 }

}

 

Thank you for reading!

Share On

Share on facebook
Share on twitter
Share on linkedin
Share on whatsapp
Share on tumblr
Share on email

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top

Lifetime Membership Club

LIFETIME MEMBERSHIP BIG SALE - ALL LIVE COURES JUST - 10000 RS/149 USD
Attend All Live courses in just 10000 rs / $149 - offer ends 25th April 2024