不借助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 ...
随机推荐
- DOM之节点类型加例子
DOM= Document Object Model,文档对象模型,DOM可以以一种独立于平台和语言的方式访问和修改一个文档的内容和结构.换句话说,这是表示和处理一个HTML或XML文档的常用方法.D ...
- 第十一周(11.24-12.01)----WBS功能分解
功能 子功能 二级子功能 预计花费时间(小时) 游戏基础功能 显示首界面 绘制产产品主logo及不同难度下的布局 4 游戏 难度选择(初级.中级.高级) 4 退出整个程序 1 放弃 ...
- PHP中Cookie的使用
1.什么是Cookie? Cookie保存在客户端浏览器中,cookie是Http头的一部分,通过浏览器请求页面时,它会被通过Http头的形式发送过去.被请求的页面,可以通过PHP来获取cookie的 ...
- 知识点【JavaScript模块化】
JavaScript模块化历程 JavaScript发展变迁大概是一下几个步骤: 工具(浏览器兼容) 组件(功能模块) 框架(功能模块组织) 应用(业务模块组织) 但是经过了长长的后天努力过程Java ...
- HDU 5112 A Curious Matt (2014ACM/ICPC亚洲区北京站-重现赛)
A Curious Matt Time Limit: 2000/2000 MS (Java/Others) Memory Limit: 512000/512000 K (Java/Others) ...
- 【题解】 bzoj1875: [SDOI2009]HH去散步 (动态规划+矩阵乘法)
bzoj1875,懒得复制,戳我戳我 Solution: 看到这道题,看的出是个dp,每个点\(t\)时刻到达的方案数等于\(t-1\)到连过来的点方案数之和 但又因为题目有要求不能走一样的边回去不是 ...
- Java实现的一个简单的模板渲染
代码 package com.hdwang; import java.util.HashMap; import java.util.Map; /** * Created by hdwang on 20 ...
- MathExam V2.0
# 隔壁村小学的小朋友都羡慕哭了2.0版 一.预估与实际 PSP2.1 Personal Software Process Stages 预估耗时(分钟) 实际耗时(分钟) Planning 计划 1 ...
- vue 新增时清除表单验证注意事项
// 清除表单校验的提示 if (this.$refs['XXX']) { // 延时执行 this.$nextTick(function () { this.$refs['XXX'].clearVa ...
- word默认字体与大小
对于红色地方单击,“正文框”按右键+修改 修改字体大小 修改中文和西文时的字体 注意宋体和宋体 (中文正文)是不同的