const wd = require('wd');
const Promise = require('bluebird');
const browser = require('./browser');
const normalise = require('./normalise-logs');

function test (opts) {
  return function () {
    return Promise.using(browser(opts.browser), (session) => {
      return session
        .get(opts.url)
        .then(() => {
          return Promise.resolve()
            .then(() => {
              if (opts.scroll || opts.reporter === 'fps') {
                return session.execute('function f () { window.scrollBy(0,5); window.requestAnimationFrame(f); } window.requestAnimationFrame(f);');
              }
            })
            .then(() => {
              if (typeof opts.inject === 'function') {
                return opts.inject(session);
              }
            })
            .then(() => {
              return session.waitFor(wd.asserters.jsCondition(`document.readyState==='complete'`), 120000)
                .catch(() => {
                  console.error('Page loading timed out after 120s');
                });
            })
            .then(() => {
              return session;
            });
        })
        .sleep(opts.sleep)
        .log('performance')
        .then((logs) => {
          return normalise(logs, opts.url);
        });
    });
  };
}

module.exports = test;

const wd = require('wd');
const Promise = require('bluebird');
const browser = require('./browser');
const normalise = require('./normalise-logs');

function test (opts) {
  return function () {
    return Promise.using(browser(opts.browser), (session) => {
      return session
        .setWindowSize(1024, 768)
        .get(opts.url)
        .then(() => {
          return Promise.resolve()
            .then(() => {
              if (opts.scroll || opts.reporter === 'fps') {
                return session.execute('function f () { window.scrollBy(0,5); window.requestAnimationFrame(f); } window.requestAnimationFrame(f);');
              }
            })
            .then(() => {
              if (typeof opts.inject === 'function') {
                return opts.inject(session);
              }
            })
            .then(() => {
              return session.waitFor(wd.asserters.jsCondition(`document.readyState==='complete'`), 120000)
                .catch(() => {
                  console.error('Page loading timed out after 120s');
                });
            })
            .then(() => {
              return session;
            });
        })
        .sleep(opts.sleep)
        .log('performance')
        .then((logs) => {
          return normalise(logs, opts.url);
        });
    });
  };
}

module.exports = test;

Fix window size before running tests
Fix window size before running tests

To make tests as repeatable as possible.
