Learning Outcome
5
Follow best practices for efficient resource management.
4
Close browser resources in the correct order.
3
Create and manage Playwright, Browser, Context, and Page objects.
2
Understand each stage of the browser lifecycle.
1
Explain the Browser Lifecycle in Playwright
Imagine you are staying in a hotel.
To complete your stay, you follow a sequence of steps:
Arrive at the hotel
Check in at the reception
Receive your room
Enter the room
Stay and use the room.
Pack your belongings.
Check out.
Leave the hotel.
Similarly, Playwright follows a structured Browser Lifecycle where every object must be created and closed in the correct order.
What is Browser Lifecycle?
Browser Lifecycle refers to the complete journey of a browser during test execution, from launching the browser until closing it.
What problems they are facing
Create Playwright Instance
Launch Browser
Create Browser Context
Create Page
Close Browser
Close Context
Close Page
Perform Browser Actions
Close Playwright
Browser Lifecycle Flow
Create Page
Start Test
Create Playwright Instance
Launch Browser
Create Browser Context
Close Playwright
Perform Browser Actions
Close Page
Close Context
Close Browser
Browser Lifecycle Components
Component
Playwright
Entry point for automation
Browser
Launches the browser
Browser Context
Creates an isolated session
Component
Page
Represents a browser tab
Browser Actions
Performs user operations
Page Close
Closes the current tab
Removes session data
Context Close
Component
Browser Close
Terminates the browse
Releases Playwright resources
Playwright Close
Component
Browser Lifecycle Components
Playwright playwright
Playwright.create();
Browser browser
playwright.chromium().launch();
BrowserContext context
browser.newContext();Page page
context.newPage();page.navigate
"https://www.google.com"page.close();context.close();browser.close();playwright.close();
Best Practices
Create a new Browser Context for isolated sessions
Close the Page before closing the Context
Close the Context before closing the Browser.
Always create Playwright first.
Always call playwright.close() at the end.
Best Practices
Create a new Browser Context for isolated sessions
Close the Page before closing the Context
Close the Context before closing the Browser.
Always create Playwright first.
Always call playwright.close() at the end.
Browser Context
A Browser Context is an browser session.
Think of it as an independent browser profile.
Without Browser Context
(Browser)
User A Login
User B Login
Session Conflict
With Browser Context
(Browser)
Context A → User A
Context B → User B
No Session Conflict
Real-World Applications
Proper Browser Lifecycle management is important in:
Parallel Test Execution
Target Audience
Cross-Browser Testing
Large Enterprise Automation Projects
Regression Testing
CI/CD Pipelines
Summary
4
Proper lifecycle management improves stability and performance.
3
Resources should always be closed in the correct order
2
Every object has a specific responsibility
1
Browser Lifecycle is the complete journey of browser execution
Quiz
Which object is created first?
A. Browser
B. Context
C.Playwright
D. Page
Quiz-Answer
Which object is created first?
A. Browser
B. Context
C.Playwright
D. Page