Selenium webdriver 用于下载pdf文件的量角器e2e测试用例

Selenium webdriver 用于下载pdf文件的量角器e2e测试用例,selenium-webdriver,jasmine,protractor,Selenium Webdriver,Jasmine,Protractor,有谁能告诉我如何使用jasmine框架为下载pdf文件的链接编写测试用例? 提前感谢。这可能是检查href属性的测试,如下所示: var link = element(by.css("a.pdf")); expect(link.getAttribute('href')).toEqual('someExactUrl'); 我当前可以设置下载路径位置 铬 功能:{ 'browserName':'chrome', '平台':'任何', '版本':'任何', “颜色选项”:{ //清除--忽略证书黄色

有谁能告诉我如何使用jasmine框架为下载pdf文件的链接编写测试用例?
提前感谢。

这可能是检查href属性的测试,如下所示:

var link = element(by.css("a.pdf"));
expect(link.getAttribute('href')).toEqual('someExactUrl');

我当前可以设置下载路径位置

功能:{
'browserName':'chrome',
'平台':'任何',
'版本':'任何',
“颜色选项”:{
//清除--忽略证书黄色警告
参数:['--no sandbox','--test type=browser'],
//设置下载路径并避免提示下载,即使
//这已经是Chrome的默认设置,但出于完整性考虑
首选项:{
“下载”:{
“提示下载”:false,
“默认目录”:“/e2e/downloads/”,
}
}
}
}
对于远程测试,您需要一个更复杂的基础设施,如设置Samba共享或网络共享目录目的地

火狐

var FirefoxProfile=require('firefox-profile');
var q=要求('q');
[...]
getMultiCapabilities:getFirefoxProfile,
框架:“jasmine2”,
[...]
函数getFirefoxProfile(){
“严格使用”;
var deferred=q.deferred();
var firefoxProfile=新的firefoxProfile();
firefoxProfile.setPreference(“browser.download.folderList”,2);
firefoxProfile.setPreference(“browser.download.manager.showWhenStarting”,false);
firefoxProfile.setPreference(“browser.download.dir”,'/tmp');
firefoxProfile.setPreference(“browser.helperApps.neverAsk.saveToDisk”、“application/vnd.openxmlformats officedocument.wordprocessingml.document”);
firefoxProfile.encoded(函数(encodedProfile){
var多容量=[{
browserName:“firefox”,
firefox_配置文件:encodedProfile
}];
延迟。解决(多容量);
});
回报。承诺;
}
最后,也可能是显而易见的,要触发下载,您可以点击下载链接,如您所知

$('a.some-download-link').click();

我过去做过的一件事是使用HTTP HEAD命令。基本上,它与“GET”相同,但它只检索头

不幸的是,web服务器需要明确支持“HEAD”。如果是这样,您可以实际尝试URL,然后在内容类型中检查“application/pdf”,而无需实际下载文件


如果服务器未设置为支持HEAD,您可能只需像上面建议的那样检查链接文本。

我需要根据预期结果检查下载文件(在我的情况下是CSV导出)的内容,并发现以下内容有效:

var filename = '/tmp/export.csv';
var fs = require('fs');

if (fs.existsSync(filename)) {
    // Make sure the browser doesn't have to rename the download.
    fs.unlinkSync(filename);
}

$('a.download').click();

browser.driver.wait(function() {
    // Wait until the file has been downloaded.
    // We need to wait thus as otherwise protractor has a nasty habit of
    // trying to do any following tests while the file is still being
    // downloaded and hasn't been moved to its final location.
    return fs.existsSync(filename);
}, 30000).then(function() {
    // Do whatever checks you need here.  This is a simple comparison;
    // for a larger file you might want to do calculate the file's MD5
    // hash and see if it matches what you expect.
    expect(fs.readFileSync(filename, { encoding: 'utf8' })).toEqual(
        "A,B,C\r\n"
    );
});
我发现Leo的配置建议有助于将下载保存在可访问的地方


30000ms超时是默认值,因此可以省略,但我保留它作为提醒,以防有人想更改它。

上述解决方案不适用于远程浏览器测试,例如通过BrowserStack。仅针对Chrome的替代解决方案可能如下所示:

if((等待browser.getCapabilities()).get('browserName')='chrome'){
等待browser.driver.get命令chrome://downloads/');
常数项=
等待browser.executeScript('return downloads.Manager.get().items)作为任何[];
预期(项目长度)为(1);
expect(items[0]。file_name).toBe('some.pdf');
}

我很高兴我的回答有帮助!:)我还编写了一个助手函数waitFileExists(fileAbsPath);)关于phantomjs呢?@LeoGallucci您能分享一下您在
waitFileExists(fileAbsPath)
中所做的事情吗?您是否使用该方法获取
文件abspath
以便它可以在任何计算机上运行?您是什么意思,代码就在上面,请参阅“等待文件下载”。你好,Leo,我正在尝试为Chrome设置默认路径位置,但显然您的解决方案对我不起作用。@LeoGallucci使用上述代码,Firefox不会生成,也没有错误。但如果我在export.config中使用browserName:“firefox”,它会成功打开firefox。原因可能是什么?谢谢是的,当然,如果你想使用Firefox,你需要设置browserName:'Firefox'@LeoGallucci我需要使用1个配置文件并将browserName作为参数传递,我知道如何实现这一点,我看到对于'Firefox',使用了“getMultiCapabilities”,对于'CHROME',使用了“capabilities”。非常感谢您的帮助。是否有人成功地为Internet Explorer开发了相同的工具?