Pass Download Popup Dialog Using Selenium Webdriver And WGet

When you download a file in the selenium webdriver automation testing script, you can not access the popup dialog. Although you can use some method to pass it in some web browsers, 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.

1. 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 the WGet home page to see a detailed introduction.

2. How To Install And Use Wget?

  1. Go to the WGet download page.
  2. Download the version you need. Because our example is run on windows, so I just download the windows version.
  3. After download, you can run the wget.exe in a dos window to use it to get a web resource.
  4. CD to go to the wget.exe download directory, then run the command wget -P C:/WorkSpace –no-check-certificate http://www.dev2qa.com/demo/download/downloadExample.html in the dos window as below.
    C:\Users\zhaosong\Downloads>wget -P C:/WorkSpace --no-check-certificate http://www.dev2qa.com/demo/download/downloadExample.html
    --2021-06-07 15:27:47--  http://www.dev2qa.com/demo/download/downloadExample.html
    Resolving www.dev2qa.com (www.dev2qa.com)... 172.67.220.159, 104.21.78.118
    Connecting to www.dev2qa.com (www.dev2qa.com)|172.67.220.159|:80... connected.
    HTTP request sent, awaiting response... 301 Moved Permanently
    Location: https://www.dev2qa.com/demo/download/downloadExample.html [following]
    --2021-06-07 15:27:48--  https://www.dev2qa.com/demo/download/downloadExample.html
    Connecting to www.dev2qa.com (www.dev2qa.com)|172.67.220.159|:443... connected.
    HTTP request sent, awaiting response... 200 OK
    Length: unspecified [text/html]
    Saving to: 'C:/WorkSpace/downloadExample.html'
    
    downloadExample.html                                            [ <=>                                                                                                                              ]  13.19K  --.-KB/s    in 0.001s
    
    2021-06-07 15:27:49 (14.0 MB/s) - 'C:/WorkSpace/downloadExample.html' saved [13505]
  5. -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.
  6. wget --help will list all the helpful information about how to use it.

3. 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 the below command in java code to invoke wget.
    "cmd /c \"C:/User/Downloads/wget.exe\" -P C:/WorkSpace --no-check-certificate " + downloadLink
  3. Waiting for the process to complete.
  4. Please note we should clear the cache ( read out wget generated info ) generated by the java process, this can avoid process suspension. Please see the java code comments for detailed 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();  
    	        }  
    	    }  
    }

References

  1. How To Install And Use Wget On Mac OS X To Implement File Download Breakpoint Resume

Leave a Comment

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.