krishworkstech.com

Pages & New Window

Pages & New Window

Each BrowserContext may contain several pages. A Page denotes an individual tab or a popup window within a browser context. It is intended for navigating to URLs and engaging with the content of the page.

PAGES AND NEW WINDOW - COVER
				
					// code snippet:
// Create a page.
const page = await context.newPage();

// Navigate explicitly, similar to entering a URL in the browser.
await page.goto('http://example.com');
// Fill an input.
await page.locator('#search').fill('query');

// Navigate implicitly by clicking a link.
await page.locator('#submit').click();
// Expect a new url.
console.log(page.url());

				
			

Multiple pages

Each browser context is capable of hosting several pages (tabs).

  1. Each page functions as a focused, active entity. It is unnecessary to bring the page to the forefront.
  2. Pages within a context adhere to context-level emulation, including viewport dimensions, custom network routes, or browser locale.
				
					// code snippet:
// Create two pages
const pageOne = await context.newPage();
const pageTwo = await context.newPage();

// Get pages of a browser context
const allPages = context.pages();

				
			

Handling new pages

The page event within browser contexts serves to retrieve newly created pages in that context. This functionality can be utilized to manage new pages that are opened via target=”_blank” links.

				
					// code snippet:
// Start waiting for new page before clicking. Note no await.
const pagePromise = context.waitForEvent('page');
await page.getByText('open new tab').click();
const newPage = await pagePromise;
// Interact with the new page normally.
await newPage.getByRole('button').click();
console.log(await newPage.title());

				
			

If the action that initiates the new page is not known, the subsequent pattern may be employed.

				
					// code snippet:
// Get all new pages (including popups) in the context
context.on('page', async page => {
  await page.waitForLoadState();
  console.log(await page.title());
});

				
			

Handling popups

If a pop-up is opened by the page (for instance, pages accessed via target=”_blank” links), you can obtain a reference to it by monitoring the popup event on the page.

 

This event is triggered alongside the browserContext.on(‘page’) event, but it is specifically for pop-ups that pertain to this page.

				
					// code snippet:
// Start waiting for popup before clicking. Note no await.
const popupPromise = page.waitForEvent('popup');
await page.getByText('open the popup').click();
const popup = await popupPromise;
// Interact with the new popup normally.
await popup.getByRole('button').click();
console.log(await popup.title());

				
			

If the action that activates the popup is not identified, the subsequent pattern may be employed.

				
					// code snippet:
// Get all popups when they open
page.on('popup', async popup => {
  await popup.waitForLoadState();
  console.log(await popup.title());
});

				
			

Open a New Window

Each context is like a separate browser window with isolated cookies and cache

				
					// code snippet:
const newContext = await browser.newContext();
const newPage = await newContext.newPage();
await newPage.goto('https://example.com/');
				
			

Playwright Essentials: A Step-by-Step Journey into Test Automation

cover - part 9

Part 01: Master Playwright Testing – Framework from Scratch Guide

cover - part 10

Part 02: How to install Playwright

cover - part 11

Part 03: Basic Playwright Test Structure

cover - part 13 (1)

Part 04: Importance of Playwright configuration file and its details to run the tests

cover - part 17

Part 05: Writing Tests in PlayWright

cover - part 19

Part 06: Playwright Actions Illustrated

cover - part 13 (1)

Part 07: Auto-waiting

cover - part 17

Part 08: Handling the Dialogs

Rectangle 26833

Part 09: Download & Handling frames

Rectangle 26834

Part 10: Locators

cover - part 17

Part 11: Pages & New Window

Rectangle 26836

Part 12: Page object models

Scroll to Top
  • Schematic design
  • PCB and schematic source files
  • Assembling drawing files
  • Providing prototype/sample and production PCB service
  • Testing and validation of designed hardware
  • HIPAA
  • Azure Key
  • Management
  • ES, Checksum,
  • MD5sum
  • AWS
  • Azure
  • GCP
  • DigitalOcean
  • Kotlin
  • Python
  • Tensorflow
  • Computer Vision
  • ECG
  • SPO2
  • Heart Rate
  • Glucometer
  • Blood Pressure
  • UX UI Process
  • Figma and FigJam
  • Adobe Suite
  • Selenium Java
  • Postman
  • Swagger
  • Jmeter
  • SQL
  • Java Scripter
  • Test ng
  • Extents Reports
  • Flutter
  • Java
  • Kotlin
  • Swift
  • Dart
  • React JS
  • Python
  • NodeJS
  • Django
  • HTML, CSS, JS
RDBMS
  • PostgreSQL
  • Oracle
  • MySQL
  • MariaDB
No SQL Based
  • MongoDB
  • GCP
  • FirestoreDB
  • DynamoDB
  • Azure
  • CosmosDB
  • AWS