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

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

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

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版本之前的可以,之后不可以,这个有兴趣的小伙伴可以自己去试试。代码如下:

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);
} }

测试文件:

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

不借助autolt实现下载文件到指定目录的更多相关文章

  1. CentOS7.5 通过wget下载文件到指定目录

    在Linux命令行下面下载文件,通过wget是比较普遍简单的,比如在CentOS7 里面也一样. 我们先来看下自己的CentOS7 系统有没有安装wget: [root@test redis]# rp ...

  2. CentOS7通过wget下载文件到指定目录

    查看自己的CentOS7系统有没有安装wget: [duanyongchun@192 3DUnetCNN]$ rpm -qa|grep wget 已安装提示: #已安装提示:wget-1.14-18. ...

  3. 通过url下载文件到指定目录 java

    import java.io.DataInputStream; import java.io.DataOutputStream; import java.io.File; import java.io ...

  4. PHP下载远程文件到指定目录

    PHP用curl可以轻松实现下载远程文件到指定目录: <?php class Download { public static function get($url, $file) { retur ...

  5. winform复制文件到指定目录

    执行步骤 弹出选择对话框:var openFileDialog = new OpenFileDialog(); 设置选择内容,如所有图片:openFileDialog.Filter="图像文 ...

  6. C#使用WebClient下载文件到本地目录

    C#使用WebClient下载文件到本地目录. 1.配置本地目录路径 <appSettings> <!--文件下载目录--> <add key="Downloa ...

  7. Linux解压文件到指定目录

    Linux解压文件到指定目录 tar在Linux上是常用的打包.压缩.加压缩工具,他的参数很多,折里仅仅列举常用的压缩与解压缩参数 参数:-c :create 建立压缩档案的参数:-x : 解压缩压缩 ...

  8. Git .gitignore使用 -- 过滤class文件或指定目录

    1. 进入当前的项目根目录 执行 git init touch .gitignore 2. 过滤class文件或指定目录 *.class /target/ 3. 提交 git add . 将所有文件提 ...

  9. 解压.zip,.tar.gz文件到指定目录,重命名文件

    1.解压文件到指定目录 /** * 解压文件到指定目录 * zipFile:要解压的文件 * descDir:解压到哪个文件 * */ @SuppressWarnings("rawtypes ...

随机推荐

  1. ehlib使用内存表的方法

    ehlib提供了一个TMemTableEh控件,这个控件不需要连接数据库就可以在ehlib中显示数据,在做一些虚的表格时比较有用. 简单的使用主要有这几个步骤: 1.添加量过控件Tdatasource ...

  2. Windows下多线程编程(二)

    线程的分类 1.     有消息循环线程 MFC中有用户界面线程,从CWinThread派生出一个新的类作为UI线程类CUIThread,然后调用AfxBeginthread(RUNTIME_CLAS ...

  3. spirngcloud文件

    https://blog.csdn.net/liumiaocn/article/details/54309866

  4. 服务端增加WCF服务全局异常处理机制

    服务端增加WCF服务全局异常处理机制,任一WCF服务或接口方式出现异常,将统一调用WCF_ExceptionHandler.ProvideFault方法,因此不需要每个方法使用try catch写法. ...

  5. 【设计模式】—— 状态模式State

    前言:[模式总览]——————————by xingoo 模式意图 允许一个对象在内部改变它的状态,并根据不同的状态有不同的操作行为. 例如,水在固体.液体.气体是三种状态,但是展现在我们面前的确实不 ...

  6. 【BZOJ】1002:轮状病毒(基尔霍夫矩阵【附公式推导】或打表)

    Description 轮状病毒有很多变种,所有轮状病毒的变种都是从一个轮状基产生的.一个N轮状基由圆环上N个不同的基原子和圆心处一个核原子构成的,2个原子之间的边表示这2个原子之间的信息通道.如下图 ...

  7. 【转】ubuntu 12.04下如何开启 NFS 服务 & 设置

    在嵌入式Linux开发中,利用NFS服务从开发板访问Linux主机是个高效&方便的调试方法,在程序调试过程中可以避免多次下载程序到开发板.但这需要在Linux主机上首先开通NFS服务. 以ub ...

  8. bzoj1345 序列问题 (贪心)

    考虑某个点产生的贡献: 如果i左边是一个比它小的数x,那有两种情况: 1.x的左边的数y大于i,肯定要把x合并到i,i的贡献++ 2.x的左边的数y小于i,那肯定要把x合并到y,而这时候递归地来考虑, ...

  9. 进程和线程(4)-进程 vs. 线程

    进程 vs. 线程 我们介绍了多进程和多线程,这是实现多任务最常用的两种方式.现在,我们来讨论一下这两种方式的优缺点. 首先,要实现多任务,通常我们会设计Master-Worker模式,Master负 ...

  10. isspace 对含有中文 的字符串进行检查的时候表现不正常!?

    #include <stdio.h> #include <stdlib.h> #include <string.h> #include <ctype.h> ...