|
| 1 | +const { test, expect } = require('@playwright/test'); |
| 2 | + |
| 3 | +test('Verify New Window', async ({ browser }) => { |
| 4 | + // Create a new browser context and page |
| 5 | + const context = await browser.newContext(); |
| 6 | + const page = await context.newPage(); |
| 7 | + |
| 8 | + // Navigate to the "New Browser Window" page |
| 9 | + const targetUrl = 'https://qa-practice.netlify.app/window'; |
| 10 | + await page.goto(targetUrl); |
| 11 | + |
| 12 | + // Define selectors and expected values |
| 13 | + const newWindowButtonSelector = '#newWindowBtn'; |
| 14 | + const expectedWindowUrl = 'https://qa-practice.netlify.app/web-table.html'; |
| 15 | + const expectedWindowTitle = 'QA Practice | Learn with RV'; |
| 16 | + const newWindowHeadingSelector = 'h2'; |
| 17 | + const expectedHeadingText = 'Table Example'; |
| 18 | + |
| 19 | + // Locate the button to open a new window |
| 20 | + const newWindowButton = page.locator(newWindowButtonSelector); |
| 21 | + |
| 22 | + // Listen for the new window (page) and click the button |
| 23 | + const [newWindow] = await Promise.all([ |
| 24 | + context.waitForEvent('page'), |
| 25 | + newWindowButton.click(), |
| 26 | + ]); |
| 27 | + |
| 28 | + // Wait for the new window to load |
| 29 | + await newWindow.waitForLoadState(); |
| 30 | + |
| 31 | + // Validate the URL of the new window |
| 32 | + expect(newWindow.url()).toBe(expectedWindowUrl); |
| 33 | + |
| 34 | + // Validate the title of the new window |
| 35 | + const newWindowTitle = await newWindow.title(); |
| 36 | + expect(newWindowTitle).toBe(expectedWindowTitle); |
| 37 | + |
| 38 | + // Validate specific content in the new window |
| 39 | + const newWindowHeading = await newWindow.locator(newWindowHeadingSelector).textContent(); |
| 40 | + expect(newWindowHeading).toBe(expectedHeadingText); |
| 41 | + |
| 42 | + // Close the new window and the main page |
| 43 | + await newWindow.close(); |
| 44 | + await page.close(); |
| 45 | +}); |
0 commit comments