不借助autolt实现下载文件到指定目录
今天尝试了下不用借助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实现下载文件到指定目录的更多相关文章
- CentOS7.5 通过wget下载文件到指定目录
在Linux命令行下面下载文件,通过wget是比较普遍简单的,比如在CentOS7 里面也一样. 我们先来看下自己的CentOS7 系统有没有安装wget: [root@test redis]# rp ...
- CentOS7通过wget下载文件到指定目录
查看自己的CentOS7系统有没有安装wget: [duanyongchun@192 3DUnetCNN]$ rpm -qa|grep wget 已安装提示: #已安装提示:wget-1.14-18. ...
- 通过url下载文件到指定目录 java
import java.io.DataInputStream; import java.io.DataOutputStream; import java.io.File; import java.io ...
- PHP下载远程文件到指定目录
PHP用curl可以轻松实现下载远程文件到指定目录: <?php class Download { public static function get($url, $file) { retur ...
- winform复制文件到指定目录
执行步骤 弹出选择对话框:var openFileDialog = new OpenFileDialog(); 设置选择内容,如所有图片:openFileDialog.Filter="图像文 ...
- C#使用WebClient下载文件到本地目录
C#使用WebClient下载文件到本地目录. 1.配置本地目录路径 <appSettings> <!--文件下载目录--> <add key="Downloa ...
- Linux解压文件到指定目录
Linux解压文件到指定目录 tar在Linux上是常用的打包.压缩.加压缩工具,他的参数很多,折里仅仅列举常用的压缩与解压缩参数 参数:-c :create 建立压缩档案的参数:-x : 解压缩压缩 ...
- Git .gitignore使用 -- 过滤class文件或指定目录
1. 进入当前的项目根目录 执行 git init touch .gitignore 2. 过滤class文件或指定目录 *.class /target/ 3. 提交 git add . 将所有文件提 ...
- 解压.zip,.tar.gz文件到指定目录,重命名文件
1.解压文件到指定目录 /** * 解压文件到指定目录 * zipFile:要解压的文件 * descDir:解压到哪个文件 * */ @SuppressWarnings("rawtypes ...
随机推荐
- 解决局域网IP冲突
进入cmd ipconfig -all 查看现有IP,发现IP不是192.168.1.*的形式,而是192.168.0.*等异常 ipconfig -release 释放现有IP ipconfig ...
- Docker(三)-Docker中Image、Container与Volume的迁移
Image 镜像的迁移,适用于离线环境. 一般离线环境,都会自建Docker Registry. 无论 官方的 ,还是最近流行的 Harbor ,都是不错的选择. 但是,这个世界上就是有些环境,或者说 ...
- Oracle 数据库执行 操作系统的命令
1 Linux环境下面的处理 在sqlplus 里面 添加一个 ! 就可以执行 但是 因为Oracle 必须为非root用户 所以很多命令可能无法执行: 2. Windows 环境执行命令的方式 是在 ...
- 以用户注册功能模块为例浅谈MVC架构下的JavaWeb开发流程
JavaWeb应用开发,撇开分布式不谈,只讨论一个功能服务应用的开发,无论是使用原生的Servlet/JSP方案,还是时下的SSM架构,都有一套经过工程实践考验的最佳实践,这综合考虑了团队协作.项目管 ...
- Java之File类详解
常用操作: File f = new File("C:\\testdata\\filedir\\a\\b\\c"); f.mkdir(); //建立单级目录 f.mkdirs(); ...
- Oracle 存储容量最大的字段类型CLOB
1.优点,无限容量 2.数据量大时读取很慢
- 【刷题】BZOJ 4805 欧拉函数求和
Description 给出一个数字N,求sigma(phi(i)),1<=i<=N Input 正整数N.N<=2*10^9 Output 输出答案. Sample Input 1 ...
- 【题解】 P1879 玉米田Corn Fields (动态规划,状态压缩)
题目描述 Farmer John has purchased a lush new rectangular pasture composed of M by N (1 ≤ M ≤ 12; 1 ≤ N ...
- VirtualBox 文件共享及网络配置技巧
文件共享 1. 安装增强功能 2. 分配数据空间 这样就可以在virtualbox里访问本地的数据了: 网络配置
- 前端学习 -- Css -- 样式的继承
像儿子可以继承父亲的遗产一样,在CSS中,祖先元素上的样式,也会被他的后代元素所继承, 利用继承,可以将一些基本的样式设置给祖先元素,这样所有的后代元素将会自动继承这些样式. 但是并不是所有的样式都会 ...