不借助autolt實現下載文件到指定目錄

今天嘗試了下不用藉助autolt完成下載文件到指定目錄,html

好處:在於集成迴歸,遠程機能夠繞過執行autolt程序權限問題,致使autolt程序沒法調用,不能完成腳本的迴歸java

Firefox瀏覽器已經成功,代碼以下:web

package com.brower.demo;

import java.io.File;

import org.openqa.selenium.*;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.firefox.FirefoxProfile;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.Test;

/**
 * @author longrong.lang
 * 不借助autolt實現下載文件到指定目錄
 */
public class FirefoxDownloadTest {
    WebDriver driver;

    @BeforeClass
    public void beforeClass() {
        driver = getDriver();
    }

    /**
     * 設置火狐瀏覽器默認參數
     *
     * @return
     */
    private WebDriver getDriver() {
        FirefoxProfile profile = new FirefoxProfile();
        // 能夠在Firefox瀏覽器地址欄中輸入about:config來查看屬性
        // 設置下載文件放置路徑,注意若是是windows環境必定要用\\,用/不行
        String path = "C:\\wps";
        String downloadFilePath = path + "\\demo.exe";
        File file = new File(downloadFilePath);
        if (file.exists()) {
            file.delete();
        }
        // 配置響應下載參數
        // 下載路徑
        profile.setPreference("browser.download.dir", path);
        // 2爲保存在指定路徑,0表明默認路徑
        profile.setPreference("browser.download.folderList", 2);
        // 是否顯示開始
        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");
        return new FirefoxDriver((Capabilities) profile);
    }

    @Test
    public void test() throws InterruptedException {
        driver.get("file:///C:/Demo.html");
        driver.manage().window().maximize();
        driver.findElement(By.linkText("下載")).click();
        Thread.sleep(3000);
    }
}

 

chrome瀏覽器,也算成功,可是遺留個小問題,就是會提示是否保留,點保留會下載到你指定的目錄,如不點擊不保存,在羣裏問的發總,發總說chrome的這個profile被取消了,結果我又百度了下,說是33版本以前的能夠,以後不能夠,這個有興趣的小夥伴能夠本身去試試。代碼以下:chrome

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.chrome.ChromeOptions;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.Test;

import java.io.File;
import java.util.HashMap;
import java.util.Map;

/**
 * @author longrong.lang
 * 不借助autolt實現下載文件到指定目錄
 */
public class ChromeDownloadTest {

    WebDriver driver;
    @BeforeClass
    public void beforeClass() {
        driver = getDriver();
    }

    @Test
    public void testChromeDownload() throws Exception {
        WebDriver driver = getDriver();
        driver.get("file:///C:/Users/Administrator/Desktop/demo.html");
        driver.manage().window().maximize();
        driver.findElement(By.linkText("下載")).click();
        Thread.sleep(3000);
    }

    /**
     * 設置默認參數
     * @return
     */
    private WebDriver getDriver() {
        String path = "C:\\wps";
        // 設置下載文件放置路徑,注意若是是windows環境必定要用\\,用/不行
        String downloadFilePath = path + "\\demo.exe";
        File file = new File(downloadFilePath);
        if (file.exists()) {
            file.delete();
        }
        System.setProperty("webdriver.chrome.driver", "driver/chromedriver.exe");
        ChromeOptions options = new ChromeOptions();
        // 去掉打開谷歌瀏覽器時上方提示的不支持的命令行標記
        options.addArguments("test-type");
        options.addArguments("--start-maximized");
        options.addArguments("--disable-popup-blocking");
        options.addArguments("no-sandbox");
        options.addArguments("disable-extensions");
        options.addArguments("no-default-browser-check");
        Map<String, Object> prefs = new HashMap<String, Object>();
        prefs.put("credentials_enable_service", false);
        // 禁用密碼保存
        prefs.put("profile.password_manager_enabled", false);
        // 2爲保存在指定路徑,0表明默認路徑
        prefs.put("profile.default_content_settings.popups", 2);
        prefs.put("download.default_directory", path);
        options.setExperimentalOption("prefs", prefs);
        return new ChromeDriver(options);
    }

}

 

 

測試文件:windows

<!DOCTYPE html>
<html>
<head>

<title>download</title>
</head>
<body>
    <a href="demo.exe">下載</a>
</body>
</html>
相關文章
相關標籤/搜索