I'm using Puppeteer to run a test within a website. Once a button has been clicked, a new popup browser window appears with a report. Within that new window is information I'd like to extract. How can I get puppeteer to shift it's focus to this newly created browser window?
I have multiple print buttons on my puppeteer page clicking on which creates a popup, I am trying to get a pdf of those pages. Here is the code I am using.
let printButtons = await this.page.$$('#printNowButton');
for (var i = 0; i < printButtons.length; i++) {
printButtons[i].click();
const fileName = ('Document'+ i + '.pdf');
browser.on('targetcreated', async target => {
if ((target.url() !== 'about:blank')) {
try {
const pageList = await browser.pages();
const newPage = await pageList[pageList.length - 1];
await newPage.pdf({
path: fileName,
format: 'A4'
}).then(() => {
newPage.close();
});
} catch (e) {
console.log('Error: ' + e);
}
}
});
}
I am using puppeteer version 0.13.0 Any other way to obtain what I am trying to achieve is also appreciated.
In the html of the popup body, add to the body tag onload="this. window. focus()". Even if you minimize the window, it pops back up front and center.
Browser alerts can be handled via Puppeteer. After an alert has been displayed on the website, automation tools like Selenium and WebdriverIO accept or reject it. However, before an alert shows on the page in Puppeteer, the user must specify whether to accept or dismiss it.
The syntax to open a popup is: window. open(url, name, params) : url. An URL to load into the new window.
There are two scenarios in you case First,If you are using version less than v0.13.0 you won't be able to do it Second if you are using node version 6 you have to build node components manually
cd node_modules/puppeteer
npm run build
I've solved a similar use case where I'm testing a page that opens a new window via window.open, and I need to find it after to run some tests. Adding a listener for "targetcreated" works, as long as you register the listener before opening the new window.
This is my test runner which does the following:
Adds a listener for the "targetcreated" event, in which it:
Finds the newly opened page object
Runs some tests on the opened page.
Finally, a call to "openSearchWindow" which actually triggers the window.open.
Here is the test runner. I've replaced my own code with comments to make it easier for you to use.
await browser.on('targetcreated', async () => {
const foundPage = await changePage(theNewURLOfThePopUpWindow);
if(foundPage) {
//Run some basic tests on the new page
const hasLoaded = await runSomeTestsHere();
expect(hasLoaded).to.be.true;
done();
} else {
done('Failed to find new page.');
}
});
//Opens the new popup window by clicking a button
await openSearchWindow(page);
changePage is a simple function that loops through all open browser pages and saves the page object for the page with a matching url. After running this you can use the new page for your tests, if it is opened.
async function changePage(url) {
let pages = await browser.pages();
let foundPage = false;
for(let i = 0; i < pages.length; i += 1) {
if(pages[i].url() === url) {
foundPage = true;
module.exports.page = pages[i];//set the new working page as the popup
break;
}
}
return foundPage;
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With