Skip to content

Commit

Permalink
test: implement e2e tests on analytics
Browse files Browse the repository at this point in the history
  • Loading branch information
shadowusr committed Jan 3, 2025
1 parent b1dabf2 commit bf55a2b
Show file tree
Hide file tree
Showing 13 changed files with 193 additions and 7 deletions.
2 changes: 1 addition & 1 deletion lib/static/modules/middlewares/metrika.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ export function getMetrikaMiddleware(analytics: YandexMetrika): Middleware<{}, S
[action.type]: Date.now() - startLoadTime,
initView: state.view,
testsCount,
isNewUi: Boolean(state.app.isNewUi)
isNewUi: Boolean(state?.app?.isNewUi)
});

return result;
Expand Down
1 change: 1 addition & 0 deletions lib/static/new-ui/components/MainLayout/Footer.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ export function Footer(props: FooterProps): ReactNode {
title: 'Info',
onItemClick: props.onFooterItemClick,
current: isInfoCurrent,
qa: 'footer-item-info',
itemWrapper: (params, makeItem) => makeItem({
...params,
icon: <Icon className={classNames({
Expand Down
2 changes: 1 addition & 1 deletion lib/static/new-ui/components/MetrikaScript/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -31,5 +31,5 @@ export function MetrikaScript(): ReactNode {
return null;
}

return <div ref={ref}></div>;
return <div ref={ref} data-qa={'metrika-script'}></div>;
}
4 changes: 1 addition & 3 deletions lib/static/new-ui/utils/analytics.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,8 @@ declare global {

export const getAreAnalyticsEnabled = (): boolean => {
const metrikaConfig = (window.data || {}).config?.yandexMetrika;
const areAnalyticsEnabled = metrikaConfig?.enabled && metrikaConfig?.counterNumber;
const isYaMetrikaAvailable = window.ym && typeof window.ym === 'function';

return Boolean(areAnalyticsEnabled && isYaMetrikaAvailable);
return Boolean(metrikaConfig?.enabled && metrikaConfig?.counterNumber);
};

export const getCounterId = (): number => {
Expand Down
16 changes: 16 additions & 0 deletions test/func/fixtures/analytics/disabled.testplane.conf.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
'use strict';

const _ = require('lodash');

const {getFixturesConfig} = require('../fixtures.testplane.conf');

module.exports = _.merge(getFixturesConfig(__dirname), {
plugins: {
'html-reporter-tester': {
baseHost: 'https://example.com:123',
yandexMetrika: {
enabled: false
}
}
}
});
13 changes: 13 additions & 0 deletions test/func/fixtures/analytics/enabled.testplane.conf.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
'use strict';

const _ = require('lodash');

const {getFixturesConfig} = require('../fixtures.testplane.conf');

module.exports = _.merge(getFixturesConfig(__dirname), {
plugins: {
'html-reporter-tester': {
baseHost: 'https://example.com:123'
}
}
});
11 changes: 11 additions & 0 deletions test/func/fixtures/analytics/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"name": "analytics",
"description": "Test project that generates html-report for testing analytics",
"version": "0.0.0",
"private": true,
"scripts": {
"clean": "rm -rf report",
"generate": "true # This report should be generated with different env vars during tests",
"gui": "npx testplane gui --hostname 0.0.0.0 --port $(../../utils/get-port.js analytics gui)"
}
}
3 changes: 3 additions & 0 deletions test/func/fixtures/analytics/test.testplane.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
it('some test', async () => {
throw new Error('Test should fail');
});
8 changes: 6 additions & 2 deletions test/func/tests/.testplane.conf.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ const serverPort = process.env.SERVER_PORT ?? 8083;
const projectUnderTest = process.env.PROJECT_UNDER_TEST;

const isRunningGuiTests = projectUnderTest.includes('gui');
const isRunningAnalyticsTests = projectUnderTest.includes('analytics');
if (!projectUnderTest) {
throw 'Project under test was not specified';
}
Expand All @@ -40,12 +41,15 @@ const config = _.merge(commonConfig, {
},
plugins: {
files: 'plugins/**/*.testplane.js'
},
analytics: {
files: 'analytics/**/*.testplane.js'
}
},

plugins: {
'html-reporter-test-server': {
enabled: !isRunningGuiTests,
enabled: !isRunningGuiTests && !isRunningAnalyticsTests,
port: serverPort
},
'html-reporter-tester': {
Expand All @@ -56,7 +60,7 @@ const config = _.merge(commonConfig, {
}
});

if (!isRunningGuiTests) {
if (!isRunningGuiTests && !isRunningAnalyticsTests) {
_.set(config.plugins, ['hermione-global-hook', 'beforeEach'], async function({browser}) {
await browser.url(this.browser.options.baseUrl);

Expand Down
117 changes: 117 additions & 0 deletions test/func/tests/analytics/index.testplane.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
const childProcess = require('child_process');
const path = require('path');
const {PORTS} = require('../../utils/constants');

const projectDir = path.resolve(__dirname, '../../fixtures/analytics');

const generateFixtureReport = async (args, env) => {
await new Promise(resolve => {
const proc = childProcess.spawn('npx', ['testplane', ...args], {cwd: projectDir, env: {...process.env, ...env}});

proc.on('exit', () => {
resolve();
});
});
};

const launchStaticServer = () => {
return new Promise((resolve) => {
const proc = childProcess.spawn('node', [path.resolve(__dirname, '../static-server.js')], {
stdio: ['pipe', 'pipe', 'pipe', 'ipc'],
env: {
...process.env,
STATIC_DIR: path.resolve(__dirname, '../..'),
PORT: PORTS.analytics.server
}
});

proc.on('message', () => {
resolve(proc);
});
});
};

describe('Analytics', () => {
let server;

afterEach(() => {
server?.kill();
});

it('should include metrika script by default', async ({browser}) => {
await generateFixtureReport(['-c', 'enabled.testplane.conf.js']);
server = await launchStaticServer();

await browser.url(browser.options.baseUrl.replace('index.html', 'new-ui.html'));
const scriptElement = await browser.$('div[data-qa="metrika-script"] script');

await expect(scriptElement).toBeExisting();
});

it('should track feature usage when opening info panel', async ({browser}) => {
await generateFixtureReport(['-c', 'enabled.testplane.conf.js']);
server = await launchStaticServer();

await browser.url(browser.options.baseUrl.replace('index.html', 'new-ui.html'));
await browser.execute(() => {
window.ym = (...args) => {
if (!window.ym.calls) {
window.ym.calls = [];
}
window.ym.calls.push(args);
};
});

await browser.$('[data-qa="footer-item-info"]').click();

const [metrikaCall] = await browser.execute(() => {
return window.ym.calls;
});

expect(metrikaCall[1]).toEqual('reachGoal');
expect(metrikaCall[2]).toEqual('FEATURE_USAGE');
expect(metrikaCall[3]).toEqual({featureName: 'Open info panel'});
});

it('should not fail when opening info panel with analytics not available', async ({browser}) => {
await generateFixtureReport(['-c', 'disabled.testplane.conf.js']);
server = await launchStaticServer();

await browser.url(browser.options.baseUrl.replace('index.html', 'new-ui.html'));
await browser.$('[data-qa="footer-item-info"]').click();

const infoPanelElement = await browser.$('div*=Data sources');

await expect(infoPanelElement).toBeExisting();
});

it('should not include metrika script if analytics are disabled in config', async ({browser}) => {
await generateFixtureReport(['-c', 'disabled.testplane.conf.js']);
server = await launchStaticServer();

await browser.url(browser.options.baseUrl.replace('index.html', 'new-ui.html'));
const scriptElement = await browser.$('div[data-qa="metrika-script"] script');

await expect(scriptElement).not.toBeExisting();
});

it('should not include metrika script if analytics are disabled via gemini-configparser env var', async ({browser}) => {
await generateFixtureReport(['-c', 'enabled.testplane.conf.js'], {'html_reporter_yandex_metrika_enabled': false});
server = await launchStaticServer();

await browser.url(browser.options.baseUrl.replace('index.html', 'new-ui.html'));
const scriptElement = await browser.$('div[data-qa="metrika-script"] script');

await expect(scriptElement).not.toBeExisting();
});

it('should not include metrika script if analytics are disabled via NO_ANALYTICS env var', async ({browser}) => {
await generateFixtureReport(['-c', 'enabled.testplane.conf.js'], {'NO_ANALYTICS': true});
server = await launchStaticServer();

await browser.url(browser.options.baseUrl.replace('index.html', 'new-ui.html'));
const scriptElement = await browser.$('div[data-qa="metrika-script"] script');

await expect(scriptElement).not.toBeExisting();
});
});
2 changes: 2 additions & 0 deletions test/func/tests/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,14 @@
"gui:playwright": "TOOL=playwright PROJECT_UNDER_TEST=playwright npx testplane --set common gui",
"gui:plugins": "TOOL=testplane PROJECT_UNDER_TEST=plugins SERVER_PORT=8084 npx testplane --set plugins gui",
"gui:testplane-tinder": "TOOL=testplane PROJECT_UNDER_TEST=testplane-gui SERVER_PORT=8084 npx testplane --set common-tinder gui",
"gui:analytics": "TOOL=testplane PROJECT_UNDER_TEST=analytics SERVER_PORT=8085 npx testplane --set analytics gui",
"testplane:testplane-common": "TOOL=testplane PROJECT_UNDER_TEST=testplane SERVER_PORT=8061 npx testplane --set common",
"testplane:testplane-eye": "TOOL=testplane PROJECT_UNDER_TEST=testplane-eye SERVER_PORT=8062 npx testplane --set eye",
"testplane:testplane-gui": "TOOL=testplane PROJECT_UNDER_TEST=testplane-gui SERVER_PORT=8063 npx testplane --no --set common-gui",
"testplane:playwright": "TOOL=playwright PROJECT_UNDER_TEST=playwright SERVER_PORT=8065 npx testplane --set common",
"testplane:plugins": "TOOL=testplane PROJECT_UNDER_TEST=plugins SERVER_PORT=8064 npx testplane --set plugins",
"testplane:testplane-tinder": "TOOL=testplane PROJECT_UNDER_TEST=testplane-gui SERVER_PORT=8084 npx testplane --set common-tinder",
"testplane:analytics": "TOOL=testplane PROJECT_UNDER_TEST=analytics SERVER_PORT=8085 npx testplane --set analytics",
"test": "run-s testplane:*"
},
"devDependencies": {
Expand Down
17 changes: 17 additions & 0 deletions test/func/tests/static-server.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
const express = require('express');

const dir = process.env.STATIC_DIR;
const port = process.env.PORT;
const host = process.env.HOST ?? 'localhost';

const app = express();
app.use(express.static(dir));
app.listen(port, (err) => {
if (err) {
console.error('Failed to start test server:');
throw new Error(err);
}

process.send('Ready');
console.info(`Server is listening on ${host}:${port}`);
});
4 changes: 4 additions & 0 deletions test/func/utils/constants.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,10 @@ module.exports = {
plugins: {
server: 8084,
gui: 8074
},
analytics: {
server: 8085,
gui: 8075
}
}
};

0 comments on commit bf55a2b

Please sign in to comment.