-
Notifications
You must be signed in to change notification settings - Fork 133
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
addInitScript in qawolf.register since it happens before pages load, …
…so it should not throw any errors with the addInitScript - Remove the ability to independently call indexPages. Just call qawolf.register and indexPages and ContextEventCollector work. One way to do things
- Loading branch information
Showing
12 changed files
with
149 additions
and
218 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,99 @@ | ||
import Debug from 'debug'; | ||
import { readFileSync } from 'fs'; | ||
import { BrowserContext, Page } from 'playwright-core'; | ||
import { basename, join } from 'path'; | ||
import { loadConfig } from '../../config'; | ||
import { Registry } from '../Registry'; | ||
import { saveArtifacts } from './saveArtifacts'; | ||
|
||
const debug = Debug('qawolf:register'); | ||
|
||
const scriptPath = require.resolve('../../../build/qawolf.web.js'); | ||
const webScript = readFileSync(scriptPath, 'utf8'); | ||
|
||
export type IndexedPage = Page & { | ||
createdIndex: number; | ||
}; | ||
|
||
export type RegisteredBrowserContext = BrowserContext & { | ||
_qawRegistered: boolean; | ||
}; | ||
|
||
export const addInitScript = async (context: BrowserContext): Promise<void> => { | ||
const attribute = JSON.stringify(loadConfig().attribute); | ||
|
||
const script = | ||
'(() => {\n' + | ||
webScript + | ||
`\nnew qawolf.PageEventCollector({ attribute: ${attribute} });\n` + | ||
'})();'; | ||
|
||
await context.addInitScript(script); | ||
}; | ||
|
||
export const isRegistered = (context: BrowserContext): boolean => { | ||
return !!(context as RegisteredBrowserContext)._qawRegistered; | ||
}; | ||
|
||
export const getArtifactPath = (): string | null => { | ||
let artifactPath = process.env.QAW_ARTIFACT_PATH; | ||
if (!artifactPath) return null; | ||
|
||
if (require.main) { | ||
// store artifacts under the name of the main module, if there is one | ||
// ex. /artifacts/search.test.js | ||
artifactPath = join(artifactPath, basename(require.main.filename)); | ||
} | ||
|
||
// store artifacts under the name of the browser being tested | ||
const browserName = process.env.QAW_BROWSER; | ||
if (browserName && artifactPath) { | ||
artifactPath = join(artifactPath, browserName); | ||
} | ||
|
||
return artifactPath; | ||
}; | ||
|
||
export const indexPages = async (context: BrowserContext): Promise<void> => { | ||
/** | ||
* Set page.createdIndex on pages. | ||
*/ | ||
let index = 0; | ||
|
||
const pages = context.pages(); | ||
if (pages.length > 1) { | ||
throw new Error( | ||
`Cannot index pages when more than 1 exist (${pages.length})`, | ||
); | ||
} | ||
|
||
if (pages[0]) { | ||
debug(`index existing page ${index}`); | ||
(pages[0] as IndexedPage).createdIndex = index++; | ||
} | ||
|
||
context.on('page', (page: IndexedPage) => { | ||
debug(`index created page ${index}`); | ||
page.createdIndex = index++; | ||
}); | ||
}; | ||
|
||
export const register = async (context: BrowserContext): Promise<void> => { | ||
Registry.instance().setContext(context); | ||
|
||
if (isRegistered(context)) return; | ||
(context as RegisteredBrowserContext)._qawRegistered = true; | ||
|
||
const promises: Promise<any>[] = []; | ||
|
||
promises.push(addInitScript(context)); | ||
|
||
promises.push(indexPages(context)); | ||
|
||
const artifactPath = getArtifactPath(); | ||
if (artifactPath) { | ||
promises.push(saveArtifacts(context, artifactPath)); | ||
} | ||
|
||
await Promise.all(promises); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,24 +1,46 @@ | ||
import Debug from 'debug'; | ||
import { appendFileSync } from 'fs'; | ||
import { ensureDir } from 'fs-extra'; | ||
import { dirname } from 'path'; | ||
// import { appendFileSync } from 'fs'; | ||
// import { ensureDir } from 'fs-extra'; | ||
// import { dirname } from 'path'; | ||
import { Page } from 'playwright-core'; | ||
import { interceptConsoleLogs } from './interceptConsoleLogs'; | ||
// import { QAWolfWeb } from '../../web'; | ||
// import { LogCallback } from '../../web/interceptConsoleLogs'; | ||
|
||
const debug = Debug('qawolf:saveConsoleLogs'); | ||
// const debug = Debug('qawolf:saveConsoleLogs'); | ||
|
||
// let logCallbackId = 0; | ||
|
||
// export const interceptConsoleLogs = async ( | ||
// page: Page, | ||
// callback: LogCallback, | ||
// ): Promise<void> => { | ||
// // TODO refactor | ||
// // await addScriptToPage(page); | ||
|
||
// const callbackName = `interceptLogs${logCallbackId++}`; | ||
// await page.exposeFunction(callbackName, callback); | ||
|
||
// await initEvaluateScript( | ||
// page, | ||
// (callbackName: string) => { | ||
// const web: QAWolfWeb = (window as any).qawolf; | ||
// web.interceptConsoleLogs(callbackName); | ||
// }, | ||
// callbackName, | ||
// ); | ||
// }; | ||
|
||
export const saveConsoleLogs = async ( | ||
page: Page, | ||
savePath: string, | ||
): Promise<void> => { | ||
debug(`save console logs at ${savePath}`); | ||
|
||
await ensureDir(dirname(savePath)); | ||
|
||
const callback = (level: string, message: string): void => { | ||
const line = `${level}: ${message}\n`; | ||
appendFileSync(savePath, line); | ||
}; | ||
|
||
return interceptConsoleLogs(page, callback); | ||
console.log('page', page, 'savePath', savePath); | ||
// TODO | ||
// debug(`save console logs at ${savePath}`); | ||
// await ensureDir(dirname(savePath)); | ||
// const callback = (level: string, message: string): void => { | ||
// const line = `${level}: ${message}\n`; | ||
// appendFileSync(savePath, line); | ||
// }; | ||
// return interceptConsoleLogs(page, callback); | ||
}; |
Oops, something went wrong.