Selenium Test (Java)-Download file (16)

Source: Internet
Author: User

To download the file you need to configure some parameters in the profile property of Firefox, as in the following code:

 PackageCom.test.download;ImportJava.io.File;Importorg.openqa.selenium.By;ImportOrg.openqa.selenium.JavascriptExecutor;ImportOrg.openqa.selenium.WebDriver;ImportOrg.openqa.selenium.firefox.FirefoxDriver;ImportOrg.openqa.selenium.firefox.FirefoxProfile; Public classDownloadtest { Public Static voidMain (string[] args) {firefoxprofile profile=NewFirefoxprofile (); //You can enter about:config in the address bar of the Firefox browser to view the properties//set the download file placement path, note that if the Windows environment must use \ \, do notString Path = "D:\\10-selenium\\workspace\\seleniumtest\\src\\com\\test\\download\\down"; String Downloadfilepath= path + "\\d.exe"; File File=NewFile (Downloadfilepath); if(File.exists ()) {file.delete (); }        //Configure response Download ParametersProfile.setpreference ("Browser.download.dir", Path);//Download PathProfile.setpreference ("Browser.download.folderList", 2);//2 is saved at the specified path, 0 represents the default pathProfile.setpreference ("Browser.download.manager.showWhenStarting",false);//whether to show start//Suppress pop-up save box, value is file format, such as ZIP fileProfile.setpreference ("Browser.helperApps.neverAsk.saveToDisk",                "Application/zip,text/plain,application/vnd.ms-excel,text/csv,text/comma-separated-values,application/ octet-stream,application/vnd.openxmlformats-officedocument.spreadsheetml.sheet,application/ Vnd.openxmlformats-officedocument.wordprocessingml.document ");//about types: You can refer to http://www.w3school.com.cn/media/media_mimeref.aspWebdriver Driver=Newfirefoxdriver (profile); Driver.get ("File:///D:/10-selenium/workspace/SeleniumTest/src/com/test/download/download.html");        Driver.manage (). window (). Maximize (); Driver.findelement (By.linktext (Download) . Click (); WaitTime (3000); String js_exist= "alert (\" Download successfully\ ")"; String js_not_exist= "alert (\" Download unsuccessfully\ ")"; if(File.exists ()) {((javascriptexecutor) driver). Executescript (Js_exist); } Else{(javascriptexecutor) driver). Executescript (Js_not_exist); } waitTime (5000); //driver.quit ();    }    Static  Public voidWaitTime (intTime ) {        Try{thread.sleep (time); } Catch(interruptedexception e) {//TODO auto-generated Catch blockE.printstacktrace (); }    }}

Examples of pages to use:

<! DOCTYPE html>
<meta charset= "UTF-8" >
<title>download</title>
<body>
<a href= "D.exe" > Downloads </a>
</body>

To test the code structure:

Selenium Test (Java)-Download file (16)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.