Download and validate file using webdriver,wget


Downloading Files

WebDriver has no capability to access the Download dialog boxes presented by browsers when you click on a download link or button. However, we can bypass these dialog boxes using a separate 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 process.  

Setting up Wget

Step 1
In your C Drive, create a new folder and name it as "Wget".
Download wget.exe here and place it in the Wget folder you created from the step above.
Step 2
Bring up the System Properties window by pressing Win + Pause on your keyboard.
Click on "Advanced System Settings"







Click on the "Environment Variables".










Step 3
In the Environment Variables dialog box, in the "System variables" section, scroll down the list until you find "Path" and then double-click it.





Step 4
In the "Variable value" text box, add the new path "C:\Wget\". Just be sure that there is a semi-colon separating this new entry from the pre-existing values







Step 5
Click OK on all dialog boxes.
Launch the command prompt and type the command "wget". You should get the following response.




Using WebDriver and Wget

In the following example, we will use WebDriver and wget to download a popular chat software called Yahoo Messenger. Our base URL shall be http://messenger.yahoo.com/.





Step 1
Import the "java.io.IOException" package because we will have to catch an IOException later in Step 4.
import java.io.IOException;
Step 2
Use getCssAttribute() to obtain the "href" value of the download link and save it as a String variable. In this case, we named the variable as "sourceLocation".



Step 3
Set-up the syntax for wget using the following command.




Step 4
Initiate the download process by calling wget from our WebDriver code.













To sum it all up, your WebDriver code could look like the one shown below.
package mypackage;

import java.io.IOException;

import org.openqa.selenium.*;
import org.openqa.selenium.firefox.FirefoxDriver;
public class myclass {

public static void main(String[] args) {
String baseUrl = "http://messenger.yahoo.com/";
WebDriver driver = new FirefoxDriver();

driver.get(baseUrl);
WebElement downloadButton = driver.findElement(By
.id("messenger-download"));
String sourceLocation = downloadButton.getAttribute("href");
String wget_command = "cmd /c wget -P c: --no-check-certificate " + sourceLocation;

try {
Process exec = Runtime.getRuntime().exec(wget_command);
int exitVal = exec.waitFor();
System.out.println("Exit value: " + exitVal);
} catch (InterruptedException | IOException ex) {
System.out.println(ex.toString());
}
driver.quit();
}
}
After executing this code, check your C drive and verify that the Yahoo Messenger installer was successfully downloaded there.

Comments

Popular posts from this blog

What’s the Difference Between Docker and Kubernetes?

TestNG Listeners

Database Sharding