Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion .eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -1479,7 +1479,8 @@ module.exports = {
'playwright/no-slowed-test': 'error',
'playwright/no-standalone-expect': 'error',
'playwright/no-unsafe-references': 'error',
'playwright/no-wait-for-selector': 'warn',
'playwright/no-useless-await': 'error',
'playwright/no-wait-for-selector': 'error',
'playwright/max-nested-describe': ['error', { max: 1 }],
'playwright/missing-playwright-await': 'error',
'playwright/prefer-comparison-matcher': 'error',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,19 @@ import { getRunTarget, stripRunCommand } from './cli_processing';
describe('cli_processing', () => {
describe('stripRunCommand', () => {
it(`should return the correct run command when started with 'npx'`, () => {
const argv = ['npx', 'playwright', 'test', '--config', 'path/to/config', '--grep=@svlSearch'];
const argv = [
'npx',
'playwright',
'test',
'--config',
'path/to/config',
'--project',
'local',
'--grep=@svlSearch',
];

expect(stripRunCommand(argv)).toBe(
'npx playwright test --config path/to/config --grep=@svlSearch'
'npx playwright test --config path/to/config --project local --grep=@svlSearch'
);
});

Expand All @@ -26,11 +35,13 @@ describe('cli_processing', () => {
'test',
'--config',
'path/to/config',
'--project',
'local',
'--grep=@svlSearch',
];

expect(stripRunCommand(argv)).toBe(
'npx playwright test --config path/to/config --grep=@svlSearch'
'npx playwright test --config path/to/config --project local --grep=@svlSearch'
);
});

Expand Down
4 changes: 3 additions & 1 deletion src/platform/packages/shared/kbn-scout/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -186,9 +186,11 @@ If the servers are already running, you can execute tests independently using ei
- Command Line: Use the following command to run tests:

```bash
npx playwright test --config <plugin-path>/ui_tests/playwright.config.ts
npx playwright test --config <plugin-path>/ui_tests/playwright.config.ts --project local
```

We use `project` flag to define test target, where tests to be run: local servers or Elastic Cloud. Currently we only support local servers.

### Contributing

We welcome contributions to improve and extend `kbn-scout`. This guide will help you get started, add new features, and align with existing project standards.
Expand Down
3 changes: 1 addition & 2 deletions src/platform/packages/shared/kbn-scout/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,10 @@ export {
expect,
test,
spaceTest,
globalSetupHook,
tags,
createPlaywrightConfig,
createLazyPageObject,
ingestTestDataHook,
ingestSynthtraceDataHook,
} from './src/playwright';
export type {
ScoutPlaywrightOptions,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,8 @@ describe('createPlaywrightConfig', () => {
expect(config.timeout).toBe(60000);
expect(config.expect?.timeout).toBe(10000);
expect(config.outputDir).toBe('./output/test-artifacts');
expect(config.projects![0].name).toEqual('chromium');
expect(config.projects).toHaveLength(1);
expect(config.projects![0].name).toEqual('local');
});

it('should return a Playwright configuration with Scout reporters', () => {
Expand Down Expand Up @@ -96,12 +97,17 @@ describe('createPlaywrightConfig', () => {
]);
});

it(`should override 'workers' count in Playwright configuration`, () => {
it(`should override 'workers' count and add 'setup' project dependency`, () => {
const testDir = './my_tests';
const workers = 2;

const config = createPlaywrightConfig({ testDir, workers });
expect(config.workers).toBe(workers);

expect(config.projects).toHaveLength(2);
expect(config.projects![0].name).toEqual('setup');
expect(config.projects![1].name).toEqual('local');
expect(config.projects![1]).toHaveProperty('dependencies', ['setup']);
});

it('should generate and cache runId in process.env.TEST_RUN_ID', () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,34 @@ export function createPlaywrightConfig(options: ScoutPlaywrightOptions): Playwri
process.env.TEST_RUN_ID = runId;
}

const scoutProjects: PlaywrightTestConfig<ScoutTestOptions>['projects'] = [
{
name: 'local',
use: { ...devices['Desktop Chrome'], configName: 'local' },
},
];

/**
* For parallel tests, we need to add a setup project that runs before the tests project.
*/
if (options.workers && options.workers > 1) {
const parentProject = scoutProjects.find((p) => p.use?.configName);

scoutProjects.unshift({
name: 'setup',
use: parentProject?.use ? { ...parentProject.use } : {},
testMatch: /global.setup\.ts/,
});

scoutProjects.forEach((project) => {
if (project.name !== 'setup') {
project.dependencies = ['setup'];
}
});
}

return defineConfig<ScoutTestOptions>({
testDir: options.testDir,
globalSetup: options.globalSetup,
/* Run tests in files in parallel */
fullyParallel: false,
/* Fail the build on CI if you accidentally left test.only in the source code. */
Expand All @@ -47,6 +72,7 @@ export function createPlaywrightConfig(options: ScoutPlaywrightOptions): Playwri
],
/* Shared settings for all the projects below. See https://playwright.dev/docs/api/class-testoptions. */
use: {
// 'configName' is not defined by default to enforce using '--project' flag when running the tests
testIdAttribute: 'data-test-subj',
serversConfigDir: SCOUT_SERVERS_ROOT,
[VALID_CONFIG_MARKER]: true,
Expand All @@ -70,24 +96,6 @@ export function createPlaywrightConfig(options: ScoutPlaywrightOptions): Playwri

outputDir: './output/test-artifacts', // For other test artifacts (screenshots, videos, traces)

/* Configure projects for major browsers */
projects: [
{
name: 'chromium',
use: { ...devices['Desktop Chrome'] },
},

// {
// name: 'firefox',
// use: { ...devices['Desktop Firefox'] },
// },
],

/* Run your local dev server before starting the tests */
// webServer: {
// command: 'npm run start',
// url: 'http://127.0.0.1:3000',
// reuseExistingServer: !process.env.CI,
// },
projects: scoutProjects,
});
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,13 @@
*/

import { mergeTests } from 'playwright/test';
import { apiFixtures, coreWorkerFixtures, scoutSpaceParallelFixture } from './worker';
import {
apiFixtures,
coreWorkerFixtures,
esArchiverFixture,
scoutSpaceParallelFixture,
synthtraceFixture,
} from './worker';
import type {
ApiParallelWorkerFixtures,
EsClient,
Expand Down Expand Up @@ -52,3 +58,10 @@ export interface ScoutParallelWorkerFixtures extends ApiParallelWorkerFixtures {
esClient: EsClient;
scoutSpace: ScoutSpaceParallelFixture;
}

export const globalSetup = mergeTests(
coreWorkerFixtures,
esArchiverFixture,
synthtraceFixture,
apiFixtures
);
Original file line number Diff line number Diff line change
Expand Up @@ -66,10 +66,15 @@ export const coreWorkerFixtures = base.extend<
*/
config: [
({ log }, use, workerInfo) => {
const configName = 'local';
const projectUse = workerInfo.project.use as ScoutTestOptions;
if (!projectUse.configName) {
throw new Error(
`Failed to read the 'configName' property. Make sure to run tests with '--project' flag and target enviroment (local or cloud),
e.g. 'npx playwright test --project local --config <path_to_Playwright.config.ts>'`
);
}
const serversConfigDir = projectUse.serversConfigDir;
const configInstance = createScoutConfig(serversConfigDir, configName, log);
const configInstance = createScoutConfig(serversConfigDir, projectUse.configName, log);

use(configInstance);
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,13 @@
*/

import { Readable } from 'stream';
import type { ApmFields, Fields, InfraDocument, OtelDocument } from '@kbn/apm-synthtrace-client';
import type {
ApmFields,
Fields,
InfraDocument,
OtelDocument,
SynthtraceGenerator,
} from '@kbn/apm-synthtrace-client';
import Url from 'url';
import type { SynthtraceEsClient } from '@kbn/apm-synthtrace/src/lib/shared/base_client';
import {
Expand All @@ -17,10 +23,9 @@ import {
getOtelSynthtraceEsClient,
} from '../../../common/services/synthtrace';
import { coreWorkerFixtures } from './core_fixtures';
import type { SynthtraceEvents } from '../../global_hooks/synthtrace_ingestion';

interface SynthtraceFixtureEsClient<TFields extends Fields> {
index: (events: SynthtraceEvents<TFields>) => Promise<void>;
index: (events: SynthtraceGenerator<TFields>) => Promise<void>;
clean: SynthtraceEsClient<TFields>['clean'];
}

Expand All @@ -34,15 +39,12 @@ const useSynthtraceClient = async <TFields extends Fields>(
client: SynthtraceEsClient<TFields>,
use: (client: SynthtraceFixtureEsClient<TFields>) => Promise<void>
) => {
const index = async (events: SynthtraceEvents<TFields>) =>
const index = async (events: SynthtraceGenerator<TFields>) =>
await client.index(Readable.from(Array.from(events).flatMap((event) => event.serialize())));

const clean = async () => await client.clean();

await use({ index, clean });

// cleanup function after all tests have ran
await client.clean();
};

export const synthtraceFixture = coreWorkerFixtures.extend<{}, SynthtraceFixture>({
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,9 @@ const getSynthtraceClient = (
}
};

/**
* @deprecated Use `globalSetupHook` and synthtrace fixtures instead
*/
export async function ingestSynthtraceDataHook(config: FullConfig, data: SynthtraceIngestionData) {
const log = getLogger();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,16 @@
* License v3.0 only", or the "Server Side Public License, v 1".
*/

import { scoutFixtures, scoutParallelFixtures } from './fixtures';
import { scoutFixtures, scoutParallelFixtures, globalSetup } from './fixtures';

// Scout core fixtures: worker & test scope
export const test = scoutFixtures;

// Scout core 'space aware' fixtures: worker & test scope
export const spaceTest = scoutParallelFixtures;

export const globalSetupHook = globalSetup;

export { createPlaywrightConfig } from './config';
export { createLazyPageObject } from './page_objects/utils';
export { expect } from './expect';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ describe('parseTestFlags', () => {
expect(result).toEqual({
mode: 'serverless=oblt',
configPath: '/path/to/config',
testTarget: 'local',
headed: false,
esFrom: undefined,
installDir: undefined,
Expand All @@ -82,6 +83,7 @@ describe('parseTestFlags', () => {
it(`should parse with correct config and stateful flags`, async () => {
const flags = new FlagsReader({
config: '/path/to/config',
testTarget: 'local',
stateful: true,
logToFile: false,
headed: true,
Expand All @@ -93,10 +95,41 @@ describe('parseTestFlags', () => {
expect(result).toEqual({
mode: 'stateful',
configPath: '/path/to/config',
testTarget: 'local',
headed: true,
esFrom: 'snapshot',
installDir: undefined,
logsDir: undefined,
});
});

it(`should throw an error with incorrect '--testTarget' flag`, async () => {
const flags = new FlagsReader({
config: '/path/to/config',
testTarget: 'a',
stateful: true,
logToFile: false,
headed: true,
esFrom: 'snapshot',
});

await expect(parseTestFlags(flags)).rejects.toThrow(
'invalid --testTarget, expected one of "local", "cloud"'
);
});

it(`should throw an error with incorrect '--testTarget' flag set to 'cloud'`, async () => {
const flags = new FlagsReader({
config: '/path/to/config',
testTarget: 'cloud',
stateful: true,
logToFile: false,
headed: true,
esFrom: 'snapshot',
});
validatePlaywrightConfigMock.mockResolvedValueOnce();
await expect(parseTestFlags(flags)).rejects.toThrow(
'Running tests against Cloud / MKI is not supported yet'
);
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ export interface RunTestsOptions {
configPath: string;
headed: boolean;
mode: CliSupportedServerModes;
testTarget: 'local' | 'cloud';
esFrom: 'serverless' | 'source' | 'snapshot' | undefined;
installDir: string | undefined;
logsDir: string | undefined;
Expand All @@ -27,18 +28,24 @@ export interface RunTestsOptions {
export const TEST_FLAG_OPTIONS: FlagOptions = {
...SERVER_FLAG_OPTIONS,
boolean: [...(SERVER_FLAG_OPTIONS.boolean || []), 'headed'],
string: [...(SERVER_FLAG_OPTIONS.string || []), 'config'],
default: { headed: false },
string: [...(SERVER_FLAG_OPTIONS.string || []), 'config', 'testTarget'],
default: { headed: false, testTarget: 'local' },
help: `${SERVER_FLAG_OPTIONS.help}
--config Playwright config file path
--headed Run Playwright with browser head
--testTarget Run tests agaist locally started servers or Cloud deployment / MKI project
`,
};

export async function parseTestFlags(flags: FlagsReader) {
const options = parseServerFlags(flags);
const configPath = flags.string('config');
const headed = flags.boolean('headed');
const testTarget = flags.enum('testTarget', ['local', 'cloud']) || 'local';

if (testTarget === 'cloud') {
throw createFlagError(`Running tests against Cloud / MKI is not supported yet`);
}

if (!configPath) {
throw createFlagError(`Path to playwright config is required: --config <file path>`);
Expand All @@ -51,5 +58,6 @@ export async function parseTestFlags(flags: FlagsReader) {
...options,
configPath,
headed,
testTarget,
};
}
Loading