You can get all the links using the tag "a" because all links are build using a tag.
var hrefs = await page.$$eval('a', as => as.map(a => a.href));
Puppeteer and protractor are not related, Puppeteer and selenium are not related. The only common thing between puppeteer and protractor is NodeJS.
Both Puppeteer and protractor are built on NodeJS platform.
Yes, Nodejs is required for the puppeteer to work. Puppeteer is built on NodeJS framework, so most of the puppeteer functions use the Nodejs functions internally, without nodeJS Puppeteer will not work.
If you are from the selenium background, then let's put this as "without JDK java will not work".
NodeJS is similar to JDK.
You can use the click method of the puppeteer to select a checkbox or a radio button. But using just click checks the already checked checkbox.
So we have to check whether a check box is checked, if a check box is not checked then click the element otherwise ignore it. Please note not sign(!) in the if block
const element = await page.$("locator");
const isCheckBoxChecked = await (await element.getProperty("checked")).jsonValue();
if(! isCheckBoxChecked ){
await page.click("css locator");
}
You can get the attribute value of an element using getProperty() method from Puppeteer.
const element = await page.$("locator");
const isCheckBoxChecked = await (await element.getProperty("checked")).jsonValue();
waitFor() makes the puppeteer sleep for a given amount of time, the time is taken in milliseconds.
await page.waitFor(2000);
Puppeteer takes the CSS selector as a selector by default.
You can use the $$mthod to find more than one element using the puppeteer.
await page.$$('locator');
It is just a variable, you can name it as anything but as a standard practice, we call it a page. Page is browser tab in the puppeteer perspective. Without the page, you cannot open the browser in puppeteer. When you close the last tab manually it closes the browser in puppeteer just like a manual thing.
WebElement Operations in Puppeteer
The default in puppeteer timeout is 30 seconds
You can answer based on your project framework, Typically page object model, Cucumber in combination with the page object model.
Jasmine is a unit testing framework built on Nodejs, majorly used with the protractor. As Puppeteer is also built on Nodejs, you can use the jasmine framework with the puppeteer.
By default, the puppeteer converts every locator to a CSS locator, so it is difficult to use XPath, moreover, the puppeteer does not give any function to use the Xpath. In case if you wanted to use XPath then you should use the page.exec() method.
await page.evaluate(() => {
$x("xpath here").operation
});
If you want to install puppeteer along with chromium-browser then you can use the below command.
npm install puppeteer
or
npm i puppeteer
If you just want to install the puppeteer but do not want chromium to be installed then use the below command.
npm install puppeteer
or
npm i puppeteer
You cannot connect to the already existing browser or tab. The browser object in puppeteer gets destroyed after every session.
Chromium is an engine for a browser, a simple UI built around the engine is a chromium engine. for every application, we receive .exe file similar to that we can also have nodejs package. Puppeteer is nothing but a NodeJS package of chromium engine.
Chrome and MS Edge are built around the chromium engine.
It is possible to work with firefox using the puppeteer.
Install firefox and puppeteer support
npm install puppeteer-firefox​
The remaining code is normal.
const puppeteerFirefox = require('puppeteer-firefox');
(async () => {
const browser = await puppeteerFirefox.launch({headless:false});
const page = await browser.newPage();
await page.goto('https://google.com');
await page.screenshot({path: 'chercher-tech.png'});
await browser.close();
})();
Puppeteer support Chromium, and in 2020 Microsoft announced MS edge is rebuild on chromium. Puppeteer supports MS Edge very well.
Running puppeteer code similar to running any Nodejs file.
node filename.js
I am Pavankumar, Having 8.5 years of experience currently working in Video/Live Analytics project.