-
Notifications
You must be signed in to change notification settings - Fork 11
/
multi-tab.js
38 lines (31 loc) · 1.2 KB
/
multi-tab.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
async function testPage(page) {
// Set the screen resolution, as things render differently on different screen widths
await page.setViewport({
width: 1200,
height: 800,
})
await page.goto('https://digital-strategy.ec.europa.eu/en/policies/plan-ai');
await page.screenshot({ path: 'firstTab.png' });
// Use XPath to select the link based on its text content
const LINK_TEXT = "plan’s latest update was published in 2021";
const linkXPath = `//a[text()="${LINK_TEXT}"]`;
await page.waitForXPath(linkXPath);
const [linkElement] = await page.$x(linkXPath);
if (linkElement) {
await linkElement.click();
console.log('Link clicked successfully!');
} else {
console.error('Link not found');
}
// Wait for the new tab to open and switch to it
await page.waitForTimeout(2500);
// Get all pages within the same browser context
const pages = await page.browserContext().pages();
// The newly opened page should be the last one in the array
const newPage = pages[pages.length - 1];
await newPage.waitForTimeout(2500);
await newPage.screenshot({ path: 'newTab.png' });
// Log the title of the new tab
console.log(await newPage.title());
}
module.exports = testPage;