-
Notifications
You must be signed in to change notification settings - Fork 134
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #733 from qawolf/fix-crash
Use qawolf.register to add qawolf script once before pages are loaded
- Loading branch information
Showing
33 changed files
with
265 additions
and
451 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 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 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,70 @@ | ||
import { readFileSync } from 'fs'; | ||
import { BrowserContext } from 'playwright-core'; | ||
import { basename, join } from 'path'; | ||
import { loadConfig } from '../../config'; | ||
import { indexPages } from './indexPages'; | ||
import { Registry } from '../Registry'; | ||
import { saveArtifacts } from './saveArtifacts'; | ||
|
||
const scriptPath = require.resolve('../../../build/qawolf.web.js'); | ||
const webScript = readFileSync(scriptPath, 'utf8'); | ||
|
||
export type RegisteredBrowserContext = BrowserContext & { | ||
_qawRegistered: boolean; | ||
}; | ||
|
||
export const addInitScript = async (context: BrowserContext): Promise<void> => { | ||
const attribute = JSON.stringify(loadConfig().attribute); | ||
|
||
const script = | ||
'(() => {\n' + | ||
webScript + | ||
`new qawolf.PageEventCollector({ attribute: ${attribute} });\n` + | ||
'qawolf.interceptConsoleLogs();\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 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 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 was deleted.
Oops, something went wrong.
Oops, something went wrong.