Pass Download Popup Dialog Using Selenium Webdriver And WGet

When you download a file in selenium webdriver automation testing script, you can not access the popup dialog. Although you can use some method to pass it in some web broswer, but you still can not pass it with all web browsers. In this article, we will show you how to pass the download popup dialog permanently using WGet.

What Is WGet ?

  1. A small command line program which is used to download a file automatically.
  2. Open source and free.
  3. You can go to WGet home page to see detail introduction.
    wget home page feature introduction

How To Install And Use WGet?

  1. Go to WGet download page.
  2. Download the version you need. Because our example is run on windows, so i just download windows version.
    wget installer download page
  3. Download the wget complete windows installer as below picture. It will include all the dependency dll files.
    wget windows installer download page
  4. After download, click the wget-1.11.4-1-setup.exe file to install it.
  5. After default installation, you can find it has been installed in below directory.
    C:\Program Files (x86)\GnuWin32\bin
  6. Now open a dos window, cd to above installation directory. Run command as below.
    cd C:\Program Files (x86)\GnuWin32\bin
    wget -P C:/WorkSpace --no-check-certificate http://www.dev2qa.com/demo/download/downloadExample.html

    run wget command result
  7. -P C:/WorkSpace means save the downloaded file in folder C:/WorkSpace. So you should create the folder first.
    --no-check-certificate  means do not check url server certification.
    wget --help will list all the help information about how to use it.

Integrate WGet With Selenium WebDriver Test

Now we can use selenium and wget together in our automation testing script to download any web file without popup dialog generated by any web browser.

  1. Use selenium webdriver to get the url file link.
  2. Run below command in java code to invoke wget.
    "cmd /c \"C:/Program Files (x86)/GnuWin32/bin/wget.exe\" -P C:/WorkSpace --no-check-certificate " + downloadLink
  3. Waiting for the process complete.
  4. Please note we should clear the cache ( read out wget generated info ) generated by the java process, this can avoid process suspend. Please see the java code comments for detail explanation.
    public class TestPassDonwloadPopupDialog {
    
    	public static void main(String[] args) {
    		
    		TestPassDonwloadPopupDialog tpdpd = new TestPassDonwloadPopupDialog();
    		
    		String downloadLink = tpdpd.getDonwloadLink();
    		
    		tpdpd.downloadItWithWGet(downloadLink);
    	}
    
    	/* Get the url link for the download file. */
    	public String getDonwloadLink()
    	{
    		String retLink = "";
    		
    		WebDriver driver = new FirefoxDriver();
    		
    		// Example page include a link to download file.
    		driver.get("http://www.dev2qa.com/demo/download/downloadExample.html");
    		
    		By byLinkPartialText = By.partialLinkText("Setup Program For Windows");
    		
    		WebElement linkElement = driver.findElement(byLinkPartialText);
    		
    		retLink = linkElement.getAttribute("href");
    		
    		driver.close();
    		
    		System.out.println("Parse url link complete, close the Firefox browser.");
    		
    		return retLink;
    	}
    	/* Use WGet to download the file. */
    	public void downloadItWithWGet(String downloadLink)
    	{
    		/*
    		 * -P C/WorkSpace : Save the download file in C/WordSpace.
    		 * --no-check-certificate : Ignore the certificate check for the url.
    		 * */
    		String wgetCommand = "cmd /c \"C:/Program Files (x86)/GnuWin32/bin/wget.exe\" -P C:/WorkSpace --no-check-certificate " + downloadLink;
    
            try {
            	
            	System.out.println("Downloading url " + downloadLink);
            	
            	// Run the command.
            	Process execProc = Runtime.getRuntime().exec(wgetCommand);
            	
            	// Clear the Process input stream cache to avoid Process suspend.
            	ClearProcessStream clearProcessInputStream = new ClearProcessStream(execProc.getInputStream(), "INFO");
            	clearProcessInputStream.start();
            	
            	// Clear the Process error stream cache to avoid Process suspend.
            	ClearProcessStream clearProcessErrorStream = new ClearProcessStream(execProc.getErrorStream(), "ERROR"); 
            	clearProcessErrorStream.start();
            	
            	// Wait for the process to complete.
            	int wGetExitVal = execProc.waitFor();
            	
            	System.out.println("WGet process exit value : " + wGetExitVal);
            	
            	System.out.println("Download url complete " + downloadLink);
            } catch ( Exception ex) {
            	ex.printStackTrace();
            }
    	}
    }
    
    /* This class is used to avoid java Process suspend issue. 
     * It will clear all the data in the Process cache stream to
     * make the Process execution finished.
     * */
    class ClearProcessStream extends Thread{
    	
    	 private InputStream is;  
    	 
    	 private String isType;  
    	  
    	 ClearProcessStream(InputStream is, String isType) {  
    	        this.is = is;  
    	        this.isType = isType;  
    	 }  
    	  
    	 public void run() {  
    	 
    		 try { 
    	            InputStreamReader isReader = new InputStreamReader(is);  
    	            BufferedReader br = new BufferedReader(isReader); 
    	           
    	            /* Read out all the data in the InputStream. */
    	            String lineInfo = null;  
    	            while ((lineInfo = br.readLine()) != null) {  
    	                System.out.println(isType + " : " + lineInfo);  
    	            }  
    	        } catch (IOException ex) {  
    	            ex.printStackTrace();  
    	        }  
    	    }  
    }

    Output
    wget download file example output1
    wget download file example output2

Download “PassDownloadPopupDialogUsingSeleniumWebdriverWGetExampleCode.zip” PassDownloadPopupDialogUsingSeleniumWebdriverWGetExampleCode.zip – Downloaded 39 times – 2 KB

(Visited 505 times, 1 visits today)
READ :   How To Scroll Web Page In Selenium Webdriver Testing Script Using Java

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.