Difference between ‘Browser Context’ and ‘Page’ in Playwright Python

Tutorial 15​​ ​​ Difference between ‘Browser Context’ and ‘Page’​​ in Playwright​​ Python​​ 

What you will Learn​​ in this​​ blog:

  • ‘Browser Context’ versus ‘Page’​​ in Playwright​​ python

  • Launch 2 browser contexts

  • Launch 2 pages in 2nd​​ browser context

  • Code snippets

‘Browser Context’ vs​​ ‘Page’, what is the difference?

So far, in every script, we have been using the keywords ‘browser’, ‘context’ and ‘page’, see below​​ 

Let us understand the​​ conceptual​​ difference by launching​​ a browser

As seen above, a browser window gets launched and this is simply the ‘browser context’ or ‘browser instance’.

Next, let us navigate to below webpage

https://www.way2automation.com/lifetime-membership-club/​​ 

As seen above,​​ we now have a page or a tab within this browser​​ context.

Let us click + and launch​​ https://www.way2automation.com/soapui/rest-api-webservices-testing-training/ ​​​​ ​​ 

As seen above, we now have 2 tabs/pages within a single browser context.

Next, let us launch 2 browsers, so we now have 2 browser contexts.​​ The second browser context seen below have 2 pages while the first context has only 1 page​​ 

Let us see the same thing from code perspective.​​ 

Coming back to the same screenshot, now you should be able to understand that:​​ first​​ we are launching a chromium browser,​​ second​​ we are creating an instance or browser context,​​ third​​ we are creating a page and navigating to a site

Let us execute below

Notice that a browser context is lauched navigating to a page​​ 

Launch 2 browser contexts

Let us create a 2nd​​ browser context, see line#12

Create a page linked to 2nd​​ browser context

Save and execute.​​ 

Notice below that 2 browser contexts are launched

Launch 2 pages in 2nd​​ browser context

Let us create a 2nd​​ page in 2nd​​ browser context

Execute.​​ 

The result below is self-explanatory


Code snippet​​ (single browser context and a page)

import​​ os

from​​ playwright.sync_api​​ import​​ Playwright,​​ sync_playwright,​​ expect


def​​ run(playwright: Playwright) ->​​ None:
 ​​ ​​ ​​​​ browser = playwright.chromium.launch(
headless=False)
 ​​ ​​ ​​​​ context = browser.new_context()


 ​​ ​​ ​​​​ page = context.new_page()

 ​​ ​​ ​​​​ page.goto(
"https://www.way2automation.com/lifetime-membership-club/")

 ​​ ​​ ​​​​ page.wait_for_timeout(
5000)

 ​​ ​​ ​​​​ 
# ---------------------
 ​​ ​​ ​​​​ 
context.close()
 ​​ ​​ ​​​​ browser.close()



with​​ sync_playwright()​​ as​​ playwright:
 ​​ ​​ ​​​​ run(playwright)

 

Code snippet (2 browser contexts)

import​​ os

from​​ playwright.sync_api​​ import​​ Playwright,​​ sync_playwright,​​ expect


def​​ run(playwright: Playwright) ->​​ None:
 ​​ ​​ ​​​​ browser = playwright.chromium.launch(
headless=False)
 ​​ ​​ ​​​​ 
# 1st browser context
 ​​ ​​ ​​​​ 
context = browser.new_context()

 ​​ ​​ ​​​​ 
# 2nd browser context
 ​​ ​​ ​​​​ 
context2 = browser.new_context()

 ​​ ​​ ​​​​ page = context.new_page()

 ​​ ​​ ​​​​ page.goto(
"https://www.way2automation.com/lifetime-membership-club/")

 ​​ ​​ ​​​​ 
# open page in 2nd browser context
 ​​ ​​ ​​​​ 
page2 = context2.new_page()
 ​​ ​​ ​​​​ page2.goto(
"https://www.way2automation.com/soapui/rest-api-webservices-testing-training/")

 ​​ ​​ ​​​​ page.wait_for_timeout(10000)
 ​​ ​​ ​​​​ page2.wait_for_timeout(
5000)

 ​​ ​​ ​​​​ 
# ---------------------
 ​​ ​​ ​​​​ 
context.close()
 ​​ ​​ ​​​​ browser.close()



with​​ sync_playwright()​​ as​​ playwright:
 ​​ ​​ ​​​​ run(playwright)

 

Code snippet (2 pages in 2nd browser context)

import​​ os

from​​ playwright.sync_api​​ import​​ Playwright,​​ sync_playwright,​​ expect


def​​ run(playwright: Playwright) ->​​ None:
 ​​ ​​ ​​​​ browser = playwright.chromium.launch(
headless=False)
 ​​ ​​ ​​​​ 
# 1st browser context
 ​​ ​​ ​​​​ 
context = browser.new_context()

 ​​ ​​ ​​​​ 
# 2nd browser context
 ​​ ​​ ​​​​ 
context2 = browser.new_context()

 ​​ ​​ ​​​​ page = context.new_page()

 ​​ ​​ ​​​​ page.goto(
"https://www.way2automation.com/lifetime-membership-club/")

 ​​ ​​ ​​​​ # open page in 2nd browser context
 ​​ ​​ ​​​​ 
page2 = context2.new_page()
 ​​ ​​ ​​​​ page2.goto(
"https://www.way2automation.com/soapui/rest-api-webservices-testing-training/")

 ​​ ​​ ​​​​ 
# open 2 pages in 2nd browser context
 ​​ ​​ ​​​​ 
page3 = context2.new_page()
 ​​ ​​ ​​​​ page3.goto(
"https://www.way2automation.com/soapui/rest-api-webservices-testing-training/")

 ​​ ​​ ​​​​ page.wait_for_timeout(
10000)
 ​​ ​​ ​​​​ page2.wait_for_timeout(
5000)

 ​​ ​​ ​​​​ 
# ---------------------
 ​​ ​​ ​​​​ 
context.close()
 ​​ ​​ ​​​​ browser.close()



with​​ sync_playwright()​​ as​​ playwright:
 ​​ ​​ ​​​​ run(playwright)

 

Thank you for reading!

Share On

Share on facebook
Share on twitter
Share on linkedin
Share on whatsapp
Share on tumblr
Share on email

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top

Lifetime Membership Club

LIFETIME MEMBERSHIP BIG SALE - ALL LIVE COURES JUST - 10000 RS/149 USD
Attend All Live courses in just 10000 rs / $149 - offer ends 25th April 2024