Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .github/workflows/pull-request.yml
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ jobs:
node-version: ${{ matrix.version }}
- run: npm ci
- run: npm run build
- run: npm run test
- run: npm run install:browsers
- run: npm run test:e2e
- name: junit report
Expand Down
4 changes: 1 addition & 3 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,5 @@ lib/
coverage/
test-e2e/report.*
.idea/
customDir/
dirToStoreTraces/
dirToStoreVideos/
traces/
video/
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@ Check [Keep a Changelog](http://keepachangelog.com/) for recommendations on how
:beetle: - bugfix
:x: - deprecation

## [0.41.1]
- :rocket: added _reuseSession_ option to keep browser/application opened after test ends

## [0.41.0]
- :rocket: introduced browserManager object to control all launched browser and electron instances
- :rocket: added steps to start/stop/switch to other browser/electron instances
Expand Down
15 changes: 15 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,21 @@ To properly use globals exposed by @qavajs/steps-playwright add corresponding ty
}
```

## reuseSession
reuseSession flag allows to share session between tests in frames of process. But setting of this flag
transfers session control to user.

```javascript
module.exports = {
default: {
browser: {
reuseSession: true
}
}
}
```


## Development and testing
Install dependencies
`npm install`
Expand Down
18 changes: 9 additions & 9 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@qavajs/steps-playwright",
"version": "0.41.0",
"version": "0.41.1",
"description": "steps to interact with playwright",
"main": "./index.js",
"scripts": {
Expand Down Expand Up @@ -37,7 +37,7 @@
"@types/express": "^4.17.21",
"@vitest/coverage-v8": "^0.34.6",
"@vitest/ui": "^0.34.6",
"electron": "^27.1.2",
"electron": "^27.1.3",
"express": "^4.18.2",
"ts-node": "^10.9.1",
"typescript": "^5.3.2",
Expand Down
22 changes: 17 additions & 5 deletions src/browserManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,16 @@ export class BrowserManager {
this.setContext((driverInstance as any as ElectronApplication).context());
this.setPage(await (driverInstance as any as ElectronApplication).firstWindow())
} else {
const context = await driverInstance.newContext(driverConfig);
const firstContext = driverInstance.contexts()[0];
const context = driverConfig.reuseSession && firstContext
? firstContext
: await driverInstance.newContext(driverConfig.capabilities);
this.setContext(context);
this.setPage(await context.newPage());
const firstPage = this.context?.pages()[0];
const page = driverConfig.reuseSession && firstPage
? firstPage
: await context.newPage();
this.setPage(page);
}
(this.context as NamedContext).name = 'default';
}
Expand Down Expand Up @@ -88,7 +95,9 @@ export class BrowserManager {
/**
* return to default state (1 browser, no contexts)
*/
async teardown() {
async teardown({ reuseSession } = { reuseSession: false }) {
this.setDriver(this.drivers['default']);
if (reuseSession) return;
for (const driverKey in this.drivers) {
const driverInstance = this.drivers[driverKey] as any;
if (driverInstance.firstWindow) {
Expand All @@ -97,7 +106,6 @@ export class BrowserManager {
await this.browserTeardown(driverInstance, driverKey);
}
}
this.driver = this.drivers['default'];
}

async browserTeardown(driverInstance: Browser, driverKey: string) {
Expand All @@ -120,7 +128,11 @@ export class BrowserManager {
async close() {
for (const driverKey in this.drivers) {
const driverInstance = this.drivers[driverKey] as any;
await driverInstance.close();
if (driverInstance.firstWindow) {
await this.electronTeardown(driverInstance, driverKey);
} else {
await driverInstance.close();
}
delete this.drivers[driverKey];
}
};
Expand Down
35 changes: 14 additions & 21 deletions src/hooks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,12 @@ import { po } from '@qavajs/po-playwright';
import {
saveScreenshotAfterStep,
saveScreenshotBeforeStep,
saveTrace,
saveVideo,
traceArchive
saveVideo
} from './utils/utils';
import { readFile } from 'node:fs/promises';
import { createJSEngine } from './selectorEngines';
import browserManager, {BrowserManager} from './browserManager';
import tracingManager from './utils/tracingManager';

declare global {
var browser: Browser
Expand All @@ -47,12 +46,7 @@ Before({name: 'Init'}, async function () {
config.driverConfig.capabilities.recordVideo = config.driverConfig.video;
}
await browserManager.launchDriver('default', config.driverConfig);
if (config.driverConfig.trace) {
await context.tracing.start({
screenshots: true,
snapshots: true
});
}
await tracingManager.start(driverConfig);
po.init(page, { timeout: config.driverConfig.timeout.present, logger: this });
po.register(config.pageObject);
global.browserManager = browserManager;
Expand Down Expand Up @@ -80,20 +74,19 @@ AfterStep(async function (step: ITestStepHookParameter) {
});

After({name: 'Teardown'}, async function (scenario: ITestCaseHookParameter) {
if (saveTrace(config.driverConfig, scenario)) {
const path = traceArchive(config.driverConfig, scenario);
await context.tracing.stop({ path });
if (config.driverConfig?.trace.attach) {
const zipBuffer: Buffer = await readFile(path);
this.attach(zipBuffer.toString('base64'), 'base64:application/zip');
}
}
await browserManager.teardown();
await tracingManager.stop(config.driverConfig, this, scenario);
await browserManager.teardown({ reuseSession: config.driverConfig.reuseSession });
if (saveVideo(config.driverConfig, scenario)) {
if (config.driverConfig?.video.attach) {
const videoPath = await page.video()?.path() ?? '';
const zipBuffer: Buffer = await readFile(videoPath);
this.attach(zipBuffer.toString('base64'), 'base64:video/webm');
const video = page.video();
console.log(video)
if (video) {
const videoPath = await video.path();
const zipBuffer: Buffer = await readFile(videoPath);
this.attach(zipBuffer.toString('base64'), 'base64:video/webm');
} else {
console.warn('Video was not recorded');
}
}
}
});
Expand Down
13 changes: 10 additions & 3 deletions src/multiBrowser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,14 @@ When('I launch new driver as {string}', async function (driverName: string) {
* @param {string} driverName - driver name
* @param {string} config - json with browser config
* @example
* When I launch new driver as 'browser2'
* When I launch new driver as 'browser2':
* """
* {
* "capabilities": {
* "browserName": "firefox"
* }
* }
* """
*/
When('I launch new driver as {string}:', async function (driverName: string, rawConfig: string) {
const config = await getValue(rawConfig);
Expand All @@ -40,7 +47,7 @@ When('I switch to {string} driver', async function (driverName: string) {
* Close driver
* @param {string} driverName - driver name
* @example
* When I close to 'browser2' driver
* When I close 'browser2' driver
*/
When('I close {string} driver', async function (driverName: string) {
await browserManager.closeDriver(driverName);
Expand Down Expand Up @@ -72,7 +79,7 @@ When('I switch to {string} browser context', async function (browserContextName:
* Close browser context
* @param {string} browserContextName - browser context name
* @example
* When I close to 'browser2' browser context
* When I close 'browser2' browser context
*/
When('I close {string} browser context', async function (browserContextName: string) {
await browserManager.closeContext(browserContextName);
Expand Down
32 changes: 32 additions & 0 deletions src/utils/tracingManager.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import { saveTrace, traceArchive } from './utils';
import { readFile } from 'node:fs/promises';

class TracingManager {

isTracingStarted = false;
async start(driverConfig: any) {
if (driverConfig.trace) {
if (!driverConfig.reuseSession || (driverConfig.reuseSession && !this.isTracingStarted)) {
this.isTracingStarted = true;
await context.tracing.start({
screenshots: true,
snapshots: true
});
}
await context.tracing.startChunk();
}
}

async stop(driverConfig: any, world: any, scenario: any) {
if (saveTrace(config.driverConfig, scenario)) {
const path = traceArchive(config.driverConfig, scenario);
await context.tracing.stopChunk({ path });
if (driverConfig?.trace.attach) {
const zipBuffer: Buffer = await readFile(path);
world.attach(zipBuffer.toString('base64'), 'base64:application/zip');
}
}
}
}

export default new TracingManager();
1 change: 1 addition & 0 deletions test-e2e/features/validations.feature
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
@debug
Feature: validations

Background:
Expand Down
15 changes: 8 additions & 7 deletions test-e2e/webui.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,16 +15,16 @@ const common = {
},
trace: {
event: ['onFail'],
dir: 'dirToStoreTraces',
dir: 'traces',
attach: true
},
video: {
event: ['onFail'],
dir: 'dirToStoreVideos',
dir: 'video',
size: { width: 800, height: 600 },
attach: true
},
screenshot: ['onFail'],
screenshot: ['onFail']
},
format: [
'@qavajs/console-formatter',
Expand Down Expand Up @@ -60,17 +60,18 @@ export const debug = {
headless: false
},
trace: {
event: 'onFail',
dir: 'customDir',
event: ['onFail'],
dir: 'traces',
attach: true
},
video: {
event: ['onFail'],
event: ['afterScenario'],
dir: 'video',
size: { width: 800, height: 600 },
attach: true
}
}
},
parallel: 1
}

export const electron = {
Expand Down
Loading