github.com/readium/readium-lcp-server@v0.0.0-20240101192032-6e95190e99f1/frontend/manage/protractor.config.js (about)

     1  // FIRST TIME ONLY- run:
     2  //   ./node_modules/.bin/webdriver-manager update
     3  //
     4  //   Try: `npm run webdriver:update`
     5  //
     6  // AND THEN EVERYTIME ...
     7  //   1. Compile with `tsc`
     8  //   2. Make sure the test server (e.g., http-server: localhost:8080) is running.
     9  //   3. ./node_modules/.bin/protractor protractor.config.js
    10  //
    11  //   To do all steps, try:  `npm run e2e`
    12  
    13  var fs = require('fs');
    14  var path = require('canonical-path');
    15  var _ = require('lodash');
    16  
    17  
    18  exports.config = {
    19    directConnect: true,
    20  
    21    // Capabilities to be passed to the webdriver instance.
    22    capabilities: {
    23      'browserName': 'chrome'
    24    },
    25  
    26    // Framework to use. Jasmine is recommended.
    27    framework: 'jasmine',
    28  
    29    // Spec patterns are relative to this config file
    30    specs: ['**/*e2e-spec.js' ],
    31  
    32  
    33    // For angular tests
    34    useAllAngular2AppRoots: true,
    35  
    36    // Base URL for application server
    37    baseUrl: 'http://localhost:8080',
    38  
    39    // doesn't seem to work.
    40    // resultJsonOutputFile: "foo.json",
    41  
    42    onPrepare: function() {
    43      //// SpecReporter
    44      //var SpecReporter = require('jasmine-spec-reporter');
    45      //jasmine.getEnv().addReporter(new SpecReporter({displayStacktrace: 'none'}));
    46      //// jasmine.getEnv().addReporter(new SpecReporter({displayStacktrace: 'all'}));
    47  
    48      // debugging
    49      // console.log('browser.params:' + JSON.stringify(browser.params));
    50      jasmine.getEnv().addReporter(new Reporter( browser.params )) ;
    51  
    52      // Allow changing bootstrap mode to NG1 for upgrade tests
    53      global.setProtractorToNg1Mode = function() {
    54        browser.useAllAngular2AppRoots = false;
    55        browser.rootEl = 'body';
    56      };
    57    },
    58  
    59    jasmineNodeOpts: {
    60      // defaultTimeoutInterval: 60000,
    61      defaultTimeoutInterval: 10000,
    62      showTiming: true,
    63      print: function() {}
    64    }
    65  };
    66  
    67  // Custom reporter
    68  function Reporter(options) {
    69    var _defaultOutputFile = path.resolve(process.cwd(), './_test-output', 'protractor-results.txt');
    70    options.outputFile = options.outputFile || _defaultOutputFile;
    71  
    72    initOutputFile(options.outputFile);
    73    options.appDir = options.appDir ||  './';
    74    var _root = { appDir: options.appDir, suites: [] };
    75    log('AppDir: ' + options.appDir, +1);
    76    var _currentSuite;
    77  
    78    this.suiteStarted = function(suite) {
    79      _currentSuite = { description: suite.description, status: null, specs: [] };
    80      _root.suites.push(_currentSuite);
    81      log('Suite: ' + suite.description, +1);
    82    };
    83  
    84    this.suiteDone = function(suite) {
    85      var statuses = _currentSuite.specs.map(function(spec) {
    86        return spec.status;
    87      });
    88      statuses = _.uniq(statuses);
    89      var status = statuses.indexOf('failed') >= 0 ? 'failed' : statuses.join(', ');
    90      _currentSuite.status = status;
    91      log('Suite ' + _currentSuite.status + ': ' + suite.description, -1);
    92    };
    93  
    94    this.specStarted = function(spec) {
    95  
    96    };
    97  
    98    this.specDone = function(spec) {
    99      var currentSpec = {
   100        description: spec.description,
   101        status: spec.status
   102      };
   103      if (spec.failedExpectations.length > 0) {
   104        currentSpec.failedExpectations = spec.failedExpectations;
   105      }
   106  
   107      _currentSuite.specs.push(currentSpec);
   108      log(spec.status + ' - ' + spec.description);
   109    };
   110  
   111    this.jasmineDone = function() {
   112      outputFile = options.outputFile;
   113      //// Alternate approach - just stringify the _root - not as pretty
   114      //// but might be more useful for automation.
   115      // var output = JSON.stringify(_root, null, 2);
   116      var output = formatOutput(_root);
   117      fs.appendFileSync(outputFile, output);
   118    };
   119  
   120    function ensureDirectoryExistence(filePath) {
   121      var dirname = path.dirname(filePath);
   122      if (directoryExists(dirname)) {
   123        return true;
   124      }
   125      ensureDirectoryExistence(dirname);
   126      fs.mkdirSync(dirname);
   127    }
   128  
   129    function directoryExists(path) {
   130      try {
   131        return fs.statSync(path).isDirectory();
   132      }
   133      catch (err) {
   134        return false;
   135      }
   136    }
   137  
   138    function initOutputFile(outputFile) {
   139      ensureDirectoryExistence(outputFile);
   140      var header = "Protractor results for: " + (new Date()).toLocaleString() + "\n\n";
   141      fs.writeFileSync(outputFile, header);
   142    }
   143  
   144    // for output file output
   145    function formatOutput(output) {
   146      var indent = '  ';
   147      var pad = '  ';
   148      var results = [];
   149      results.push('AppDir:' + output.appDir);
   150      output.suites.forEach(function(suite) {
   151        results.push(pad + 'Suite: ' + suite.description + ' -- ' + suite.status);
   152        pad+=indent;
   153        suite.specs.forEach(function(spec) {
   154          results.push(pad + spec.status + ' - ' + spec.description);
   155          if (spec.failedExpectations) {
   156            pad+=indent;
   157            spec.failedExpectations.forEach(function (fe) {
   158              results.push(pad + 'message: ' + fe.message);
   159            });
   160            pad=pad.substr(2);
   161          }
   162        });
   163        pad = pad.substr(2);
   164        results.push('');
   165      });
   166      results.push('');
   167      return results.join('\n');
   168    }
   169  
   170    // for console output
   171    var _pad;
   172    function log(str, indent) {
   173      _pad = _pad || '';
   174      if (indent == -1) {
   175        _pad = _pad.substr(2);
   176      }
   177      console.log(_pad + str);
   178      if (indent == 1) {
   179        _pad = _pad + '  ';
   180      }
   181    }
   182  
   183  }