use*_*796 39 jasmine selenium-webdriver protractor
任何人都可以告诉我如何使用jasmine框架编写测试用例以获取下载pdf文件的链接?提前致谢.
Leo*_*cci 46
我目前可以设置下载路径位置
capabilities: {
    'browserName': 'chrome',
    'platform': 'ANY',
    'version': 'ANY',
    'chromeOptions': {
        // Get rid of --ignore-certificate yellow warning
        args: ['--no-sandbox', '--test-type=browser'],
        // Set download path and avoid prompting for download even though
        // this is already the default on Chrome but for completeness
        prefs: {
            'download': {
                'prompt_for_download': false,
                'default_directory': '/e2e/downloads/',
            }
        }
    }
}
对于远程测试,您需要更复杂的基础架构,例如设置Samba共享或网络共享目录.
var FirefoxProfile = require('firefox-profile');
var q = require('q');
[...]
getMultiCapabilities: getFirefoxProfile,
framework: 'jasmine2',
[...]
function getFirefoxProfile() {
    "use strict";
    var deferred = q.defer();
    var firefoxProfile = new 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(function(encodedProfile) {
        var multiCapabilities = [{
            browserName: 'firefox',
            firefox_profile : encodedProfile
        }];
        deferred.resolve(multiCapabilities);
    });
    return deferred.promise;
}
最后也许很明显,要触发下载,您可以点击下载链接,例如
$('a.some-download-link').click();
Ste*_*lie 43
我需要检查下载文件的内容(在我的情况下为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超时是默认值,因此可以省略,但我将其留在提醒中以防有人想要更改它.
它可能是检查 href 属性的测试,如下所示:
var link = element(by.css("a.pdf"));
expect(link.getAttribute('href')).toEqual('someExactUrl');