This is a Playwright module for NestJS, making it easy to inject the playwright into your project. It's modeled after the official modules, allowing for asynchronous configuration and such.
Inspired and forked from nest-puppeteer
In your existing NestJS-based project:
npm install nestjs-playwright playwright
Overall, it works very similarly to any injectable module described in the NestJS documentation. You may want to refer to those docs as well -- and maybe the dependency injection docs too if you're still trying to wrap your head around the NestJS implementation of it.
In the simplest case, you can explicitly specify options you'd normally provide to your playwright.chromium.launch
or the instance name using PlaywrightModule.forRoot()
:
import { Module } from '@nestjs/common';
import { PlaywrightModule } from 'nestjs-playwright';
@Module({
imports: [
PlaywrightModule.forRoot(
{
headless: true,
channel: 'chrome',
isGlobal: true,
}, // optional, any Playwright launch options here or leave empty for good defaults */,
'BrowserInstanceName', // optional, can be useful for using Chrome and Firefox in the same project
),
],
})
export class CatsModule {}
To inject the Playwright Browser
object:
import type { Browser } from 'playwright';
import { Injectable } from '@nestjs/common';
import { InjectBrowser } from 'nestjs-playwright';
import { Cat } from './interfaces/cat';
@Injectable()
export class CatsRepository {
constructor(@InjectBrowser() private readonly browser: Browser) {}
async create(cat: Cat) {
const version = this.browser.version();
return { version };
}
}
To inject a new incognito BrowserContext
object:
import { Module } from '@nestjs/common';
import { PlaywrightModule } from 'nestjs-playwright';
import { CatsController } from './cats.controller';
import { CatsService } from './cats.service';
@Module({
imports: [PlaywrightModule.forFeature()],
controllers: [CatsController],
providers: [CatsService],
})
export class CatsModule {}
import type { BrowserContext } from 'playwright';
import { Injectable } from '@nestjs/common';
import { InjectContext } from 'nestjs-playwright';
import { Cat } from './interfaces/cat';
@Injectable()
export class CatsRepository {
constructor(
@InjectContext() private readonly browserContext: BrowserContext,
) {}
async create(cat: Cat) {
const page = await this.browserContext.newPage();
await page.goto('https://test.com/');
return await page.content();
}
}
Inject Page
object:
import { Injectable } from '@nestjs/common';
import { InjectPage } from 'nestjs-playwright';
import type { Page } from 'playwright';
@Injectable()
export class CrawlerService {
constructor(@InjectPage() private readonly page: Page) {}
async crawl(url: string) {
await this.page.goto(url, { waitUntil: 'networkidle2' });
const content = await this.page.content();
return { content };
}
}
If you want to pass in Playwright configuration options from a ConfigService or other provider, you'll need to perform the Playwright module configuration asynchronously, using PlaywrightModule.forRootAsync()
. There are several different ways of doing this.
The first is to specify a factory function that populates the options:
import { Module } from '@nestjs/common'
import { PlaywrightModule } from 'nestjs-playwright'
import { ConfigService } from '../config/config.service'
@Module({
imports: [PlaywrightModule.forRootAsync({
imports: [ConfigModule],
useFactory: (config: ConfigService) => {
launchOptions: config.chromeLaunchOptions,
},
inject: [ConfigService]
})]
})
export class CatsModule {}
Alternatively, you can write a class that implements the PlaywrightOptionsFactory
interface and use that to create the options:
import { Module } from '@nestjs/common';
import {
PlaywrightModule,
PlaywrightOptionsFactory,
PlaywrightModuleOptions,
} from 'nestjs-playwright';
@Injectable()
export class PlaywrightConfigService implements PlaywrightOptionsFactory {
private readonly launchOptions = { pipe: true };
private readonly dbName = 'BestAppEver';
createMongoOptions(): PlaywrightModuleOptions {
return {
launchOptions: this.launchOptions,
instanceName: this.instanceName,
};
}
}
@Module({
imports: [
PlaywrightModule.forRootAsync({
useClass: PlaywrightConfigService,
}),
],
})
export class CatsModule {}
Just be aware that the useClass
option will instantiate your class inside the PlaywrightModule, which may not be what you want.
If you wish to instead import your PlaywrightConfigService class from a different module, the useExisting
option will allow you to do that.
import { Module } from '@nestjs/common'
import { PlaywrightModule } from 'nest-playwright'
import { ConfigModule, ConfigService } from '../config/config.service'
@Module({
imports: [PlaywrightModule.forRootAsync({
imports: [ConfigModule]
useExisting: ConfigService
})]
})
export class CatsModule {}
In this example, we're assuming that ConfigService
implements the PlaywrightOptionsFactory
interface and can be found in the ConfigModule.
When you want to use PlaywrightModule
in other modules, you'll need to import it (as is standard with any Nest module). Alternatively, declare it as a global module by setting the options object's isGlobal
property to true
, as shown below. In that case, you will not need to import PlaywrightModule
in other modules once it's been loaded in the root module (e.g., AppModule
).
PlaywrightModule.forRoot({
isGlobal: true,
});
- Support for Firefox & Opera
nestjs-playwright
is MIT licensed.