代码之家  ›  专栏  ›  技术社区  ›  Alexandre

OnPrepare with Jasmine Reporters在使用IE执行时会导致失败

  •  1
  • Alexandre  · 技术社区  · 7 年前

    经过一段时间的尝试,我发现为什么我的测试开始失败(仅对于IE,Chrome工作正常),当涉及到代码的这一部分时,它是由on-prepare函数引起的:

      jasmine.getEnv().addReporter({
        specDone: function (result) {
          browser.getCapabilities().then(function (caps) 
          {
            var browserName = caps.get('browserName');
            browser.takeScreenshot().then(function (png) {
              var stream = fs.createWriteStream('./execution_results/reports/results/screenshots/' + browserName + '-' + result.fullName+ '.png');
              stream.write(new Buffer.from(png, 'base64'));
              stream.end();
            });
          });
        }
      });
    

    如果我对这部分进行评论,测试将顺利进行。 我的登录页面不是有角度的,所以我关闭了登录的同步并再次打开,不确定这是否相关。

    在继续运行之前,如何强制量角器等待此部分完成?

    我已经尝试将此代码添加到承诺(在conf文件中)中,以使量角器等待,但即使这样,我也会得到jasmine timeout“timeouterrror:wait timeout after 20000ms”,因此我认为我做错了。

    我得到的错误是:

    Failed: Unable to determine type from: E. Last 1 characters read: E
    Build info: version: '3.141.59', revision: 'e82be7d358', time: '2018-11-14T08:25:53'
    System info: host: 'xxxxx', ip: 'xx.xx.xx.xx', os.name: 'Windows 10', os.arch: 'amd64', os.version: '10.0', java.version: '10.0.2'
    Driver info: driver.version: unknown
    

    全CONF文件:

    var jasmineReporters = require('./lib/node_modules/jasmine-reporters');
    var HTMLReport = require('./lib/node_modules/protractor-html-reporter-2');
    var mkdirp = require('./lib/node_modules/mkdirp');
    var fs = require('./lib/node_modules/fs-extra');
    let date = require('./lib/node_modules/date-and-time');  
    
    var environmentToExecute = 'https://myportal' 
    
    exports.config = {
    
    seleniumAddress: 'http://'+process.env.AUTOTEST_ADDRESS+'/wd/hub',
    
    framework: 'jasmine2',
    
    specs: ['all my specs'],
    
    suites: {
      //All my suites
    },
    
    allScriptsTimeout: 20000,
    
    onPrepare: function () {   
      {
       //Here I create the folders (removed to make it shorter)
      }
    
      jasmine.getEnv().addReporter(new jasmineReporters.JUnitXmlReporter({
        consolidateAll: true,
        savePath: './execution_results/reports/xml/',
        filePrefix: 'xmlresults'
      }));
    
      jasmine.getEnv().addReporter({
        specDone: function (result) {
          browser.getCapabilities().then(function (caps) 
          {
            var browserName = caps.get('browserName');
            browser.takeScreenshot().then(function (png) {
              var stream = fs.createWriteStream('./execution_results/reports/results/screenshots/' + browserName + '-' + result.fullName+ '.png');
              stream.write(new Buffer.from(png, 'base64'));
              stream.end();
            });
          });
        }
      });
    },
    
    //HTMLReport called once tests are finished
    onComplete: function() 
    {
      //I removed this to make it shorter, but basically it is the function
      // that comverts the xml in html and build the report
    },
    
    jasmineNodeOpts: {
      showColors: true, // Use colors in the command line report.
      // If true, display spec names.
      isVerbose: true,
      defaultTimeoutInterval: 100000
    },
    
    params: {
        //Other files like functions and so on...
      },
      login:{
        //parameters to login
      }
    },
    
    multiCapabilities:
    [
     {
       'browserName': 'internet explorer',
       'version': 11,
     },
     /*   
     //chrome, firefox...
     */
    ],
    
    };//end of Conf.js
    

    谢谢!

    1 回复  |  直到 7 年前
        1
  •  1
  •   Alexandre    7 年前

    我最近在Jasmine的一个记者身上也遇到过异步操作的问题,不幸的是,在继续之前,我还没弄清楚如何让他们正确地等待承诺的结果。如果有人知道这方面的信息,我也非常感谢。

    我确实实现了一个使用全局变量和Afterall钩子的解决方案,它能够正确地等待可能对您有用的承诺。 我假设您只需要结果的“fullname”属性,这样您就可以尝试此操作。

    在onPrepare中声明一个全局属性,您可以在报告程序中分配这个全局变量值。将spec fullname值赋给它 启动的 而不是 斯芬登 . 然后,您可以在测试后的内部创建屏幕截图,这些语句能够正确地等待承诺的结果。

    onPrepare: function () {   
       global.currentlyExecutingSpec = 'tbd';
    
      jasmine.getEnv().addReporter({
        specStarted: function (result) {
          currentlyExecutingSpec = result.fullName
        }
      })
      jasmine.getEnv().addReporter(new jasmineReporters.JUnitXmlReporter({
        consolidateAll: true,
        savePath: './execution_results/reports/xml/',
        filePrefix: 'xmlresults'
      }));
    }
    

    在测试文件中

    afterEach(function(){
      browser.getCapabilities().then(function (caps) 
      {
          var browserName = caps.get('browserName');
          browser.takeScreenshot().then(function (png) {
            var stream = 
            fs.createWriteStream('./execution_results/reports/results/screenshots/' + browserName + '-' + currentlyExecutingSpec + '.png');
            stream.write(new Buffer.from(png, 'base64'));
            stream.end();
          });
      };
    });