码迷,mamicode.com
首页 > 其他好文 > 详细

不借助autolt实现下载文件到指定目录

时间:2017-08-23 13:24:16      阅读:170      评论:0      收藏:0      [点我收藏+]

标签:集成   wait   adf   hash   public   service   不用   disk   star   

今天尝试了下不用借助autolt完成下载文件到指定目录,

好处:在于集成回归,远程机可以绕过执行autolt程序权限问题,导致autolt程序无法调用,不能完成脚本的回归

Firefox浏览器已经成功,代码如下:

package com.dwkj.test.util;

import java.io.File;

import org.openqa.selenium.Alert;
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;

/**
 * 
 * @author longrong.lang
 *    不借助autolt实现下载文件到指定目录
 *
 */
public class FirefoxDownloadTest {

    public static void main(String[] args) {

        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");
        WebDriver driver = new FirefoxDriver(profile);
        driver.get("file:///C:/Demo.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);
        }
        Alert alert = driver.switchTo().alert();
        waitTime(2000);
        alert.accept();
        // driver.quit();

    }

    static public void waitTime(int time) {

        try {
            Thread.sleep(time);
        } catch (InterruptedException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

}

chrome浏览器,也算成功,但是遗留个小问题,就是会提示是否保留,点保留会下载到你指定的目录,如不点击不保存,在群里问的发总,发总说chrome的这个profile被取消了,结果我又百度了下,说是33版本之前的可以,之后不可以,这个有兴趣的小伙伴可以自己去试试。代码如下:

package com.dwkj.test.util;

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

import org.openqa.selenium.Alert;
import org.openqa.selenium.By;
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.chrome.ChromeOptions;

/**
 * 
 * @author longrong.lang
 *    不借助autolt实现下载文件到指定目录
 *
 */
public class ChromeDownloadTest {

    public static void main(String[] args) {

        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", "tools/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);
        WebDriver driver = new ChromeDriver(options);
        driver.get("file:///C:/demo.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);
        }
        Alert alert = driver.switchTo().alert();
        waitTime(2000);
        alert.accept();
        // 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>

<title>download</title>
</head>
<body>
    <a href="demo.exe">下载</a>
</body>
</html>

 

不借助autolt实现下载文件到指定目录

标签:集成   wait   adf   hash   public   service   不用   disk   star   

原文地址:http://www.cnblogs.com/longronglang/p/7417389.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!