File Upload using Sikuli in Selenium





File Upload using Sikuli in Selenium will discuss about how we can upload a file into web application while automating the application using selenium webdriver. As we know that selenium webdriver automates only browsers (i.e. web applications only). But sometimes we might need to automate windows based applications/popups such as upload a file or download a file etc…

But selenium can not handle these kind of scenarios. So, we will use some third party tools to automate these type of functionality. Especially uploading a file is one of the scenarios we will face while automating most of the applications. For this we will depend mostly some third party tools like AutoIt and Sikuli.

In this blog we will look into Sikuli to automate file upload functionality in selenium webdriver. “Sikuli automates anything you see on the screen. It uses image recognition to identify and control GUI components. It is useful when there is no easy access to a GUI’s internal or source code.”.

To work with Sikuli, need to download “sikuli-setup.jar” and run the same to get the “sikuli-java.jar” then we can use this jar file in our project to work with Sikuli.




Below are the steps to to download and get the sikuli jar:

1. Download the sikuli setup jar file from here.

2. Double click on the jar file. Then you will find the below screen.

sikuli home screen

Check the check boxes of 4th and 6th options and click Setup Now button.

3. Now you can find the below screen

Sikuli want to preceed screen

 

Click Yes to proceed further.

4. Then you can see the below screen

Sikuli confirmation window

Click OK to complete the installation.

5. Once after completing the above procedure then you can able to see the “sikuli-java.jar” file in the same location where the “sikuli-setup.jar” exist.




By following the above procedure we will get the sikuli-java.jar file. Then we have to add this jar file to our selenium project to work with sikuli in the selenium code.

As we discussed in the above paragraphs, sikuli automates using image recognition. So, we need to take the screenshots/images of the elements which we want to automate using sikuli. In the file upload functionality usually we will automate the things till the window pop-up comes using selenium code. So on the window pop-up we need to identify two things. One is File Name input box and Open button. Once we capture these two images we need to place the same in our project folder for the best practice.

Apart from all these things, we need to use two of the classes in Sikuli to automate the window based objects. One is Screen Class, this is a representation of the physical monitor where the capture process happens. Second is Pattern class, this is used to associate an image file with additional attributes used in find operations and when acting on a match object.

In simple words, Screen holds the entire screen and Pattern is a part of screen which we captured and stored in the project folder. When you use Pattern object then it will verify where this kind of part is on the screen and once it identifies then it will interact with that object.

Below are the sample captures of the File Name input box and Open Button.

FileOpenInput

OpenButton

And then, below is the sample script for File Upload functionality using Sikuli.

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.sikuli.script.Pattern;
import org.sikuli.script.Screen;
import org.testng.annotations.Test;

public class FileUploadWithSikuli
{
    @Test
    public void fileUpload() throws Exception
    {
        System.setProperty("webdriver.gecko.driver","D:\\Selenium\\geckodriver.exe");
        WebDriver driver = new FirefoxDriver();
        driver.manage().window().maximize();

        driver.get("http://demo.automationtesting.in/Register.html");
        driver.findElement(By.id("imagesrc")).click();

        Thread.sleep(5000);

        Pattern fileNameInput = new Pattern("D:\\Selenium\\FileOpenInput.PNG");
        Pattern openButton = new Pattern("D:\\Selenium\\OpenButton.PNG");

        Screen screen = new Screen();
        screen.type(fileNameInput, "D:\\Selenium\\OpenButton.PNG");
        screen.click(openButton);
    }
}

This way we can upload a file in selenium using Sikuli.

Please watch the youtube video for better understanding.