@serenity-js/protractor
Version:
Adapter that integrates @serenity-js/web with Protractor, enabling Serenity/JS reporting and using the Screenplay Pattern to write end-to-end test scenarios
89 lines (72 loc) • 3.19 kB
text/typescript
import type { TestRunnerAdapter } from '@serenity-js/core/lib/adapter';
import type { Config as ProtractorConfig } from 'protractor';
import type { TestRunnerLoader } from './TestRunnerLoader';
/**
* Detects the [`TestRunnerAdapter`](https://serenity-js.org/api/core-adapter/interface/TestRunnerAdapter/) to use,
* based on Protractor configuration.
*
* @group Integration
*/
export class TestRunnerDetector {
static cucumberOpts = 'cucumberOpts';
static jasmineNodeOpts = 'jasmineNodeOpts';
static mochaOpts = 'mochaOpts';
static protractorCliOptions(): string[] {
return [
TestRunnerDetector.cucumberOpts,
TestRunnerDetector.jasmineNodeOpts,
TestRunnerDetector.mochaOpts,
];
}
constructor(private readonly testRunnerLoader: TestRunnerLoader) {
}
runnerFor(config: ProtractorConfig): TestRunnerAdapter {
const
specifiesRunnerFor = (type: string) =>
!!config.serenity &&
!!config.serenity.runner &&
config.serenity.runner === type;
switch (true) {
case specifiesRunnerFor('cucumber'):
return this.useCucumber(config);
case specifiesRunnerFor('jasmine'):
return this.useJasmine(config);
case specifiesRunnerFor('mocha'):
return this.useMocha(config);
case !! config.cucumberOpts:
return this.useCucumber(config);
case !! config.mochaOpts:
return this.useMocha(config);
case !! config.jasmineNodeOpts: // eslint-disable-line unicorn/no-useless-switch-case
default:
return this.useJasmine(config);
}
}
private useJasmine(config: ProtractorConfig): TestRunnerAdapter {
return this.testRunnerLoader.forJasmine(this.mergedConfigFor(config, TestRunnerDetector.jasmineNodeOpts));
}
private useMocha(config: ProtractorConfig): TestRunnerAdapter {
return this.testRunnerLoader.forMocha(this.mergedConfigFor(config, TestRunnerDetector.mochaOpts));
}
private useCucumber(config: ProtractorConfig): TestRunnerAdapter {
const serenityReportingServicesConfigured = config?.serenity?.crew?.length > 0;
return this.testRunnerLoader.forCucumber(this.mergedConfigFor(config, TestRunnerDetector.cucumberOpts), {
useStandardOutput: serenityReportingServicesConfigured,
uniqueFormatterOutputs: this.multiCapabilitiesOrTestShardingEnabled(config),
})
}
private mergedConfigFor<K extends keyof ProtractorConfig>(config: ProtractorConfig = {}, key: K): ProtractorConfig[K] {
return Object.assign(
{},
config[key],
(config.capabilities || {})[key],
);
}
private multiCapabilitiesOrTestShardingEnabled(config: ProtractorConfig): boolean {
return !! (
(Array.isArray(config.multiCapabilities) && config.multiCapabilities.length > 0)
|| typeof config.getMultiCapabilities === 'function'
|| config.capabilities?.shardTestFiles
);
}
}