2016-08-03 18:00:07 -04:00
|
|
|
/**
|
|
|
|
* @license
|
|
|
|
* Copyright Google Inc. All Rights Reserved.
|
|
|
|
*
|
|
|
|
* Use of this source code is governed by an MIT-style license that can be
|
|
|
|
* found in the LICENSE file at https://angular.io/license
|
|
|
|
*/
|
|
|
|
|
2016-08-24 16:39:44 -04:00
|
|
|
import {Provider, ReflectiveInjector} from '@angular/core';
|
2015-05-27 17:57:54 -04:00
|
|
|
|
2016-08-03 18:00:07 -04:00
|
|
|
import {Options} from './common_options';
|
|
|
|
import {Metric} from './metric';
|
|
|
|
import {MultiMetric} from './metric/multi_metric';
|
|
|
|
import {PerflogMetric} from './metric/perflog_metric';
|
|
|
|
import {UserMetric} from './metric/user_metric';
|
|
|
|
import {Reporter} from './reporter';
|
2015-05-27 17:57:54 -04:00
|
|
|
import {ConsoleReporter} from './reporter/console_reporter';
|
|
|
|
import {MultiReporter} from './reporter/multi_reporter';
|
2016-08-03 18:00:07 -04:00
|
|
|
import {SampleDescription} from './sample_description';
|
|
|
|
import {SampleState, Sampler} from './sampler';
|
|
|
|
import {Validator} from './validator';
|
2015-05-27 17:57:54 -04:00
|
|
|
import {RegressionSlopeValidator} from './validator/regression_slope_validator';
|
|
|
|
import {SizeValidator} from './validator/size_validator';
|
2016-08-03 18:00:07 -04:00
|
|
|
import {WebDriverAdapter} from './web_driver_adapter';
|
|
|
|
import {WebDriverExtension} from './web_driver_extension';
|
2015-05-27 17:57:54 -04:00
|
|
|
import {ChromeDriverExtension} from './webdriver/chrome_driver_extension';
|
2015-06-08 16:51:10 -04:00
|
|
|
import {FirefoxDriverExtension} from './webdriver/firefox_driver_extension';
|
2015-05-27 17:57:54 -04:00
|
|
|
import {IOsDriverExtension} from './webdriver/ios_driver_extension';
|
2016-08-03 18:00:07 -04:00
|
|
|
|
2015-05-27 17:57:54 -04:00
|
|
|
|
2016-08-26 19:34:08 -04:00
|
|
|
|
2015-05-27 17:57:54 -04:00
|
|
|
/**
|
|
|
|
* The Runner is the main entry point for executing a sample run.
|
|
|
|
* It provides defaults, creates the injector and calls the sampler.
|
|
|
|
*/
|
|
|
|
export class Runner {
|
2016-08-26 19:34:08 -04:00
|
|
|
constructor(private _defaultProviders: Provider[] = []) {}
|
2015-05-27 17:57:54 -04:00
|
|
|
|
2016-08-03 18:00:07 -04:00
|
|
|
sample({id, execute, prepare, microMetrics, providers, userMetrics}: {
|
|
|
|
id: string,
|
2016-08-26 19:34:08 -04:00
|
|
|
execute?: Function,
|
|
|
|
prepare?: Function,
|
|
|
|
microMetrics?: {[key: string]: string},
|
|
|
|
providers?: Provider[],
|
|
|
|
userMetrics?: {[key: string]: string}
|
2016-08-03 18:00:07 -04:00
|
|
|
}): Promise<SampleState> {
|
2016-11-12 08:08:58 -05:00
|
|
|
const sampleProviders: Provider[] = [
|
2016-08-03 18:00:07 -04:00
|
|
|
_DEFAULT_PROVIDERS, this._defaultProviders, {provide: Options.SAMPLE_ID, useValue: id},
|
2016-06-02 20:30:40 -04:00
|
|
|
{provide: Options.EXECUTE, useValue: execute}
|
2015-05-27 17:57:54 -04:00
|
|
|
];
|
2017-03-02 12:37:01 -05:00
|
|
|
if (prepare != null) {
|
2016-06-02 20:30:40 -04:00
|
|
|
sampleProviders.push({provide: Options.PREPARE, useValue: prepare});
|
2015-05-27 17:57:54 -04:00
|
|
|
}
|
2017-03-02 12:37:01 -05:00
|
|
|
if (microMetrics != null) {
|
2016-06-02 20:30:40 -04:00
|
|
|
sampleProviders.push({provide: Options.MICRO_METRICS, useValue: microMetrics});
|
2015-05-27 17:57:54 -04:00
|
|
|
}
|
2017-03-02 12:37:01 -05:00
|
|
|
if (userMetrics != null) {
|
2016-04-20 20:01:51 -04:00
|
|
|
sampleProviders.push({provide: Options.USER_METRICS, useValue: userMetrics});
|
|
|
|
}
|
2017-03-02 12:37:01 -05:00
|
|
|
if (providers != null) {
|
2016-03-21 06:57:17 -04:00
|
|
|
sampleProviders.push(providers);
|
2015-05-27 17:57:54 -04:00
|
|
|
}
|
2015-06-26 18:59:18 -04:00
|
|
|
|
2016-11-12 08:08:58 -05:00
|
|
|
const inj = ReflectiveInjector.resolveAndCreate(sampleProviders);
|
|
|
|
const adapter: WebDriverAdapter = inj.get(WebDriverAdapter);
|
2015-06-26 18:59:18 -04:00
|
|
|
|
2016-08-02 18:53:34 -04:00
|
|
|
return Promise
|
2015-06-26 18:59:18 -04:00
|
|
|
.all([adapter.capabilities(), adapter.executeScript('return window.navigator.userAgent;')])
|
|
|
|
.then((args) => {
|
2016-11-12 08:08:58 -05:00
|
|
|
const capabilities = args[0];
|
|
|
|
const userAgent = args[1];
|
2015-06-26 18:59:18 -04:00
|
|
|
|
|
|
|
// This might still create instances twice. We are creating a new injector with all the
|
2015-10-11 01:11:13 -04:00
|
|
|
// providers.
|
2015-06-26 18:59:18 -04:00
|
|
|
// Only WebDriverAdapter is reused.
|
|
|
|
// TODO vsavkin consider changing it when toAsyncFactory is added back or when child
|
|
|
|
// injectors are handled better.
|
2016-11-12 08:08:58 -05:00
|
|
|
const injector = ReflectiveInjector.resolveAndCreate([
|
2016-08-03 18:00:07 -04:00
|
|
|
sampleProviders, {provide: Options.CAPABILITIES, useValue: capabilities},
|
2016-06-02 20:30:40 -04:00
|
|
|
{provide: Options.USER_AGENT, useValue: userAgent},
|
|
|
|
{provide: WebDriverAdapter, useValue: adapter}
|
2015-06-26 18:59:18 -04:00
|
|
|
]);
|
|
|
|
|
2016-11-12 08:08:58 -05:00
|
|
|
const sampler = injector.get(Sampler);
|
2015-06-26 18:59:18 -04:00
|
|
|
return sampler.sample();
|
|
|
|
});
|
2015-05-27 17:57:54 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-11-12 08:08:58 -05:00
|
|
|
const _DEFAULT_PROVIDERS = [
|
2015-10-11 01:11:13 -04:00
|
|
|
Options.DEFAULT_PROVIDERS,
|
2016-03-21 06:57:17 -04:00
|
|
|
Sampler.PROVIDERS,
|
|
|
|
ConsoleReporter.PROVIDERS,
|
|
|
|
RegressionSlopeValidator.PROVIDERS,
|
|
|
|
SizeValidator.PROVIDERS,
|
|
|
|
ChromeDriverExtension.PROVIDERS,
|
|
|
|
FirefoxDriverExtension.PROVIDERS,
|
|
|
|
IOsDriverExtension.PROVIDERS,
|
|
|
|
PerflogMetric.PROVIDERS,
|
2016-08-03 15:32:26 -04:00
|
|
|
UserMetric.PROVIDERS,
|
2016-03-21 06:57:17 -04:00
|
|
|
SampleDescription.PROVIDERS,
|
2016-08-26 19:34:08 -04:00
|
|
|
MultiReporter.provideWith([ConsoleReporter]),
|
|
|
|
MultiMetric.provideWith([PerflogMetric, UserMetric]),
|
|
|
|
{provide: Reporter, useExisting: MultiReporter},
|
|
|
|
{provide: Validator, useExisting: RegressionSlopeValidator},
|
|
|
|
WebDriverExtension.provideFirstSupported(
|
|
|
|
[ChromeDriverExtension, FirefoxDriverExtension, IOsDriverExtension]),
|
|
|
|
{provide: Metric, useExisting: MultiMetric},
|
2015-05-27 17:57:54 -04:00
|
|
|
];
|