In this blog, we will explore the utilization of the 'Pause' feature and the Playwright Inspector. Additionally, we will look into the concept of managing multiple browser pages or tabs using a single browser instance.

Topics that we will cover:

  • ‘Pause’ feature alongwith Playwright Inspector
  • Utilizing a single instance of a browser to open and manage multiple browser pages/tabs
  • Source code

‘Pause’ feature alongwith Playwright Inspector 

There are instances when we may feel the need to temporarily suspend (pause) our test in order to either debug or wait for a particular event to take place. 

The .pause() method is useful in this situation, please refer to the method description provided below 

 Upon execution of the script, observe below that the 'Playwright Inspector' opens, causing the script to pause and the browser no longer closes automatically as it did before

Utilizing a single instance of a browser to open and manage multiple browser pages/tabs

Multiple browser pages can be launched using a single browser instance. Let's explore the process. 

By utilizing the 'newContext()' method as illustrated below, it is possible to generate a brand new browser context.

The browser context, as mentioned in the description, does not share its cache with other browser contexts 

Based on this context, we have the ability to construct a brand new browse page.

In a similar manner, we can generate two additional pages using the same browser context

 Execute the code.

Observe that there are three open tabs below

 Execute.

Notice that 4 window tabs open this time

 

So this is how we work with ‘pause’, inspector and multiple browser pages.

Source code (pause method)

package com.rsa.playwrightjava;


import org.junit.jupiter.api.Test;

import com.microsoft.playwright.Browser;

import com.microsoft.playwright.BrowserType;

import com.microsoft.playwright.BrowserType.LaunchOptions;

import com.microsoft.playwright.Page;

import com.microsoft.playwright.Playwright;


public class Blog4_Pause {

@Test

public void PlaywrightJTest() {

Playwright pt = Playwright.create();

LaunchOptions lo = new LaunchOptions();

//lo.setChannel("chrome");

lo.setChannel("msedge");

lo.setHeadless(false);

BrowserType btype = pt.chromium();

//BrowserType btype = pt.firefox();

//BrowserType btype = pt.webkit();

Browser b = btype.launch((new BrowserType.LaunchOptions().setHeadless(false)));

//Page pg = pt.chromium().launch().newPage();

Page pg = b.newPage();

pg.navigate("https://rahulshettyacademy.com/");

pg.pause();

System.out.println(pg.title());

}

}


Source code (multiple browser pages)

package com.rsa.playwrightjava;


import org.junit.jupiter.api.Test;


import com.microsoft.playwright.Browser;

import com.microsoft.playwright.BrowserContext;

import com.microsoft.playwright.BrowserType;

import com.microsoft.playwright.BrowserType.LaunchOptions;

import com.microsoft.playwright.Page;

import com.microsoft.playwright.Playwright;


public class Blog4_Pause {

@Test

public void PlaywrightJTest() {

Playwright pt = Playwright.create();

LaunchOptions lo = new LaunchOptions();

//lo.setChannel("chrome");

lo.setChannel("msedge");

lo.setHeadless(false);

BrowserType btype = pt.chromium();

//BrowserType btype = pt.firefox();

//BrowserType btype = pt.webkit();

Browser b = btype.launch((new BrowserType.LaunchOptions().setHeadless(false)));

BrowserContext bcontext = b.newContext();

Page pg1 = bcontext.newPage();

Page pg2 = bcontext.newPage();

Page pg3 = bcontext.newPage();

Page pg4 = bcontext.newPage();

//Page pg = b.newPage();

pg1.navigate("https://courses.rahulshettyacademy.com/courses/");

pg2.navigate("https://rahulshettyacademy.com/mentorship");

pg3.navigate("https://rahulshettyacademy.com/learning-path");

pg4.navigate("https://rahulshettyacademy.com");

pg4.pause();

//System.out.println(pg1.title());

}

}


Thanks!


Tags


You may also like

Leave a Reply

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

{"email":"Email address invalid","url":"Website address invalid","required":"Required field missing"}