今天尝试了下不用借助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. Android map转json格式,附上Jackson包下载地址,导入过程

    android中的map转json,需要下载jackson包,下载地址: http://www.java2s.com/Code/Jar/j/Downloadjacksonall199jar.htm 下 ...

  2. PAT 甲级 1090 Highest Price in Supply Chain

    https://pintia.cn/problem-sets/994805342720868352/problems/994805376476626944 A supply chain is a ne ...

  3. [转帖]"微信支付"勒索病毒制造者被锁定 传播、危害和疫情终极解密 --- 可以学习下一年火绒团队的分析原理的精神.

    "微信支付"勒索病毒制造者被锁定 传播.危害和疫情终极解密 https://www.cnbeta.com/articles/tech/794851.htm 12月1日,首个要求&q ...

  4. wordApp.Documents.Open 未将对象引用实例

    wordApp.Documents.Open (.........),当我打开的是.docx,能正常打开当是.doc时,却返回空置,显示失败,未能找到文件.......,但其实文件都在 解决方案 WO ...

  5. Vue入门---事件与方法详解

    一. vue方法实现 <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> &l ...

  6. Cannot read lifecycle mapping metadata for artifact org.apache.maven.plugins问题的解决

    今天换了个maven仓库,结果新建maven工程的时候,忽然报错: Cannot read lifecycle mapping metadata for artifact org.apache.mav ...

  7. 【刷题】BZOJ 1487 [HNOI2009]无归岛

    Description Neverland是个神奇的地方,它由一些岛屿环形排列组成,每个岛上都生活着之中与众不同的物种.但是这些物种都有一个共同的生活习性:对于同一个岛上的任意两个生物,他们有且仅有一 ...

  8. UVAlive-7040 color(组合数学,二项式反演)

    链接:vjudge 题目大意:有一排方格共 $n$ 个,现在有 $m$ 种颜色,要给这些方格染色,要求相邻两个格子的颜色不能相同.现在问恰好用了 $k$ 种颜色的合法方案数.答案对 $10^9+7$ ...

  9. 三、spring boot 1.5.4 web容器定制(端口号等修改)

    spring boot 默认采用tomcat作为嵌入的web容器 定制方式有三种 1. 2.如下 @Component public class CustomizationBean implement ...

  10. np.random.choice方法

    np.random.choice方法 觉得有用的话,欢迎一起讨论相互学习~Follow Me def choice(a, size=None, replace=True, p=None) 表示从a中随 ...