How to select image using Selenium Web driver.

Reading Time: 2 minutes

In this blog ,we can select images using Selenium WebDriver. Selenium WebDriver is a web framework that permits us to execute cross-browser tests.Also, this tool is used for automating web-based application testing to verify that it performs expectedly.

Browser Drivers in Selenium

In order to develop a secure connection with the browser, Selenium uses Drivers. Each browser has its own driver that hides the internal logic of the browser’s functionality.

In addition, each automation language has a corresponding browser driver. The following series of actions are performed when a Selenium automation test is triggered:

  • Every Selenium command generates a corresponding HTTP request, which is sent to the browser driver.
  • This request is routed through the HTTP Server.
  • The HTTP Server now drives the instruction execution on the browser.
  • The browser sends back the status to the HTTP Server, which forwards it to the automation script.

Some of the browser drivers are ChromeDriver – For chrome Browser, GeckoDriver – For FireFox Browser, MicrosoftEdge driver – For Edge Browser, etc.

Also, In this blog we will use ChromeDriver.

Select an Image through Selenium Webdriver

For this ,we will use https://the-internet.herokuapp.com/upload to select the image.This site undoubtedly allows any visitor to upload files without requiring them to sign up.

Uploading files in WebDriver is done by simply using the sendKeys() method on the file-select input field to enter the path to the file to be uploaded.

Here, “No file chosen” is the file selection input box in upload site.

Furthermore ,After inspecting choose file we got the input id that we use in our code.

Code:

import org.openqa.selenium.*;

import org.openqa.selenium.chrome.ChromeDriver;

import org.testng.Assert;

import org.testng.annotations.Test;

public class Image {

    WebDriver driver;

    @Test

    public void setup() {

            System.setProperty("webdriver.chrome.driver", "drivers/chromedriver");

        driver = new ChromeDriver();

        driver.manage().window().maximize();

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

        WebElement add = driver.findElement(By.id("file-upload"));

        add.sendKeys("/home/knoldus/Downloads/Image/image/1.jpeg");

        WebElement logo = driver.findElement(By.id("drag-drop-upload"));

        WebElement upload = driver.findElement(By.id("file-submit"));

        upload.click();

        WebElement errorMessage = driver.findElement(By.cssSelector("h3"));

        String expectedErrorMessage = "File Uploaded!";

        String actualErrorMessage = errorMessage.getText();

        Assert.assertTrue(actualErrorMessage.contains(expectedErrorMessage), "Actual error message does not contain expected. \nActual: " + actualErrorMessage + "\nExpected: " + expectedErrorMessage);

        driver.quit();
    }
}

Furthermore, After running this script, we will be able to upload the image successfully .Here,1.jpeg is selected as a file.

After clicking on Upload we got the message File Uploaded!

Reference:

https://www.selenium.dev/documentation/webdriver/actions_api/keyboard/

Please do check out our other blogs on a similar tech stack,

https://blog.knoldus.com/category/tech-blogs/test-automation/.

Written by 

Sakshi Sneha is Software Consultant in Knoldus Inc. She is always eager to learn new and advanced concepts in order to upskill herself.

Leave a Reply