2021-07-16 08:02:10 +03:00
/ * *
* Copyright ( c ) Microsoft Corporation .
*
* Licensed under the Apache License , Version 2.0 ( the "License" ) ;
* you may not use this file except in compliance with the License .
* You may obtain a copy of the License at
*
* http : //www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing , software
* distributed under the License is distributed on an "AS IS" BASIS ,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND , either express or implied .
* See the License for the specific language governing permissions and
* limitations under the License .
* /
import { test , expect } from './playwright-test-fixtures' ;
2021-07-20 23:03:01 +03:00
const smallReporterJS = `
class Reporter {
onBegin ( config , suite ) {
console . log ( '\\n%%begin' ) ;
}
onTestBegin ( test ) { }
onStdOut() { }
onStdErr() { }
onTestEnd ( test , result ) { }
onTimeout() { }
onError() { }
onEnd() {
console . log ( '\\n%%end' ) ;
}
}
module .exports = Reporter ;
` ;
2021-07-16 08:02:10 +03:00
test ( 'should work with custom reporter' , async ( { runInlineTest } ) = > {
const result = await runInlineTest ( {
'reporter.ts' : `
class Reporter {
constructor ( options ) {
this . options = options ;
}
onBegin ( config , suite ) {
2021-07-17 01:23:50 +03:00
console . log ( '\\n%%reporter-begin-' + this . options . begin + '%%' ) ;
2021-07-16 08:02:10 +03:00
}
onTestBegin ( test ) {
2021-07-17 01:23:50 +03:00
console . log ( '\\n%%reporter-testbegin-' + test . title + '-' + test . titlePath ( ) [ 1 ] + '%%' ) ;
2021-07-16 08:02:10 +03:00
}
onStdOut() {
console . log ( '\\n%%reporter-stdout%%' ) ;
}
onStdErr() {
console . log ( '\\n%%reporter-stderr%%' ) ;
}
2021-07-19 03:40:59 +03:00
onTestEnd ( test , result ) {
2021-07-17 01:23:50 +03:00
console . log ( '\\n%%reporter-testend-' + test . title + '-' + test . titlePath ( ) [ 1 ] + '%%' ) ;
2021-07-19 03:40:59 +03:00
if ( ! result . startTime )
console . log ( '\\n%%error-no-start-time' ) ;
2021-07-16 08:02:10 +03:00
}
onTimeout() {
console . log ( '\\n%%reporter-timeout%%' ) ;
}
onError() {
console . log ( '\\n%%reporter-error%%' ) ;
}
async onEnd() {
await new Promise ( f = > setTimeout ( f , 500 ) ) ;
console . log ( '\\n%%reporter-end-' + this . options . end + '%%' ) ;
}
}
export default Reporter ;
` ,
'playwright.config.ts' : `
module .exports = {
reporter : [
[ './reporter.ts' , { begin : 'begin' , end : 'end' } ]
] ,
projects : [
{ name : 'foo' , repeatEach : 2 } ,
{ name : 'bar' } ,
] ,
} ;
` ,
'a.test.ts' : `
const { test } = pwt ;
2021-07-27 20:37:18 +03:00
test ( 'not run' , async ( { } ) = > {
console . log ( 'log' ) ;
console . error ( 'error' ) ;
} ) ;
test . only ( 'is run' , async ( { } ) = > {
2021-07-16 08:02:10 +03:00
console . log ( 'log' ) ;
console . error ( 'error' ) ;
} ) ;
`
} , { reporter : '' , workers : 1 } ) ;
expect ( result . exitCode ) . toBe ( 0 ) ;
expect ( result . output . split ( '\n' ) . filter ( line = > line . startsWith ( '%%' ) ) ) . toEqual ( [
2021-07-17 01:23:50 +03:00
'%%reporter-begin-begin%%' ,
2021-07-27 20:37:18 +03:00
'%%reporter-testbegin-is run-foo%%' ,
2021-07-16 08:02:10 +03:00
'%%reporter-stdout%%' ,
'%%reporter-stderr%%' ,
2021-07-27 20:37:18 +03:00
'%%reporter-testend-is run-foo%%' ,
'%%reporter-testbegin-is run-foo%%' ,
2021-07-16 08:02:10 +03:00
'%%reporter-stdout%%' ,
'%%reporter-stderr%%' ,
2021-07-27 20:37:18 +03:00
'%%reporter-testend-is run-foo%%' ,
'%%reporter-testbegin-is run-bar%%' ,
2021-07-16 08:02:10 +03:00
'%%reporter-stdout%%' ,
'%%reporter-stderr%%' ,
2021-07-27 20:37:18 +03:00
'%%reporter-testend-is run-bar%%' ,
2021-07-16 08:02:10 +03:00
'%%reporter-end-end%%' ,
] ) ;
} ) ;
2021-07-20 23:03:01 +03:00
test ( 'should work without a file extension' , async ( { runInlineTest } ) = > {
const result = await runInlineTest ( {
'reporter.ts' : smallReporterJS ,
'playwright.config.ts' : `
module .exports = {
reporter : './reporter' ,
} ;
` ,
'a.test.ts' : `
const { test } = pwt ;
test ( 'pass' , async ( { } ) = > {
} ) ;
`
} , { reporter : '' , workers : 1 } ) ;
expect ( result . exitCode ) . toBe ( 0 ) ;
expect ( result . output . split ( '\n' ) . filter ( line = > line . startsWith ( '%%' ) ) ) . toEqual ( [
'%%begin' ,
'%%end' ,
] ) ;
} ) ;
test ( 'should load reporter from node_modules' , async ( { runInlineTest } ) = > {
const result = await runInlineTest ( {
'node_modules/my-reporter/index.js' : smallReporterJS ,
'playwright.config.ts' : `
module .exports = {
reporter : 'my-reporter' ,
} ;
` ,
'a.test.ts' : `
const { test } = pwt ;
test ( 'pass' , async ( { } ) = > {
} ) ;
`
} , { reporter : '' , workers : 1 } ) ;
expect ( result . exitCode ) . toBe ( 0 ) ;
expect ( result . output . split ( '\n' ) . filter ( line = > line . startsWith ( '%%' ) ) ) . toEqual ( [
'%%begin' ,
'%%end' ,
] ) ;
} ) ;
2021-07-31 02:07:02 +03:00
2021-08-03 03:17:20 +03:00
test ( 'should report expect steps' , async ( { runInlineTest } ) = > {
2021-07-31 02:07:02 +03:00
const expectReporterJS = `
class Reporter {
2021-08-10 04:09:11 +03:00
onStdOut ( chunk ) {
process . stdout . write ( chunk ) ;
}
2021-08-03 03:17:20 +03:00
onStepBegin ( test , result , step ) {
const copy = { . . . step , startTime : undefined , duration : undefined } ;
console . log ( '%%%% begin' , JSON . stringify ( copy ) ) ;
}
onStepEnd ( test , result , step ) {
const copy = { . . . step , startTime : undefined , duration : undefined } ;
2021-08-03 08:11:37 +03:00
if ( copy . error ? . stack )
copy . error . stack = '<stack>' ;
2021-08-03 03:17:20 +03:00
console . log ( '%%%% end' , JSON . stringify ( copy ) ) ;
2021-07-31 02:07:02 +03:00
}
}
module .exports = Reporter ;
` ;
const result = await runInlineTest ( {
'reporter.ts' : expectReporterJS ,
'playwright.config.ts' : `
module .exports = {
reporter : './reporter' ,
} ;
` ,
'a.test.ts' : `
const { test } = pwt ;
test ( 'fail' , async ( { } ) = > {
expect ( true ) . toBeTruthy ( ) ;
expect ( false ) . toBeTruthy ( ) ;
} ) ;
test ( 'pass' , async ( { } ) = > {
expect ( false ) . not . toBeTruthy ( ) ;
} ) ;
test ( 'async' , async ( { page } ) = > {
await expect ( page ) . not . toHaveTitle ( 'False' ) ;
} ) ;
`
} , { reporter : '' , workers : 1 } ) ;
expect ( result . exitCode ) . toBe ( 1 ) ;
2021-08-03 08:11:37 +03:00
expect ( result . output . split ( '\n' ) . filter ( line = > line . startsWith ( '%%' ) ) . map ( stripEscapedAscii ) ) . toEqual ( [
2021-08-03 03:17:20 +03:00
` %% begin { \ "title \ ": \ "Before Hooks \ ", \ "category \ ": \ "hook \ "} ` ,
` %% end { \ "title \ ": \ "Before Hooks \ ", \ "category \ ": \ "hook \ "} ` ,
` %% begin { \ "title \ ": \ "expect.toBeTruthy \ ", \ "category \ ": \ "expect \ "} ` ,
` %% end { \ "title \ ": \ "expect.toBeTruthy \ ", \ "category \ ": \ "expect \ "} ` ,
` %% begin { \ "title \ ": \ "expect.toBeTruthy \ ", \ "category \ ": \ "expect \ "} ` ,
2021-08-03 08:11:37 +03:00
` %% end { \ "title \ ": \ "expect.toBeTruthy \ ", \ "category \ ": \ "expect \ ", \ "error \ ":{ \ "message \ ": \ "expect(received).toBeTruthy() \\ n \\ nReceived: false \ ", \ "stack \ ": \ "<stack> \ "}} ` ,
2021-08-03 03:17:20 +03:00
` %% begin { \ "title \ ": \ "After Hooks \ ", \ "category \ ": \ "hook \ "} ` ,
` %% end { \ "title \ ": \ "After Hooks \ ", \ "category \ ": \ "hook \ "} ` ,
` %% begin { \ "title \ ": \ "Before Hooks \ ", \ "category \ ": \ "hook \ "} ` ,
` %% end { \ "title \ ": \ "Before Hooks \ ", \ "category \ ": \ "hook \ "} ` ,
` %% begin { \ "title \ ": \ "expect.not.toBeTruthy \ ", \ "category \ ": \ "expect \ "} ` ,
` %% end { \ "title \ ": \ "expect.not.toBeTruthy \ ", \ "category \ ": \ "expect \ "} ` ,
` %% begin { \ "title \ ": \ "After Hooks \ ", \ "category \ ": \ "hook \ "} ` ,
` %% end { \ "title \ ": \ "After Hooks \ ", \ "category \ ": \ "hook \ "} ` ,
` %% begin { \ "title \ ": \ "Before Hooks \ ", \ "category \ ": \ "hook \ "} ` ,
` %% begin { \ "title \ ": \ "browserContext.newPage \ ", \ "category \ ": \ "pw:api \ "} ` ,
` %% end { \ "title \ ": \ "browserContext.newPage \ ", \ "category \ ": \ "pw:api \ "} ` ,
2021-08-12 17:58:00 +03:00
` %% end { \ "title \ ": \ "Before Hooks \ ", \ "category \ ": \ "hook \ "} ` ,
2021-08-03 03:17:20 +03:00
` %% begin { \ "title \ ": \ "expect.not.toHaveTitle \ ", \ "category \ ": \ "expect \ "} ` ,
` %% begin { \ "title \ ": \ "page.title \ ", \ "category \ ": \ "pw:api \ "} ` ,
` %% end { \ "title \ ": \ "page.title \ ", \ "category \ ": \ "pw:api \ "} ` ,
` %% end { \ "title \ ": \ "expect.not.toHaveTitle \ ", \ "category \ ": \ "expect \ "} ` ,
` %% begin { \ "title \ ": \ "After Hooks \ ", \ "category \ ": \ "hook \ "} ` ,
` %% begin { \ "title \ ": \ "browserContext.close \ ", \ "category \ ": \ "pw:api \ "} ` ,
` %% end { \ "title \ ": \ "browserContext.close \ ", \ "category \ ": \ "pw:api \ "} ` ,
` %% end { \ "title \ ": \ "After Hooks \ ", \ "category \ ": \ "hook \ "} ` ,
2021-07-31 02:07:02 +03:00
] ) ;
} ) ;
2021-08-03 03:17:20 +03:00
test ( 'should report api steps' , async ( { runInlineTest } ) = > {
2021-07-31 02:07:02 +03:00
const expectReporterJS = `
class Reporter {
2021-08-10 04:09:11 +03:00
onStdOut ( chunk ) {
process . stdout . write ( chunk ) ;
}
onTestBegin ( test ) {
console . log ( '%%%% test begin ' + test . title ) ;
}
onTestEnd ( test ) {
console . log ( '%%%% test end ' + test . title ) ;
}
2021-08-03 03:17:20 +03:00
onStepBegin ( test , result , step ) {
const copy = { . . . step , startTime : undefined , duration : undefined } ;
console . log ( '%%%% begin' , JSON . stringify ( copy ) ) ;
}
onStepEnd ( test , result , step ) {
const copy = { . . . step , startTime : undefined , duration : undefined } ;
2021-08-03 08:11:37 +03:00
if ( copy . error ? . stack )
copy . error . stack = '<stack>' ;
2021-08-03 03:17:20 +03:00
console . log ( '%%%% end' , JSON . stringify ( copy ) ) ;
2021-07-31 02:07:02 +03:00
}
}
module .exports = Reporter ;
` ;
const result = await runInlineTest ( {
'reporter.ts' : expectReporterJS ,
'playwright.config.ts' : `
module .exports = {
reporter : './reporter' ,
} ;
` ,
'a.test.ts' : `
const { test } = pwt ;
test ( 'pass' , async ( { page } ) = > {
await page . setContent ( '<button></button>' ) ;
await page . click ( 'button' ) ;
} ) ;
2021-08-10 04:09:11 +03:00
test . describe ( 'suite' , ( ) = > {
let myPage ;
test . beforeAll ( async ( { browser } ) = > {
myPage = await browser . newPage ( ) ;
await myPage . setContent ( '<button></button>' ) ;
} ) ;
test ( 'pass1' , async ( ) = > {
await myPage . click ( 'button' ) ;
} ) ;
test ( 'pass2' , async ( ) = > {
await myPage . click ( 'button' ) ;
} ) ;
test . afterAll ( async ( ) = > {
await myPage . close ( ) ;
} ) ;
} ) ;
2021-07-31 02:07:02 +03:00
`
} , { reporter : '' , workers : 1 } ) ;
expect ( result . exitCode ) . toBe ( 0 ) ;
2021-08-03 08:11:37 +03:00
expect ( result . output . split ( '\n' ) . filter ( line = > line . startsWith ( '%%' ) ) . map ( stripEscapedAscii ) ) . toEqual ( [
2021-08-10 04:09:11 +03:00
` %%%% test begin pass ` ,
2021-08-03 03:17:20 +03:00
` %% begin { \ "title \ ": \ "Before Hooks \ ", \ "category \ ": \ "hook \ "} ` ,
` %% begin { \ "title \ ": \ "browserContext.newPage \ ", \ "category \ ": \ "pw:api \ "} ` ,
` %% end { \ "title \ ": \ "browserContext.newPage \ ", \ "category \ ": \ "pw:api \ "} ` ,
2021-08-12 17:58:00 +03:00
` %% end { \ "title \ ": \ "Before Hooks \ ", \ "category \ ": \ "hook \ "} ` ,
2021-08-03 03:17:20 +03:00
` %% begin { \ "title \ ": \ "page.setContent \ ", \ "category \ ": \ "pw:api \ "} ` ,
` %% end { \ "title \ ": \ "page.setContent \ ", \ "category \ ": \ "pw:api \ "} ` ,
` %% begin { \ "title \ ": \ "page.click \ ", \ "category \ ": \ "pw:api \ "} ` ,
` %% end { \ "title \ ": \ "page.click \ ", \ "category \ ": \ "pw:api \ "} ` ,
` %% begin { \ "title \ ": \ "After Hooks \ ", \ "category \ ": \ "hook \ "} ` ,
` %% begin { \ "title \ ": \ "browserContext.close \ ", \ "category \ ": \ "pw:api \ "} ` ,
` %% end { \ "title \ ": \ "browserContext.close \ ", \ "category \ ": \ "pw:api \ "} ` ,
` %% end { \ "title \ ": \ "After Hooks \ ", \ "category \ ": \ "hook \ "} ` ,
2021-08-10 04:09:11 +03:00
` %%%% test end pass ` ,
` %%%% test begin pass1 ` ,
` %% begin { \ "title \ ": \ "Before Hooks \ ", \ "category \ ": \ "hook \ "} ` ,
` %% end { \ "title \ ": \ "Before Hooks \ ", \ "category \ ": \ "hook \ "} ` ,
` %% begin { \ "title \ ": \ "page.click \ ", \ "category \ ": \ "pw:api \ "} ` ,
` %% end { \ "title \ ": \ "page.click \ ", \ "category \ ": \ "pw:api \ "} ` ,
` %% begin { \ "title \ ": \ "After Hooks \ ", \ "category \ ": \ "hook \ "} ` ,
` %% end { \ "title \ ": \ "After Hooks \ ", \ "category \ ": \ "hook \ "} ` ,
` %%%% test end pass1 ` ,
` %%%% test begin pass2 ` ,
` %% begin { \ "title \ ": \ "Before Hooks \ ", \ "category \ ": \ "hook \ "} ` ,
` %% end { \ "title \ ": \ "Before Hooks \ ", \ "category \ ": \ "hook \ "} ` ,
` %% begin { \ "title \ ": \ "page.click \ ", \ "category \ ": \ "pw:api \ "} ` ,
` %% end { \ "title \ ": \ "page.click \ ", \ "category \ ": \ "pw:api \ "} ` ,
` %% begin { \ "title \ ": \ "After Hooks \ ", \ "category \ ": \ "hook \ "} ` ,
` %% end { \ "title \ ": \ "After Hooks \ ", \ "category \ ": \ "hook \ "} ` ,
` %%%% test end pass2 ` ,
2021-07-31 02:07:02 +03:00
] ) ;
} ) ;
2021-08-03 08:11:37 +03:00
test ( 'should report api step failure' , async ( { runInlineTest } ) = > {
const expectReporterJS = `
class Reporter {
2021-08-10 04:09:11 +03:00
onStdOut ( chunk ) {
process . stdout . write ( chunk ) ;
}
2021-08-03 08:11:37 +03:00
onStepBegin ( test , result , step ) {
const copy = { . . . step , startTime : undefined , duration : undefined } ;
console . log ( '%%%% begin' , JSON . stringify ( copy ) ) ;
}
onStepEnd ( test , result , step ) {
const copy = { . . . step , startTime : undefined , duration : undefined } ;
if ( copy . error ? . stack )
copy . error . stack = '<stack>' ;
console . log ( '%%%% end' , JSON . stringify ( copy ) ) ;
}
}
module .exports = Reporter ;
` ;
const result = await runInlineTest ( {
'reporter.ts' : expectReporterJS ,
'playwright.config.ts' : `
module .exports = {
reporter : './reporter' ,
} ;
` ,
'a.test.ts' : `
const { test } = pwt ;
test ( 'fail' , async ( { page } ) = > {
await page . setContent ( '<button></button>' ) ;
await page . click ( 'input' , { timeout : 1 } ) ;
} ) ;
`
} , { reporter : '' , workers : 1 } ) ;
expect ( result . exitCode ) . toBe ( 1 ) ;
expect ( result . output . split ( '\n' ) . filter ( line = > line . startsWith ( '%%' ) ) . map ( stripEscapedAscii ) ) . toEqual ( [
` %% begin { \ "title \ ": \ "Before Hooks \ ", \ "category \ ": \ "hook \ "} ` ,
` %% begin { \ "title \ ": \ "browserContext.newPage \ ", \ "category \ ": \ "pw:api \ "} ` ,
` %% end { \ "title \ ": \ "browserContext.newPage \ ", \ "category \ ": \ "pw:api \ "} ` ,
2021-08-12 17:58:00 +03:00
` %% end { \ "title \ ": \ "Before Hooks \ ", \ "category \ ": \ "hook \ "} ` ,
2021-08-03 08:11:37 +03:00
` %% begin { \ "title \ ": \ "page.setContent \ ", \ "category \ ": \ "pw:api \ "} ` ,
` %% end { \ "title \ ": \ "page.setContent \ ", \ "category \ ": \ "pw:api \ "} ` ,
` %% begin { \ "title \ ": \ "page.click \ ", \ "category \ ": \ "pw:api \ "} ` ,
` %% end { \ "title \ ": \ "page.click \ ", \ "category \ ": \ "pw:api \ ", \ "error \ ":{ \ "message \ ": \ "page.click: Timeout 1ms exceeded. \\ n=========================== logs =========================== \\ nwaiting for selector \\ \ "input \\ \ " \\ n============================================================ \ ", \ "stack \ ": \ "<stack> \ "}} ` ,
` %% begin { \ "title \ ": \ "After Hooks \ ", \ "category \ ": \ "hook \ "} ` ,
` %% begin { \ "title \ ": \ "browserContext.close \ ", \ "category \ ": \ "pw:api \ "} ` ,
` %% end { \ "title \ ": \ "browserContext.close \ ", \ "category \ ": \ "pw:api \ "} ` ,
` %% end { \ "title \ ": \ "After Hooks \ ", \ "category \ ": \ "hook \ "} ` ,
] ) ;
} ) ;
test ( 'should report test.step' , async ( { runInlineTest } ) = > {
const expectReporterJS = `
class Reporter {
2021-08-10 04:09:11 +03:00
onStdOut ( chunk ) {
process . stdout . write ( chunk ) ;
}
2021-08-03 08:11:37 +03:00
onStepBegin ( test , result , step ) {
const copy = { . . . step , startTime : undefined , duration : undefined } ;
console . log ( '%%%% begin' , JSON . stringify ( copy ) ) ;
}
onStepEnd ( test , result , step ) {
const copy = { . . . step , startTime : undefined , duration : undefined } ;
if ( copy . error ? . stack )
copy . error . stack = '<stack>' ;
console . log ( '%%%% end' , JSON . stringify ( copy ) ) ;
}
}
module .exports = Reporter ;
` ;
const result = await runInlineTest ( {
'reporter.ts' : expectReporterJS ,
'playwright.config.ts' : `
module .exports = {
reporter : './reporter' ,
} ;
` ,
'a.test.ts' : `
const { test } = pwt ;
test ( 'pass' , async ( { page } ) = > {
await test . step ( 'First step' , async ( ) = > {
expect ( 1 ) . toBe ( 2 ) ;
} ) ;
} ) ;
`
} , { reporter : '' , workers : 1 } ) ;
expect ( result . exitCode ) . toBe ( 1 ) ;
expect ( result . output . split ( '\n' ) . filter ( line = > line . startsWith ( '%%' ) ) . map ( stripEscapedAscii ) ) . toEqual ( [
` %% begin { \ "title \ ": \ "Before Hooks \ ", \ "category \ ": \ "hook \ "} ` ,
` %% begin { \ "title \ ": \ "browserContext.newPage \ ", \ "category \ ": \ "pw:api \ "} ` ,
` %% end { \ "title \ ": \ "browserContext.newPage \ ", \ "category \ ": \ "pw:api \ "} ` ,
2021-08-12 17:58:00 +03:00
` %% end { \ "title \ ": \ "Before Hooks \ ", \ "category \ ": \ "hook \ "} ` ,
2021-08-03 08:11:37 +03:00
` %% begin { \ "title \ ": \ "First step \ ", \ "category \ ": \ "test.step \ "} ` ,
` %% begin { \ "title \ ": \ "expect.toBe \ ", \ "category \ ": \ "expect \ "} ` ,
` %% end { \ "title \ ": \ "expect.toBe \ ", \ "category \ ": \ "expect \ ", \ "error \ ":{ \ "message \ ": \ "expect(received).toBe(expected) // Object.is equality \\ n \\ nExpected: 2 \\ nReceived: 1 \ ", \ "stack \ ": \ "<stack> \ "}} ` ,
` %% end { \ "title \ ": \ "First step \ ", \ "category \ ": \ "test.step \ ", \ "error \ ":{ \ "message \ ": \ "expect(received).toBe(expected) // Object.is equality \\ n \\ nExpected: 2 \\ nReceived: 1 \ ", \ "stack \ ": \ "<stack> \ "}} ` ,
` %% begin { \ "title \ ": \ "After Hooks \ ", \ "category \ ": \ "hook \ "} ` ,
` %% begin { \ "title \ ": \ "browserContext.close \ ", \ "category \ ": \ "pw:api \ "} ` ,
` %% end { \ "title \ ": \ "browserContext.close \ ", \ "category \ ": \ "pw:api \ "} ` ,
` %% end { \ "title \ ": \ "After Hooks \ ", \ "category \ ": \ "hook \ "} ` ,
] ) ;
} ) ;
function stripEscapedAscii ( str : string ) {
return str . replace ( /\\u00[a-z0-9][a-z0-9]\[[^m]+m/g , '' ) ;
}