How To Download File In It File Type In Java
While Selenium testing you may have come across a requirement where you demand to either download or upload file in Selenium. Nearly every web-application over the internet may take a feature for allowing users to either download or upload a file. Be it a rich-media platform such as YouTube which lets you upload video files or online photograph collage maker, or an e-commerce spider web application which allows you to upload images. Even writing administration similar Grammarly and Plagiarism checker similar Quetext offer an uploading file functionality. Similarly, these websites offer downloading functionality too. YouTube allows offline downloading, e-commerce platforms such as Amazon volition let yous download the invoices of your orders. My point being is that if you are an automation tester who has a routine set up around Selenium testing, there is a good chance for y'all to run into a requirement where you may take to exam a feature around downloading or uploading files in Selenium WebDriver.
Tabular array OF CONTENT
- What Is A Remote Webdriver?
- Uploading files in Selenium WebDriver using Coffee with:
- Sendkeys()
- Robot Form
- AutoIT
- Jacob API
- Applied Sit-in for Uploading Files in Selenium WebDriver for:
- Local Infrastructure
- Online Selenium Grid
- Downloading files in Selenium WebDriver Using Java
- AutoIT
- Robot Form
- Browser Profile Setting
- Practical Demonstration for Downloading Files in Selenium WebDriver for:
- Local Infrastructure
- Online Selenium Filigree
In Selenium testing, it is very important to know how to Upload files in Selenium WebDriver or download files in Selenium WebDriver through automation testing with Selenium. In this Selenium Java tutorial, I am going to highlight different means through which you tin can download or upload files in Selenium WebDriver.
What Is A Remote WebDriver?
Remote WebDriver implements each command of JSONWireProtocol and users tin can perform locally and remotely on a remote server. All Browser Drivers are child class of RemoteWebDriver and RemoteWebDriver is a course type and implements all WebDriver interface. And then, RemoteWebDriver has the capability of Selenium testing on either local infrastructure or on a deject-based Selenium Grid such as LambdaTest.
Let'southward understand the real use case of uploading files in Selenium WebDriver. Suppose you lot are developing automation scripts for testing with Selenium & Coffee over an online clinical web platform where patients can book a video consultation with a doctor. On that website, there is an option to upload a Exam Report where a doctor can review and discuss test reports. In such a instance, you demand to use upload file concepts to upload reports to their clinical web application.
Note: If you have already implemented file uploading script in your local script and want to upgrade to a remote cloud-based environs then you lot need to just change WebDriver to RemoteWebDriver and use driver.setFileDetector(new LocalFileDetector());
method.
Upload Files in Selenium With Java
If you are familiar with Selenium i, Accessible web server and attachFile command were using upload files. And In Selenium two onward, It is similar but a sendkeys() control and you're done uploading a file. It is so easy to do that at present a day. When you want to upload files locally then you can straight use SendKey() and give a path in lawmaking. However, the same thing will not work remotely as did on locally. For uploading files in Selenium Remote WebDriver, you need to leverage the method called the setFileDetector method. That style, Remote WebDriver acknowledges when you are uploading files for Selenium testing over either a local machine or a remote machine. By this excellent feature of Selenium ii, you practice not have to write divide code to perform Selenium testing for uploading files over locally or remotely hosted spider web-application. We accept following options to upload files in a Remote Selenium WebDriver:
- SendKeys
- Robot Class
- AutoIT tool
- Jacob API
This certification demonstrates your knowledge of Selenium and Java, and your expertise at automating tests for any project.
Here's a short glimpse of the Selenium Java 101 certification from LambdaTest:
Upload Files In Selenium WebDriver Using Sendkeys()
It is e'er preferred to use get-go inbuilt features provided past Selenium Java to perform Upload file in Remote Selenium WebDriver. That is the SendKeys method. It directly applies to input tags which have an attribute as type='file'.
Here is an example to upload files in Selenium and Coffee using the Sendkeys():
Upload Files In Selenium WebDriver Using Robot Course
Robot class is an AWT class package in Coffee. This is also a very good option to choose for Upload file in selenium. This will aid to automate windows based Alert or pop upward, Print pop up or native windows screen. This is independent of the Operating System.
Here is the example of File uploading using Robot class:
Upload File In Selenium WebDriver Using AutoIT
AutoIT is an external automation tool and not provided past the selenium customs. Initially, AutoIT was used to automate native windows related popular up, however, there is a drawback of using AutoIT is that it creates .exe file and runs merely on Windows. It is not advisable to use AutoIT for File upload. However, if you still wish to perform a file upload in Selenium WebDriver using AutoIT then here is an example for you lot:
Notation: Save the file with .exe extension and run using command e.g Runtime.getRuntime().exec().
Upload File using Jacob API
Jacob provides the API technique to perform Upload file using Selenium. Once again, to perform a file upload in Selenium WebDriver using Jacob API you would need a .dll file. That means it won't work for Mac or Linux operating system. If you lot simply want to target Windows operating systems then here is an example of upload File using Jabob API.
Let'south Striking The Practical For File Upload In Selenium WebDriver
At present, let me demonstrate how to upload files in a Remote Selenium WebDriver over both, on your local infrastructure as well as over the cloud-based Selenium Filigree. As a part of this Selenium Java tutorial, I would be focusing on leveraging the SendKeys() method to upload files in Selenium and Java.
The scenario would be,
- open https://blueimp.github.io/jQuery-File-Upload/ and
- Click on Add files… button
- Then Kickoff Upload
- Assert it dorsum.
First off, we will start with the demonstration to upload files using local infrastructure machines. After we will have a demonstration of the aforementioned Selenium testing script over a cloud-based Selenium Grid.
Upload Files In Selenium WebDriver Over Local Infrastructure
Below is a Selenium Coffee testing script which demonstrates how to upload files in Selenium WebDriver over your local machine.
That is it.! Nevertheless, it isn't all you need to know. Information technology is important to note that when we refer to practical and existent-fourth dimension scenarios the requirement to perform automated browser testing might involve hundreds of browsers + OS combinations to be tested. Not to forget, the desired capabilities are bound to go bigger as your web awarding would scale over time.
In such scenarios, maintaining an in-house Selenium infrastructure is both time-consuming and expensive. Y'all volition need to hire more machines and resource on-board. Unless yous can afford a device lab provider such as Amazon AWS which can exist costly for many businesses. Then what can you do?
Fortunately, there is a deject-based Selenium Filigree such as LambdaTest which will help you lot execute Selenium testing for you web-application to ensure cross browser compatibility. With LambdaTest, you can test on 2000+ existent browsers & operating systems for both mobile & desktop. That style, you won't accept to worry about maintaining your Selenium Grid as we will provide you with a deject-based infrastructure with null reanimation, where machines are fix to burn up 24/7, equally per your wish.
Upload Files In Selenium WebDriver Over An Online Selenium Grid
At present, the same scenario tin be run on the LamdaTest'southward online Selenium Grid. You should observe here that we take changed only two points as shown below.
driver = new RemoteWebDriver ( new URL ( "http://hub.lambdatest.com:80/wd/hub" ) , capabilities ) ; driver . setFileDetector ( new LocalFileDetector ( ) ) ; |
And, yous are done!!! Here is the total Selenium Java testing script to upload file in Selenium WebDriver over an online Selenium Grid.
Monitoring Upshot Over LambdaTest Automation Dashboard
Once you lot navigate to the automation dashboard on LambdaTest. Yous will notice that the test has been executed successfully in the timeline.
Automation Logs: In the Automation Logs you can see each execution in detail including browser version, Operating system version, execution engagement and fourth dimension, videos, screenshots and steps of execution.
With that said, you take learned how to upload files in Remote Selenium WebDriver for both, local and cloud-based Selenium Grid.
Download Files in Selenium WebDriver
At present, that you are familiar with file uploading in Selenium WebDriver, you lot might be thinking that downloading a file with Selenium WebDriver is going to be just as easy! Well, think again! Yous have a web-application and yous would want the download file functionality to work seamlessly across different browsers and so that your customers aren't bothered by a UI bug. However, every web browser offers a unlike UI when downloading a file. Allow united states await at unlike screenshots of different browsers running on a macOS.
Mozilla Firefox download screen:
Safari download screen:
Google Chrome Download screen:
Similarly, these screenshots would differ for dissimilar operating systems, and operating system versions too. And then when yous are downloading a file through Google Chrome on Windows 7 it might give the beneath screen.
As you may notice, here the file was directly downloaded when the timer clocked-down to 0 seconds, without whatever user confirmation.
And so every browser will take a dissimilar download mechanism based on the operating system over which information technology is beingness utilized. Browser configuration using a profile, different browser, different Operating Systems play a vital role while Selenium testing with Java to download the file.
To automatically download file using Selenium with Java, we have the following options:
- AutoIT
- Robot Grade
- Browser Contour
Download File In Selenium WebDriver Using AutoIT
Already we have discussed the AutoIT tool. The same tool is used for downloading files in selenium. Again, download window changes as per Browsers. So users have to consider all scenarios to automate download pop upwards.
Hither is AutoIT script example:
WinWait ( "[CLASS:#MozillaDialogClass]" , "" , eight ) Ship ( "!s" ) Sleep ( 10000 ) Send ( "{ENTER}" ) |
Salvage this code and generate .exe file and execute in java code using Runtime.getRuntime().exec(). Again, Information technology is not advisable to use it equally it supports only Windows operating system and its external tool.
Download File In Selenium WebDriver Using Robot Class
You can run the below Selenium testing script to download files using Selenium with Java through Robot course.
Note: AutoIT and Robot grade code could alter based on the browser-specific profile prepare as well as where you want to save. Moreover, nearly important is cursor focus. If your download pop up is not in focus and then by and large your lawmaking will not piece of work.
Download File In Selenium WebDriver Using The Browser Profile Setting
By leveraging the browser profile setting, y'all can download files in Selenium WebDriver without interacting with the download window popular-up. You demand to flim-flam the browser contour. Here I have given a below example for Google Chrome browser and Mozilla Firefox browser.
Add this code into your Selenium Java testing suite.
Google Chrome
Mozilla Firefox
Let's Hitting The Applied For File Download In Selenium WebDriver
As we did a applied implementation to upload file in Selenium WebDriver. Nosotros will now practice downloading files in Selenium WebDriver on both local and cloud-based Selenium Grid. I volition exist demonstrating the file downloading using the Browser Profile Setting.
Download Files Using Selenium With Java With The Browser Profile Setting
Note: Working on a local auto for downloading files is easy to handle merely on a remote auto, information technology works based on the permission you have been provided to access the remote WebDrivers.
Download File Example For Online Selenium Grid With The Browser Profile Setting
Like to uploading, the simply affair we need to tweak in the Selenium Coffee testing script is going to be the file detector and the hub URL.
Below is a screenshot of LambdaTest automation logs which shows that the file is downloaded successfully.
Wrapping Upwardly!
If y'all have a website where you are assuasive users to either download or upload files in Selenium WebDriver then you lot need to ensure they work seamlessly across all browsers. Selenium testing can aid you easily automate the download & upload file functionality of your spider web-application. Working on upload file feature of Selenium is very easy if you have understood the difference betwixt Remote WebDriver and WebDriver Interface.
Although Selenium tin can help you lot execute exam cases in local infrastructure. Information technology is always recommended to get for a deject-based Selenium Grid in order to salve both time and resources. Selenium testing over an online Selenium Grid requires you to tweak a couple of lines of lawmaking where you specify the appropriate Hub URL for your Remote WebDriver. Similarly, Download file using a 3rd-party tool would terminate up having a flaky automation examination. Hence, information technology is always preferred browser profiling settings in guild to become a stable script. I hope the applied examples provided for both were explained in great detail and that they helped you gain more insight into the matter. If you notwithstanding got questions effectually downloading & uploading files in Selenium WebDriver so brand sure to drop them in the comment section. Cheers and happy testing!☺
Written past Harshit Paul
Harshit works as a product growth specialist at LambdaTest. He is also an experienced IT professional person, who loves to share his thoughts nearly the latest tech trends equally an enthusiast tech blogger.
DOWNLOAD HERE
Posted by: hendersonhance1943.blogspot.com