QA Automation Web Automation

Working with Browser Launch in Playwright

Working with Browser Launch

In this blog, we will cover working with different browser launch operations in Playwright and how to interact with them. 

Pre-requisites:

As a pre-requisite we must check whether the following dependencies are installed or not:

  1. Project setup
  2. Run pip list in PyCharm terminal to check whether playwright is installed or not. 
Browser launch in playwright

As shown in the above picture playwright should be installed in the project. 

If the playwright is not installed refer to Web Automation Testing using Playwright.

What is Browser?

A Browser is the instance of Chromium, Firefox, and WebKit. Every playwright script starts via browser instance launching and ends by closing the browser instance.  

In Playwright By default browser instances are launched in headless mode I.e. GUI is not visible on screen. To make GUI visible onscreen we must pass the parameter to launch() method as headless=False. 

Browser Launch in Playwright:

  • Depending on the Programming Language we choose; Playwright will auto-download browser dependencies at package install time for you, or you will need to use Playwright CLI to install these browser dependencies. Using Playwright CLI to download all browser dependencies. 
Browser launch in playwright
  • We can install specific browser dependencies by providing the arguments that we have to pass the Browser Name as an argument.
Browser launch in playwright
  • Playwright Launches the Default Browser if we don’t specify the channel parameter in launch() method. 
  • By default Playwright Launches the browser in Headless mode, to launch the browser in headed mode we should pass the argument as headless=False in the Launch Method. 
Browser launch in playwright
  • We can launch the different browsers by providing the browser name in the channel parameter
  • Playwright uses the Chromium browser builds to launch the Chromium-based browsers such as Chrome, MS Edge, Firefox,etc

Test Case Scenario for Sync API:

  1. Open Browser. 
  2. In launch() method pass the parameter as a channel for MSedge, chrome, and Firefox. 
  3. Navigate to https://www.google.com 
# Using Sync Methods 
from playwright.sync_api import sync_playwright 
with sync_playwright() as Page: 
 
    #To Launch Default Chromium Browser 
    Browser = Page.chromium.launch(headless=False) 
    page = Browser.new_page() 
    page.goto("https://www.google.com") 
 
    #To Launch Edge Browser 
    Browser = Page.chromium.launch(headless=False,channel="msedge") 
    page= Browser.new_page() 
    page.goto("https://www.google.com") 
 
    #To Launch Chrome Browser 
    Browser = Page.chromium.launch(headless=False, channel="chrome") 
    page = Browser.new_page() 
    page.goto("https://www.google.com") 
 
    #To Launch Firefox Browser 
    Browser = Page.chromium.launch(headless=False,channel="firefox") 
    page = Browser.new_page() 
    page.goto("https://www.google.com") 
Sync method
  • As Shown in the above code snippet the Statement at line no. 6 is used to launch the browser. The browser class is available in the page variable.  
  • In Playwright if we don’t provide the channel parameter then playwright will launch chromium as the default browser.  
  • The Statement at line no. 12 is used to open a new tab in the browser. 
  • The Statement at line no. 13 has the page variable which contains a .goto() method. It is used to navigate to the website. 
  • The Statement at line no. 11, the launch() method contains an additional parameter as the channel. In the channel, we can pass the browser name, which we want to launch such as ‘chrome’,’msedge’, etc. 

Test Case Scenario for Async API:

  1. Open Browser. 
  2. In launch() method pass the parameter as a channel for MSedge, chrome, and Firefox. 
  3. Navigate to https://www.google.com 
# Using ASync Method 
import asyncio 
from playwright.async_api import async_playwright 
async def Launch_Browser(): 
    async with async_playwright() as Page: 
 
        # To Lanuch Default Chromium Browser 
        browser = await Page.chromium.launch(headless=False) 
        page = await browser.new_page() 
        await page.goto("https://www.google.com") 
 
        # To Lanuch Chrome Browser 
        browser = await Page.chromium.launch(headless=False,channel="chrome") 
        page = await browser.new_page() 
        await page.goto("https://www.google.com") 
 
        # To Lanuch msedge Browser 
        browser = await Page.chromium.launch(headless=False,channel="msedge") 
        page = await browser.new_page() 
        await page.goto("https://www.google.com") 
 
        # To Lanuch firefox Browser 
        browser = await Page.chromium.launch(headless=False,channel="firefox") 
        page = await browser.new_page() 
        await page.goto("https://www.google.com") 
 
asyncio.run(Launch_Browser()) 
Async method
  • As shown in the above code snippet the Statement at lino no. 8 is used to launch the chromium browser. In Async code, we have to use the await in page class before every playwright keyword. 
  • The Statement at line no. 9 we have used the browser class to open the new page in async mode using new_page() method. 
  • The statement at line no. 10 we have used the async .goto method with await keyword to navigate to the website. 

Stay tuned for more informative blogs on Playwright.