How to Handle File Upload Window in Selenium Webdriver Java

What is Robot Class in Selenium Webdriver?

Selenium can't interact with Window dialogs on it's own.

You all must know that Selenium can only handle browser windows and not OS windows. So, when we have to upload any file on a webpage and as soon as we click on the Browse or Upload button, a windows tab opens up from which we have to select the file. We cannot handle that windows tab directly with Selenium Webdriver.

But we can use other libraries and integrate them with Selenium code to handle window based dialogs. Robot class is one of them. We can use Robot class to upload or download a file using selenium.

What is Robot Class in Selenium Webdriver?

Robot Class is used to perform Keyboard actions. It has direct support to perform actions like press and release key. Robot class has key codes for each key present on the keyboard. So, if we want to press any key, then we need to call that key code.

In automation we make use of Robot class to handle Windows tabs by interacting with them via virtual keyboard. One important thing to keep in mind that, in robot class keyPress and keyRelease functions come in a combo. So, if you are pressing a key then you have to call release key function after that.

How can we use Robot class in Selenium to handle window dialogs?

  • How to Upload a file in Selenium Webdriver
  • How to Download a file in Selenium Webdriver

Scenario 1 - Handle Upload File Window dialog and select the file to be uploaded

Test Steps:

  1. Go to File Upload Page.
  2. Click on the Upload file button.
  3. Select the file or copy the path of the file. (Windows dialog)
  4. And click on 'Ok' button. (Windows dialog)

From above steps we can execute first two steps using selenium and will perform 3rd and 4th step using Robot class.

Code to upload image using Robot Class:

                    package                    com.techlistic.selenium;                    import                    java.awt.AWTException;                    import                    java.awt.Robot;                    import                    java.awt.Toolkit;                    import                    java.awt.datatransfer.StringSelection;                    import                    java.awt.event.KeyEvent;                    import                    org.openqa.selenium.By;                    import                    org.openqa.selenium.WebDriver;                    import                    org.openqa.selenium.firefox.FirefoxDriver;                    public                    class                    FileUpload                    {                    public                    static                    void                    main(String[] args) {                    // Launch Browser                    WebDriver driver =                    new                    FirefoxDriver();                    // Maximize Window                    driver.manage().window().maximize();                    // Open URL                                        driver.get("https://www.techlistic.com/p/selenium-practice-form.html");                    // Click on Browse button                    driver.findElement(By.className("browse")).click();                    // Wait for 3 seconds                    try                    { Thread.sleep(3000);}                    catch                    (InterruptedException e){ e.printStackTrace();}                    // Get Location of the file to be uploaded                                        String fileLocation = System.getProperty("user.dir") +                    "\\test data\\"                    +                    "sample-upload-file.docx";   StringSelection filepath =                    new                    StringSelection(fileLocation);                    // Copy the file path                    Toolkit.getDefaultToolkit().getSystemClipboard().setContents(filepath,                    null);                    // Try block                    try                    {                    // Create object of Robot class                    Robot robot =                    new                    Robot();                    // Press Ctrl key                    robot.keyPress(KeyEvent.VK_CONTROL);                    // Press Ctrl + V key - It will paste the file path in windows dialog                                        robot.keyPress(KeyEvent.VK_V);                    // Now release V + Ctrl key                    robot.keyRelease(KeyEvent.VK_V);    robot.keyRelease(KeyEvent.VK_CONTROL);                    // Click Enter Key                                        robot.keyPress(KeyEvent.VK_ENTER);                    // Release Enter Key                    robot.keyRelease(KeyEvent.VK_ENTER);    }                    catch                    (AWTException e) {e.printStackTrace();}  }                  

Find Broken Links with Selenium  << Previous      ||      Next >>  Hanlde Dynamic Web Table in Selenium

Refer Selenium Webdriver Tutorials Series

Popular posts from this blog

10 Best Demo Websites for Selenium Automation Testing Practice

Image

This tutorial will certainly help you if you are learning Selenium and want to enhance your automation skills by practicing automation code. This post is specially designed for those who want to learn or  learning Selenium Webdriver. In this post, I cherry picked links of some of the best websites to practice Selenium Automation. You'll find Urls of best selenium practice websites on this tutorial like., Automation Practice form, demo web table, dummy e-commerce website, website for handling alerts, dummy travel portal etc. You can also take a look at  Selenium Practice Assignments which would help you practice your automation skills on the the shared demo website links. 1.  AUTOMATION PRACTICE FORM Level - Beginner About - This form contains all the important form elements which we come across daily like text box, radio button, check box, select drop downs, multi-select box, button, links, File Upload, Download link. Selenium Commands - To automate this practice form

Automate an e-Commerce website with Selenium Webdriver

Image

In this post you will learn to automate different functionalities of e-commerce website. This assignment is rated as 'Pro' level. We will cover multiple test scenarios of e-commerce website in this assignment like., User Registration, Buy Product,  Search Product and other website filters using Selenium Webdriver.  Along with test assignments, url of the demo e-commerce website is also provided. You can use this demo e-com website for your automation practice. Table of Content 1. Automate User Registration of an e-Commerce Website 2. Automate 'Login' with Selenium of an e-Commerce Website 3. Automate Buy Product functionality of an e-Commerce Website 4. Automate Search Product functionality of an e-Commerce Website Assignment 1  - Automate 'User Registration' of an e-commerce website with Selenium This section is focused on covering all the scenarios for User Registrati

Automate Browser Actions and Menu links with Selenium Webdriver

Image

This post contains two assignments which helps you to learn selenium  browser commands. These are the simplest assignments. So, this assignment post is the best suited to start your automation tester journey. 1. Automate Browser Actions on GoDaddy.com with Selenium If you are a beginner in automation testing and looking forward to write your first Selenium code, then this post is the best destination for you. This assignment will teach you the basic commands of Selenium Webdriver to perform basic actions like., Launch browser, maximize browser window, validate page title and close browser. In this post, you will find very basic step by step assignments which will upgrade your understanding about Selenium Webdriver and it's commands. Assignment Level - Beginner Test Case 1 - Open Godaddy.com and maximize browser window. Steps to Automate: 1. Launch browser of your choice say., Firefox, chrome etc. 2. Open this URL -  https://www.godaddy.com/ 3. Maximize or set size of browser window

Top 25 Selenium Webdriver Commands for Test Automation

Image

In this tutorial you will learn about basic Selenium Webdriver Commands which would be the backbone of your GUI automation scripts. We have divided the commands in to different categories so that you can grasp them without difficulty. Table of Content 1. Selenium Browser Commands 2. Get Commands 3. Navigation Commands 4. Web Element Commands 5. Select Dropdown Commands 1. Selenium Browser Commands Browser commands are the starting point of your Selenium Webdriver script. These commands used to launch browser, maximize it, open URL to be tested and other navigation commands. i. Set Path of browser/driver executable: It is the starting point of the webdriver script.  You have to download the browser executable file for the browser and make sure it is compatible with the version of your browser. For example, for firefox you have to download geckodriver.exe and place in your project. Download geckodriver executable from this link -  https://github.com/mozilla/geckodriver/releases Si

Top 50 Selenium Webdriver Interview Questions

Image

This blog helps you prepare for Selenium automation interview and brush up your selenium skills easily.  You can find Selenium Webdriver interview questions for Experienced Professionals to beginners on this post. Selenium interview questions are divided into different topics for sake of ease. Table of Contents 1. Selenium Webdriver Commands Interview Questions 2. Actions Class Interview Questions 3. Selenium Locators Interview Questions 4. Windows and Frame Handling Interview Questions 5. Advanced Selenium Interview Questions 6. Selenium with TestNG Interview Questions 7. OOPS, Selenium Automation Framework and Project related Interview Questions 1. Selenium Webdriver Commands Interview Questions Q1 - What is the syntax to launch browser in Selenium Webdriver? We can launch browser by creating an object of WebDriver interface and initialize it with some browser class like., ChromeDriver(), FirefoxDriver() etc. Please see the syntax for different browsers, // Set Path of driver

Handle Multiple Browser Tabs with Selenium Webdriver

Image

In this tutorial, we will learn to handle multiple browser tabs within a single Selenium script. Selenium Webdriver does not provide any commands for this task. But we can make use of the existing Selenium commands in a smarter way to automate this scenario. Although this scenario is quite rare and you will not encounter it usually. How to use Actions Class in Selenium to Handle Multiple Browser Tabs Actions Class in Selenium is a special class which provides us very useful commands, which helps us to replicate Keyboard and Mouse events i.e., press/release any Key, move curson, drag and drop, right click, double click etc. You can learn about Actions Class in detail here .  In our scenario, we'll make use of Key control commands of Actions class to automate open and automate multiple browsers. Let's understand the whole code logic step by step. 1. Launch google.com with Selenium Webdriver This is the most common part, we'll launch google.com with Selenium Webdriver as

How to Handle File Upload Window in Selenium Webdriver Java

Source: https://www.techlistic.com/2019/06/upload-file-using-selenium-webdriver.html

Related Posts

0 Response to "How to Handle File Upload Window in Selenium Webdriver Java"

Post a Comment

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel