【问题标题】:Download file to given absolute path in Firefox using Protractor使用量角器将文件下载到 Firefox 中的给定绝对路径
【发布时间】:2017-12-15 19:30:20
【问题描述】:

我正在使用 Protractor 进行 E2E 测试。在自动化过程中,我需要将文件下载到系统中的 C:\Automation 文件夹中。但是下面的代码不起作用。

注意:在自动化执行期间,“另存为”弹出窗口打开(但我以后必须禁用它),然后我手动单击“保存”选项。它保存在默认位置,即下载文件夹。如何让它保存在我给定的路径中。

let profile = require('firefox-profile');        
let firefoxProfile = new profile();

//_browser = 'chrome';
_browser = 'firefox';
// _browser = 'internet explorer';

firefoxProfile.setPreference("browser.download.folderList", 2);
firefoxProfile.setPreference('browser.download.dir', "C:\\Automation");

exports.config = {
framework: 'custom',
frameworkPath: require.resolve('protractor-cucumber-framework'),
capabilities: {
    'browserName': _browser,
    'shardTestFiles': false,
    'maxInstances': 1,
    'acceptInsecureCerts': true,
    'moz:firefoxOptions': {
    'profile': firefoxProfile
    }},
beforeLaunch: function () {...}
}

【问题讨论】:

    标签: firefox protractor firefox-profile


    【解决方案1】:

    看起来您可能只是缺少一些偏好,使其无法与 Firefox 一起使用。尝试添加这些,看看是否有帮助。

    profile.setPreference( "browser.download.manager.showWhenStarting", false );
    profile.setPreference( "browser.helperApps.neverAsk.saveToDisk", 
      /* A comma-separated list of MIME types to save to disk without asking goes here */ );
    

    【讨论】:

      【解决方案2】:

      这将保存到项目内的下载文件夹中。您可以尝试调整它以保存到所需的文件夹。您必须指定在没有提示的情况下下载哪些类型的文件。 JSON 和 csv 已经存在。

      var q = require('q');
      var path = require('path');
      var sh = require("shelljs");
      var cwd = sh.pwd().toString();
      
      var FirefoxProfile = require('selenium-webdriver/firefox').Profile;
      
      var makeFirefoxProfile = function(preferenceMap) {
          var profile = new FirefoxProfile();
          for (var key in preferenceMap) {
              profile.setPreference(key, preferenceMap[key]);
          }
          return q.resolve({
              browserName: 'firefox',
              marionette: true,
              firefox_profile: profile
          });
      };
      
      exports.config = {
          seleniumAddress: 'http://localhost:4444/wd/hub',
          framework: 'jasmine2',
          getMultiCapabilities: function() {
              return q.all([
                  makeFirefoxProfile(
                      {
                          'browser.download.folderList': 2,
                          'browser.download.dir': (path.join(cwd, 'downloads')).toString(),
                          'browser.download.manager.showWhenStarting': false,
                          'browser.helperApps.alwaysAsk.force': false,
                          'browser.download.manager.useWindow': false,
                          'browser.helperApps.neverAsk.saveToDisk': 'application/octet-stream, application/json, text/comma-separated-values, text/csv, application/csv, application/excel, application/vnd.ms-excel, application/vnd.msexcel, text/anytext, text/plaintext'
                      }
                  )
              ]);
          },
          allScriptsTimeout: 1000000,
          specs: ['./tmp/**/*.spec.js'],
      
          jasmineNodeOpts: {
              defaultTimeoutInterval: 1000000,
              showColors: true
          },
          onPrepare: function() {
              browser.driver.getCapabilities().then(function(caps) {
                  browser.browserName = caps.get('browserName');
              });
      
              setTimeout(function() {
                  browser.driver.executeScript(function() {
                      return {
                          width: window.screen.availWidth,
                          height: window.screen.availHeight
                      };
                  }).then(function(result) {
                      browser.driver.manage().window().setPosition(0,0);
                      browser.driver.manage().window().setSize(result.width, result.height);
                  });
              });
          }
      };
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-08-28
        • 1970-01-01
        • 2017-10-22
        • 2012-10-15
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多