selenium測試(Java)--下載檔案(十六)

來源:互聯網
上載者:User

標籤:reference   als   ice   ras   代碼   tle   reads   false   list   

下載檔案需要在Firefox 的profile屬性中配置一些參數,如下面的代碼:

package com.test.download;import java.io.File;import org.openqa.selenium.By;import org.openqa.selenium.JavascriptExecutor;import org.openqa.selenium.WebDriver;import org.openqa.selenium.firefox.FirefoxDriver;import org.openqa.selenium.firefox.FirefoxProfile;public class DownloadTest {    public static void main(String[] args) {        FirefoxProfile profile = new FirefoxProfile();        // 可以在Firefox瀏覽器地址欄中輸入about:config來查看屬性        // 設定下載檔案放置路徑,注意如果是windows環境一定要用\\,用/不行        String path = "D:\\10-selenium\\workspace\\SeleniumTest\\src\\com\\test\\download\\down";        String downloadFilePath = path + "\\d.exe";        File file = new File(downloadFilePath);        if (file.exists()) {            file.delete();        }        // 配置響應下載參數        profile.setPreference("browser.download.dir", path);// 下載路徑        profile.setPreference("browser.download.folderList", 2);// 2為儲存在指定路徑,0代表預設路徑        profile.setPreference("browser.download.manager.showWhenStarting", false);// 是否顯示開始        // 禁止彈出儲存框,value是檔案格式,如zip檔案        profile.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");//關於類型:可以參考http://www.w3school.com.cn/media/media_mimeref.asp        WebDriver driver = new FirefoxDriver(profile);        driver.get("file:///D:/10-selenium/workspace/SeleniumTest/src/com/test/download/download.html");        driver.manage().window().maximize();        driver.findElement(By.linkText("下載")).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 void waitTime(int time) {        try {            Thread.sleep(time);        } catch (InterruptedException e) {            // TODO Auto-generated catch block            e.printStackTrace();        }    }}

使用到的頁面例子:

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>download</title>
</head>
<body>
<a href="d.exe">下載</a>
</body>
</html>

測試代碼結構:

selenium測試(Java)--下載檔案(十六)

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.