不借助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 ...
随机推荐
- swoole多进程
<?php /** * Created by PhpStorm. * User: brady * Date: 2018/11/19 * Time: 16:29 */ $workers = []; ...
- 【转帖】Git学习笔记 记录一下
本文内容参考了廖雪峰老师的博文,并做了适当整理,方便大家查阅. 原帖地址 https://wangfanggang.com/Git/git/ 常用命令 仓库初始化 - git init 1 git i ...
- pixi.js + three.js
three.js 最好的webgl 3d渲染库之一, pixi.js 最好的webgl 2d渲染库之一,也许可以把之一去掉 两个库都很精简,如果把两个库结合起来,一定很爽很爽,你说是吧! ...
- maven的pom.xml举例
<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://mave ...
- c++ std::function
std::function 是一个模板类,用于封装各种类似于函数这样的对象,例如普通函数,仿函数,匿名函数等等.其强大的多态能力,让其使用只依赖于调用特征.在程序的升级中,可以实现一个调用表,以兼容新 ...
- MT【110】巧设法向量解决距离问题
分析:设面的法向量$n=(x,y,z)$
- BZOJ 4569 [Scoi2016]萌萌哒 | ST表 并查集
传送门 BZOJ 4569 题解 ST表和并查集是我认为最优雅(其实是最好写--)的两个数据结构. 然鹅!他俩加一起的这道题,我却--没有做出来-- 咳咳. 正解是这样的: 类似ST表有\(\log ...
- solr的基本使用
Solr 概念: 1. 搜索引擎的技术,建立在Lucene之上,可以解决跨平台,跨语言的问题.(Lucene本身是个jar包,也就是API,不能独立运行,需要程序的调用来完成全局检索,不具备跨平台,跨 ...
- 利用EF和C#泛型实现通用分页查询
利用EF和C#泛型实现通用分页查询 Entity Framework 是微软以 ADO.NET 为基础所发展出来的对象关系对应 (ORM) 解决方案,是微软的ORM框架.此框架将数据库中的 ...
- 缓存面板获取之前页面选中的数据Objuid的方法
String partUid = request.getParameter("contextInstanceUid"); contextInstanceUid是存在总线总的键名,存 ...