1414 * limitations under the License.
1515 */
1616
17+ import fs from 'fs' ;
1718import path from 'path' ;
1819import StackUtils from 'stack-utils' ;
1920import { DebugHighlight } from './debugHighlight' ;
@@ -22,13 +23,13 @@ import * as reporterTypes from './upstream/reporter';
2223import { ReusedBrowser } from './reusedBrowser' ;
2324import { SettingsModel } from './settingsModel' ;
2425import { SettingsView } from './settingsView' ;
25- import { TestModel , TestModelCollection } from './testModel' ;
26+ import { TestModel , TestModelCollection , TestProject } from './testModel' ;
2627import { configError , disabledProjectName as disabledProject , TestTree } from './testTree' ;
2728import { NodeJSNotFoundError , getPlaywrightInfo , stripAnsi , stripBabelFrame , uriToPath } from './utils' ;
2829import * as vscodeTypes from './vscodeTypes' ;
2930import { WorkspaceChange , WorkspaceObserver } from './workspaceObserver' ;
3031import { registerTerminalLinkProvider } from './terminalLinkProvider' ;
31- import { ErrorContext , RunHooks , TestConfig } from './playwrightTestTypes' ;
32+ import { RunHooks , TestConfig , ErrorContext } from './playwrightTestTypes' ;
3233import { ansi2html } from './ansi2html' ;
3334import { LocatorsView } from './locatorsView' ;
3435
@@ -185,18 +186,34 @@ export class Extension implements RunHooks {
185186 this . _reusedBrowser . closeAllBrowsers ( ) ;
186187 } ) ,
187188 vscode . commands . registerCommand ( 'pw.extension.command.recordNew' , async ( ) => {
188- if ( ! this . _models . hasEnabledModels ( ) ) {
189- await vscode . window . showWarningMessage ( messageNoPlaywrightTestsFound ) ;
189+ const model = this . _models . selectedModel ( ) ;
190+ if ( ! model )
191+ return vscode . window . showWarningMessage ( messageNoPlaywrightTestsFound ) ;
192+
193+ const project = model . enabledProjects ( ) [ 0 ] ;
194+ if ( ! project )
195+ return vscode . window . showWarningMessage ( this . _vscode . l10n . t ( `Project is disabled in the Playwright sidebar.` ) ) ;
196+
197+ const file = await this . _createFileForNewTest ( model , project ) ;
198+ if ( ! file )
190199 return ;
200+
201+
202+ const showBrowser = this . _settingsModel . showBrowser . get ( ) ?? false ;
203+ try {
204+ await this . _settingsModel . showBrowser . set ( true ) ;
205+ await this . _showBrowserForRecording ( file , project ) ;
206+ await this . _reusedBrowser . record ( model ) ;
207+ } finally {
208+ await this . _settingsModel . showBrowser . set ( showBrowser ) ;
191209 }
192- await this . _reusedBrowser . record ( this . _models , true ) ;
193210 } ) ,
194211 vscode . commands . registerCommand ( 'pw.extension.command.recordAtCursor' , async ( ) => {
195- if ( ! this . _models . hasEnabledModels ( ) ) {
196- await vscode . window . showWarningMessage ( messageNoPlaywrightTestsFound ) ;
197- return ;
198- }
199- await this . _reusedBrowser . record ( this . _models , false ) ;
212+ const model = this . _models . selectedModel ( ) ;
213+ if ( ! model )
214+ return vscode . window . showWarningMessage ( messageNoPlaywrightTestsFound ) ;
215+
216+ await this . _reusedBrowser . record ( model ) ;
200217 } ) ,
201218 vscode . commands . registerCommand ( 'pw.extension.command.toggleModels' , async ( ) => {
202219 this . _settingsView . toggleModels ( ) ;
@@ -662,6 +679,49 @@ export class Extension implements RunHooks {
662679 await this . _queueWatchRun ( new this . _vscode . TestRunRequest ( testItems ) , 'items' ) ;
663680 }
664681
682+ private async _createFileForNewTest ( model : TestModel , project : TestProject ) {
683+ let file ;
684+ for ( let i = 1 ; i < 100 ; ++ i ) {
685+ file = path . join ( project . project . testDir , `test-${ i } .spec.ts` ) ;
686+ if ( fs . existsSync ( file ) )
687+ continue ;
688+ break ;
689+ }
690+ if ( ! file )
691+ return ;
692+
693+ await fs . promises . writeFile ( file , `import { test, expect } from '@playwright/test';
694+
695+ test('test', async ({ page }) => {
696+ // Recording...
697+ });` ) ;
698+
699+ await model . handleWorkspaceChange ( { created : new Set ( [ file ] ) , changed : new Set ( ) , deleted : new Set ( ) } ) ;
700+ await model . ensureTests ( [ file ] ) ;
701+
702+ const document = await this . _vscode . workspace . openTextDocument ( file ) ;
703+ const editor = await this . _vscode . window . showTextDocument ( document ) ;
704+ editor . selection = new this . _vscode . Selection ( new this . _vscode . Position ( 3 , 2 ) , new this . _vscode . Position ( 3 , 2 + '// Recording...' . length ) ) ;
705+
706+ return file ;
707+ }
708+
709+ private async _showBrowserForRecording ( file : string , project : TestProject ) {
710+ const fileItem = this . _testTree . testItemForFile ( file ) ;
711+ if ( ! fileItem )
712+ return ;
713+ if ( fileItem . children . size !== 1 )
714+ return ;
715+
716+ const testItems = this . _testTree . collectTestsInside ( fileItem ) ;
717+ const testForProject = testItems . length === 1 ? testItems [ 0 ] : testItems . find ( t => t . label === project . name ) ;
718+ if ( ! testForProject )
719+ return ;
720+
721+ const request = new this . _vscode . TestRunRequest ( [ testForProject ] , undefined , undefined , false , true ) ;
722+ await this . _queueTestRun ( request , 'run' ) ;
723+ }
724+
665725 private async _updateVisibleEditorItems ( ) {
666726 const files = this . _vscode . window . visibleTextEditors . map ( e => uriToPath ( e . document . uri ) ) ;
667727 await this . _ensureTestsInAllModels ( files ) ;
0 commit comments