Upload a File to a Folder in C# Webapi
In this tutorial, nosotros will learn How to bargain with file uploads and downloads.
Uploading Files
For this section, we will use http://demo.guru99.com/test/upload/
equally our test application. This site easily allows any company to upload files without requiring them to sign upwardly.
Uploading files in WebDriver is washed by only using the sendKeys() method on the file-select input field to enter the path to the file to be uploaded.
handle file upload popup in selenium webdriver
Permit's say nosotros wish to upload the file "C:\newhtml.html". Our WebDriver code should be like the 1 shown beneath.
parcel newproject; import org.openqa.selenium.*; import org.openqa.selenium.firefox.FirefoxDriver; public class PG9 { public static void chief(String[] args) { Organization.setProperty("webdriver.gecko.commuter","C:\\geckodriver.exe"); String baseUrl = "http://demo.guru99.com/examination/upload/"; WebDriver commuter = new FirefoxDriver(); driver.go(baseUrl); WebElement uploadElement = driver.findElement(By.id("uploadfile_0")); // enter the file path onto the file-choice input field uploadElement.sendKeys("C:\\newhtml.html"); // cheque the "I accept the terms of service" cheque box driver.findElement(By.id("terms")).click(); // click the "UploadFile" push button driver.findElement(By.name("send")).click(); } }
After running this script, you should exist able to upload the file successfully and yous should become a bulletin like to this.
Think following two things when uploading files in WebDriver
- There is no need to simulate the clicking of the "Browse" push. WebDriver automatically enters the file path onto the file-pick text box of the <input type="file"> chemical element
- When setting the file path in your Coffee IDE, use the proper escape graphic symbol for the back-slash.
Downloading Files
WebDriver has no capability to access the Download dialog boxes presented by browsers when you click on a download link or button. Notwithstanding, we tin can featherbed these dialog boxes using a divide program called "wget".
What is Wget?
Wget is a small and easy-to-use command-line program used to automate downloads. Basically, we will access Wget from our WebDriver script to perform the download procedure.
Setting up Wget
Footstep 1: In your C Drive, create a new folder and name it every bit "Wget".
Download wget.exe from hither and Identify it in the Wget folder you created from the step above.
Step two: Open Run by pressing windows key + "R" ; type in "cmd & click ok
Type in the command "cd /" to motility to the root directory
Stride 3: Type in the command to check whether the given setup is working
cmd /c C:\\Wget\\wget.exe -P C: --no-check-certificate http://demo.guru99.com/selenium/msgr11us.exe
There seems to be an effect writing into C drive.
Step four: Yous need to debug the wget errors in command line before you execute the lawmaking using Selenium Webdriver. These errors will persist in Eclipse and the error messages volition non be equally informative. Best to first get wget working using command line. If it works in control line it volition definitely work in Eclipse.
In our case, equally show in stride 3, there is a trouble writing into C drive. Allow's alter the download location to D drive and check results.
cmd /c C:\\Wget\\wget.exe -P D: --no-cheque-certificate http://demo.guru99.com/selenium/msgr11us.exe
Messenger was downloaded successfully.
Before you lot go on further don't forget to delete the downloaded file
Using WebDriver and Wget
In the following example, nosotros will use WebDriver and wget to download a pop chat software chosen Yahoo Messenger. Our base of operations URL shall be http://demo.guru99.com/exam/yahoo.html
.
Footstep ane
Import the "java.io.IOException" package considering nosotros will have to grab an IOException subsequently in Step iv.
Step 2
Employ getAttribute() to obtain the "href" value of the download link and salvage information technology equally a String variable. In this example, nosotros named the variable every bit "sourceLocation".
Footstep iii
Set up-up the syntax for wget using the post-obit command.
Step 4
Initiate the download process past calling wget from our WebDriver lawmaking.
To sum it all up, your WebDriver code could look like the i shown below.
package newproject; import java.io.IOException; import org.openqa.selenium.*; import org.openqa.selenium.firefox.FirefoxDriver; public class PG8 { public static void primary(String[] args) { System.setProperty("webdriver.gecko.driver","C:\\geckodriver.exe"); Cord baseUrl = "http://demo.guru99.com/test/yahoo.html"; WebDriver commuter = new FirefoxDriver(); driver.become(baseUrl); WebElement downloadButton = commuter.findElement(By .id("messenger-download")); Cord sourceLocation = downloadButton.getAttribute("href"); String wget_command = "cmd /c C:\\Wget\\wget.exe -P D: --no-check-certificate " + sourceLocation; effort { Procedure exec = Runtime.getRuntime().exec(wget_command); int exitVal = exec.waitFor(); System.out.println("Exit value: " + exitVal); } grab (InterruptedException | IOException ex) { System.out.println(ex.toString()); } driver.close(); } }
Afterward executing this lawmaking, check your D drive and verify that the Yahoo Messenger installer was successfully downloaded there.
Summary
- 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.
- WebDriver cannot automate downloading of files on its own.
- The easiest way to download files using WebDriver is to use Wget.
Source: https://www.guru99.com/upload-download-file-selenium-webdriver.html
0 Response to "Upload a File to a Folder in C# Webapi"
Post a Comment