1+ /*
2+ * Copyright 2024 Google LLC
3+ *
4+ * Licensed under the Apache License, Version 2.0 (the "License");
5+ * you may not use this file except in compliance with the License.
6+ * You may obtain a copy of the License at
7+ *
8+ * https://www.apache.org/licenses/LICENSE-2.0
9+ *
10+ * Unless required by applicable law or agreed to in writing, software
11+ * distributed under the License is distributed on an "AS IS" BASIS,
12+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+ * See the License for the specific language governing permissions and
14+ * limitations under the License.
15+ */
16+
17+ import { test , expect } from '@playwright/test' ;
18+ import fs from 'fs' ;
19+ import path from 'path' ;
20+ import childProcess from 'child_process' ; // Import childProcess
21+
22+ const samplesDir = path . join ( __dirname , '..' , 'samples' ) ;
23+
24+ const sampleFolders = fs . readdirSync ( samplesDir ) . filter ( ( file ) => {
25+ return fs . statSync ( path . join ( samplesDir , file ) ) . isDirectory ( ) ;
26+ } ) ;
27+
28+ sampleFolders . forEach ( ( sampleFolder ) => {
29+ test ( `test ${ sampleFolder } ` , async ( { page } ) => {
30+ const url = `http://localhost:8080/samples/${ sampleFolder } /` ;
31+
32+ // START Build the sample
33+ const buildProcess = childProcess . spawn ( 'npm' , [ 'run' , 'build' ] , {
34+ cwd : path . join ( samplesDir , sampleFolder ) ,
35+ stdio : 'inherit' ,
36+ } ) ;
37+
38+ await new Promise ( ( resolve , reject ) => {
39+ buildProcess . on ( 'close' , ( code ) => {
40+ if ( code === 0 ) {
41+ resolve ( true ) ;
42+ } else {
43+ reject ( `Build process exited with code ${ code } ` ) ;
44+ }
45+ } ) ;
46+ } ) ;
47+ // END Build the sample
48+
49+ // START run the preview
50+ const viteProcess = childProcess . spawn ( 'npm' , [ 'run' , 'preview' , '--' , '--port=8080' ] , {
51+ cwd : path . join ( samplesDir , sampleFolder ) ,
52+ stdio : 'inherit' ,
53+ } ) ;
54+
55+ await new Promise ( ( resolve ) => setTimeout ( resolve , 1000 ) ) ;
56+ // END run the preview
57+
58+ try {
59+ await page . goto ( url ) ;
60+ await page . waitForLoadState ( 'domcontentloaded' ) ;
61+ // Add your assertions here
62+ } finally {
63+ viteProcess . kill ( ) ;
64+ }
65+ } ) ;
66+ } ) ;
0 commit comments