Skip to content

Commit de56b67

Browse files
authored
fix: support -a option in new UI (#749)
1 parent 7425970 commit de56b67

File tree

5 files changed

+92
-2
lines changed

5 files changed

+92
-2
lines changed

lib/static/new-ui/app/App.tsx

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import {HashRouter, Navigate, Route, Routes} from 'react-router-dom';
88

99
import {LoadingBar} from '@/static/new-ui/components/LoadingBar';
1010
import {GuiniToolbarOverlay} from '@/static/new-ui/components/GuiniToolbarOverlay';
11+
import {AutoRun} from '@/static/new-ui/components/AutoRun';
1112
import {MainLayout} from '../components/MainLayout';
1213
import {SuitesPage} from '../features/suites/components/SuitesPage';
1314
import {VisualChecksPage} from '../features/visual-checks/components/VisualChecksPage';
@@ -51,6 +52,7 @@ export function App(): ReactNode {
5152
<Provider store={store}>
5253
<MetrikaScript/>
5354
<FaviconChanger />
55+
<AutoRun />
5456
<AnalyticsProvider>
5557
<HashRouter>
5658
<ErrorHandler.Boundary fallback={<ErrorHandler.FallbackAppCrash />}>
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
import {useEffect, useRef} from 'react';
2+
import {useDispatch, useSelector} from 'react-redux';
3+
import {State} from '@/static/new-ui/types/store';
4+
import {thunkRunAllTests} from '@/static/modules/actions';
5+
6+
export function AutoRun(): null {
7+
const dispatch = useDispatch();
8+
const autoRun = useSelector((state: State) => state.autoRun);
9+
const isInitialized = useSelector((state: State) => state.app.isInitialized);
10+
const isAlreadyRun = useRef(false);
11+
12+
useEffect(() => {
13+
if (autoRun && isInitialized && !isAlreadyRun.current) {
14+
isAlreadyRun.current = true;
15+
dispatch(thunkRunAllTests());
16+
}
17+
}, [autoRun, isInitialized, dispatch]);
18+
19+
return null;
20+
}

lib/static/new-ui/types/store.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -369,6 +369,7 @@ export interface State {
369369
stats: {all: Stats | Record<string, never>, perBrowser: PerBrowserStats | undefined} | null;
370370
db?: Database;
371371
plugins: Record<string, unknown>;
372+
autoRun: boolean;
372373
}
373374

374375
declare module 'react-redux' {
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
const childProcess = require('child_process');
2+
const {existsSync} = require('fs');
3+
const fs = require('fs/promises');
4+
const path = require('path');
5+
const {promisify} = require('util');
6+
7+
const treeKill = promisify(require('tree-kill'));
8+
9+
const {PORTS} = require('../../utils/constants');
10+
const {runGui} = require('../utils');
11+
12+
const serverHost = process.env.SERVER_HOST ?? 'host.docker.internal';
13+
14+
const projectName = process.env.PROJECT_UNDER_TEST;
15+
const projectDir = path.resolve(__dirname, '../../fixtures', projectName);
16+
const guiUrl = `http://${serverHost}:${PORTS[projectName].gui}`;
17+
18+
const reportDir = path.join(projectDir, 'report');
19+
const reportBackupDir = path.join(projectDir, 'report-backup');
20+
const screensDir = path.join(projectDir, 'screens');
21+
22+
describe('GUI mode', () => {
23+
describe('Auto-run with -a option', () => {
24+
let guiProcess;
25+
26+
afterEach(async () => {
27+
if (guiProcess) {
28+
await treeKill(guiProcess.pid);
29+
}
30+
31+
childProcess.execSync('git restore .', {cwd: screensDir});
32+
childProcess.execSync('git clean -dfx .', {cwd: screensDir});
33+
});
34+
35+
describe('New UI', () => {
36+
beforeEach(async ({browser}) => {
37+
if (existsSync(reportBackupDir)) {
38+
await fs.rm(reportDir, {recursive: true, force: true, maxRetries: 3});
39+
await fs.cp(reportBackupDir, reportDir, {recursive: true, force: true});
40+
} else {
41+
await fs.cp(reportDir, reportBackupDir, {recursive: true});
42+
}
43+
44+
guiProcess = await runGui(projectDir, {autoRun: true, grep: 'successful test'});
45+
46+
await browser.url(guiUrl + '/new-ui');
47+
});
48+
49+
it('should automatically run tests when started with -a option', async ({browser}) => {
50+
const testItem = await browser.$('[data-list-item*="chrome"]');
51+
await testItem.click();
52+
53+
await browser.$('[data-qa="suite-status-bar-status"]*=Success').waitForDisplayed({timeout: 5000});
54+
});
55+
});
56+
});
57+
});

test/func/tests/utils.js

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,9 +32,19 @@ const hideScreenshots = async (browser) => {
3232
});
3333
};
3434

35-
const runGui = async (projectDir) => {
35+
const runGui = async (projectDir, {autoRun = false, grep = null} = {}) => {
3636
return new Promise((resolve, reject) => {
37-
const child = childProcess.spawn('npm', ['run', 'gui', '--', '--no-open'], {cwd: projectDir});
37+
const args = ['run', 'gui', '--', '--no-open'];
38+
39+
if (autoRun) {
40+
args.push('--auto-run');
41+
}
42+
43+
if (grep) {
44+
args.push('--grep', grep);
45+
}
46+
47+
const child = childProcess.spawn('npm', args, {cwd: projectDir});
3848

3949
let processKillTimeoutId = setTimeout(() => {
4050
treeKill(child.pid).then(() => {

0 commit comments

Comments
 (0)