Puppeteer – Xpath Axes

Puppeteer – Xpath Axes ”; Previous Next To determine an element uniquely, we can either take the help of any of the attributes within the html tag or we can use a combination of attributes on the html tag. Mostly the id attribute is used since it is unique to a page. However, if the id attribute is not present, we can use other attributes like the class, name, and so on. In case the attributes like id, name, and class are not present, we can utilise a distinct attribute available to only that tag or a combination of attributes and their values to identify an element. For this, we have to use the xpath expression. Also, if the element on a page is dynamic, then xpath selector can be a good choice as a selector. The xpath is bi-directional which means we can traverse from the parent to the child element and also from the child to the parent element. The details of xpath axes shall be available in the below link − https://www.tutorialspoint.com/xpath/xpath_axes.htm In the below image, let us identify the highlighted edit box and obtain the value of its class attribute – gsc-input. In the above example, there are two columns (represented by td tags) in the table having the tr tag as their parent. The input box is present in the first column. So the xpath expression shall be as follows − //table/tbody/tr/child::td. Here, we are working with the xpath selector, so we have to use the method: page.$x(xpath value). The details on this method are discussed in the Chapter of Puppeteer Locators. To begin, follow Steps 1 to 2 from the Chapter of Basic Test on Puppeteer which are as follows − Step 1 − Create a new file within the directory where the node_modules folder is created (location where the Puppeteer and Puppeteer core have been installed). The details on Puppeteer installation is discussed in the Chapter of Puppeteer Installation. Right-click on the folder where the node_modules folder is created, then click on the New file button. Step 2 − Enter a filename, say testcase1.js. Step 3 − Add the below code within the testcase1.js file created. //Puppeteer library const pt= require(”puppeteer”) async function selectorAxesXpath(){ //launch browser in headless mode const browser = await pt.launch() //browser new page const page = await browser.newPage() //launch URL await page.goto(”https://www.tutorialspoint.com/index.htm”) //identify element with xpath axes const n = (await page.$x(“//table/tbody/tr/child::td”))[0] // get value of class attribute let v = await page.$eval(“input”, n => n.getAttribute(“class”)) console.log(v) } selectorAxesXpath() Step 4 − Execute the code with the command given below − node <filename> So in our example, we shall run the following command − node testcase1.js After the command has been successfully executed, the value of the class attribute for the element – gsc-input gets printed in the console. Print Page Previous Next Advertisements ”;

Puppeteer – Xpath Functions

Puppeteer – Xpath Functions ”; Previous Next To determine an element uniquely, we can either take the help of any of the attributes within the html tag or we can use a combination of attributes on the html tag. Mostly the id attribute is used since it is unique to a page. However, if the id attribute is not present, we can use other attributes like the class, name, and so on. In case the attributes like id, name, and class are not present, we can utilise a distinct attribute available to only that tag or a combination of attributes and their values to identify an element. For this, we have to use the xpath expression. If there are duplicate attributes or no attribute for an element, then the function text() is used to identify an element. In order to use the text() function, it is mandatory that the element should have a text visible on the page. Syntax The syntax for the use of text() function is as follows − //tagname[text()=”visible text on element”] If the value of an element or the text is partially dynamic in nature or very lengthy, we can use the contains() function. In order to use the contains() function, it is mandatory that the element should either have an attribute value or a text. Syntax The syntax for the use of contains() function is as follows − //tagname[contains(@attribute,”value”)] //tagname[contains(text(),”visible text on element”)] If the text of an element begins with a particular text, we can use the starts-with() function to it. Syntax The syntax for the use of starts-with() function is as follows − //tagname[starts-with(text(),”visible text on element”) In all the above functions, tagname is optional. Instead of tagname, we can use the symbol *. In the below image, let us identify the element – Library with the help of its displayed text and then click on it. The xpath for the element shall be //*[text()=”Library”]. Here, we are working with the xpath selector, so we have to use the method: page.$x(xpath value). The detail on this method is discussed in the Chapter – Puppeteer Locators. To begin, follow Steps 1 to 2 from the Chapter of Basic Test on Puppeteer which are as follows − Step 1 − Create a new file within the directory where the node_modules folder is created (location where the Puppeteer and Puppeteer core have been installed). The details on Puppeteer installation is discussed in the Chapter of Puppeteer Installation. Right-click on the folder where the node_modules folder is created, then click on the New file button. Step 2 − Enter a filename, say testcase1.js. Step 3 − Add the below code within the testcase1.js file created. //Puppeteer library const pt= require(”puppeteer”) async function selectorFunTextXpath(){ //launch browser in headless mode const browser = await pt.launch() //browser new page const page = await browser.newPage() //launch URL await page.goto(”https://www.tutorialspoint.com/index.htm”) //identify element with xpath function – text() then click const b = (await page.$x(“//*[text()=”Library”]”))[0] b.click() //wait for sometime await page.waitForTimeout(4000) //obtain URL after click console.log(await page.url()) } selectorFunTextXpath() Step 4 − Execute the code with the command given below − node <filename> So in our example, we shall run the following command − node testcase1.js After the command has been successfully executed, the URL of the page navigated on clicking the element Library – https://www.tutorialspoint.com/tutorialslibrary.htm gets printed in the console. Print Page Previous Next Advertisements ”;

Puppeteer – Getting Element Text

Puppeteer – Getting Element Text ”; Previous Next We can get element text in Puppeteer. This is done with the help of the textContent property. This property of the element is passed as a parameter to the getProperty method. Syntax The syntax of getting element text is as follows − const n = await page.$(“#txt”) const t = await (await n.getProperty(”textContent”)).jsonValue() In the below image, let us obtain the text – About Tutorialspoint for the highlighted element − To begin, follow Steps 1 to 2 from the Chapter of Basic Test on Puppeteer which are as follows − Step 1 − Create a new file within the directory where the node_modules folder is created (location where the Puppeteer and Puppeteer core have been installed). The details on Puppeteer installation is discussed in the Chapter of Puppeteer Installation. Right-click on the folder where the node_modules folder is created, then click on the New file button. Step 2 − Enter a filename, say testcase1.js. Step 3 − Add the below code within the testcase1.js file created. //Puppeteer library const pt= require(”puppeteer”) async function getText(){ //launch browser in headless mode const browser = await pt.launch() //browser new page const page = await browser.newPage() //launch URL await page.goto(”https://www.tutorialspoint.com/about/about_careers.htm”) //identify element const f = await page.$(“[class=”heading”]”) //obtain text const text = await (await f.getProperty(”textContent”)).jsonValue() console.log(“Text is: ” + text) } getText() Step 4 − Execute the code with the command given below − node <filename> So in our example, we shall run the following command − node testcase1.js After the command has been successfully executed, the text of the element – About Tutorialspoint gets printed in the console. Print Page Previous Next Advertisements ”;

Puppeteer – Chrome

Puppeteer – Chrome ”; Previous Next The tests written in Puppeteer are executed in the Chrome or Chromium browser in a headless mode by default. Also, we have to add the below Puppeteer library in the code. const pt = require(”puppeteer”) To begin, follow Steps 1 to 2 from the Chapter of Basic Test on Puppeteer which are as follows − Step 1 − Create a new file within the directory where the node_modules folder is created (location where the Puppeteer and Puppeteer core have been installed). The details on Puppeteer installation is discussed in the Chapter of Puppeteer Installation. Right-click on the folder where the node_modules folder is created, then click on the New file button. Step 2 − Enter a filename, say testcase1.js. Step 3 − Add the below code within the testcase1.js file created. //adding Puppeteer library const pt = require(”puppeteer”); pt.launch().then(async browser => { //browser new page const p = await browser.newPage(); //set viewpoint of browser page await p.setViewport({ width: 1000, height: 500 }) //launch URL await p.goto(”https://www.tutorialspoint.com/index.htm”) //get browser const v = await p.browser().version(); console.log(“Browser: ” + v) //browser close await browser.close() }) Step 4 − Execute the code with the command given below − node <filename> So in our example, we shall run the following command − node testcase1.js After the command has been successfully executed, the browser in which the test is executed – HeadlessChrome/92.0.4512.0 gets printed in the console. Print Page Previous Next Advertisements ”;

Comparison Between Puppeteer & Protractor

Comparison Between Puppeteer & Protractor ”; Previous Next The major differences between Puppeteer and Protractor are given below − Sr.No. Puppeteer Protractor 1 Puppeteer is a module in node developed for Chromium engine. Protractor is a dedicated test automation tool. 2 Puppeteer is faster in executing tests than Protractor. Protractor is slower in executing tests than Puppeteer. 3 Puppeteer is developed mainly for Chromium so the tests developed are mainly executed in Chrome. Protractor can be used to execute tests on multiple browsers like Chrome, Firefox, IE, Safari, and so on. 4 Puppeteer can be used for API testing by utilising the requests and the responses. API testing with Protractor is difficult. 5 Puppeteer can be used to verify the count of CSS and JavaScript files utilised for loading a webpage. Protractor cannot be used to verify the count of CSS and JavaScript files utilised for loading a webpage. 6 Puppeteer can be used to work on the majority of features in the DevTools in the Chrome browser. Protractor cannot be used to work on the majority of features in the DevTools in the Chrome browser. 7 Puppeteer can be used to execute tests on various devices with the help of the emulators. Using an emulator with Protractor is not easy. 8 Puppeteer can be used to save a screenshot in both image and PDF formats. Protractor can be used to save a screenshot in only image format. 9 Puppeteer can be used to obtain the time needed for a page to load. Protractor cannot be used to obtain the time needed for a page to load. 10 In Puppeteer, we can verify an application without image loading. In Protractor, we can verify an application without image loading. Let us observe the npm trends of Puppeteer and Protractor for the last two years. We shall observe an upward trend towards the use of Puppeteer than Protractor (available from the below link) − https://www.npmtrends.com/protractor-vs-puppeteer Print Page Previous Next Advertisements ”;

Puppeteer – Handling Drop-downs

Puppeteer – Handling Drop-downs ”; Previous Next We can handle drop downs in the UI while automating a test using Puppeteer. The static drop downs are identified in the html code with the tagname as select and its options have the tagname as option. Methods to Handle Dropdown Some methods to work with static dropdowns − select() This method is used to select an option from the dropdown. The value of the option to be selected is passed as a parameter to this method. Syntax The syntax is as follows − const page = await browser.newPage() const f = await page.$(”[name=”selType”]”) await f.select(“subject”) We can also select multiple options from a multi-select dropdown. Syntax The syntax is as follows − await f.select(“subject”, “name”) To obtain select value from the dropdown, we have to use the getProperty method and pass value as a parameter to this field. const v = await (await n.getProperty(“value”)).jsonValue() console.log(v) type() This method is used to select an option from the dropdown. The value of the option to be selected is passed as a parameter to this method. Syntax The syntax is as follows − const page = await browser.newPage() const f = await page.$(”[name=”selType”]”) await f.type(“subject”) To begin, follow Steps 1 to 2 from the Chapter of Basic Test on Puppeteer which are as follows − Step 1 − Create a new file within the directory where the node_modules folder is created (location where the Puppeteer and Puppeteer core have been installed). The details on Puppeteer installation is discussed in the Chapter of Puppeteer Installation. Right-click on the folder where the node_modules folder is created, then click on the New file button. Step 2 − Enter a filename, say testcase1.js. Step 3 − Add the below code within the testcase1.js file created. //Puppeteer library const pt= require(”puppeteer”) async function dropDownHandle(){ //launch browser in headless mode const browser = await pt.launch() //browser new page const page = await browser.newPage() //launch URL await page.goto(”https://www.tutorialspoint.com/tutor_connect/index.php”) //identify dropdown then select an option by value const f = await page.$(”[name=”selType”]”) await f.select(“subject”) //wait for sometime await page.waitForTimeout(4000) //get value selected const v = await (await f.getProperty(“value”)).jsonValue() console.log(v) } dropDownHandle() Step 4 − Execute the code with the command given below − node <filename> So in our example, we shall run the following command − node testcase1.j After the command has been executed successfully, the value of the option selected in the dropdown – subject is printed in the console. Print Page Previous Next Advertisements ”;

Puppeteer VS Code Configuration

Puppeteer – VS Code Configuration ”; Previous Next The steps to install the Visual Studio (VS) Code are listed below − Step 1 − Navigate to the below link − https://code.visualstudio.com/ Step 2 − Depending on the local operating system we have for example – macOS, Linux or Windows, we need to choose the link for download. Step 3 − A zip file gets downloaded after clicking the Download button. After downloading this file has completed, click on it and the Visual Studio Code application should become available for use. Step 4 − Double-click it and the Visual Studio Code application should launch along with the welcome page. Print Page Previous Next Advertisements ”;

Comparison Between Puppeteer & Selenium

Comparison Between Puppeteer & Selenium ”; Previous Next The major differences between Puppeteer and Selenium are given below − Sr.No Puppeteer Selenium 1 Puppeteer is developed mainly for Chromium so the tests developed are mainly executed in Chrome. Selenium can be used to execute tests on multiple browsers like Chrome, Firefox, IE, Safari, and so on. 2 Puppeteer code can be implemented only in JavaScript. Selenium code can be implemented on multiple languages like Java, Python, JavaScript, C#, and so on. 3 Puppeteer provides APIs to manage headless execution in Chrome by using the DevTools protocol. Selenium requires additional external browser drivers that trigger tests as per the user commands. 4 Puppeteer manages the Chrome browser. Selenium is primarily used to execute tests to automate the actions performed on the browser. 5 Puppeteer is faster in executing tests than Selenium. Selenium is slower in executing tests than Puppeteer. 6 Puppeteer is a module in node developed for Chromium engine. Selenium is a dedicated test automation tool. 7 Puppeteer can be used for API testing by utilising the requests and the responses. API testing with Selenium is difficult. 8 Puppeteer can be used to verify the count of CSS and JavaScript files utilised for loading a webpage. Selenium cannot be used to verify the count of CSS and JavaScript files utilised for loading a webpage. 9 Puppeteer can be used to work on the majority of features in the DevTools in the Chrome browser. Selenium cannot be used to work on the majority of features in the DevTools in the Chrome browser. 10 Puppeteer can be used to execute tests on various devices with the help of the emulators. Using an emulator with Selenium is not easy. 11 Puppeteer can be used to obtain the time needed for a page to load. Selenium cannot be used to obtain the time needed for a page to load. 12 Puppeteer can be used to save a screenshot in both image and PDF formats. Selenium can be used to save a screenshot in both image and PDF formats only in the Selenium 4 version. 13 Puppeteer was first introduced in the year 2017. Selenium was first introduced in the year 2004. 14 In Puppeteer, we can verify an application without image loading. In Selenium, we can verify an application without image loading. Print Page Previous Next Advertisements ”;

Puppeteer – Handling Tabs

Puppeteer – Handling Tabs ”; Previous Next We can handle tabs in Puppeteer using the below methods − newPage() We can open a new tab using this method available in the browser object. Syntax The syntax is as follows − const p = await browser.newPage() close() We can close the tab opened using this method. Syntax The syntax is as follows − await p.close() close() We can close all the tabs opened using this method available in the browser object. Syntax The syntax is as follows − await browser.close() To begin, follow Steps 1 to 2 from the Chapter of Basic Test on Puppeteer which are as follows − Step 1 − Create a new file within the directory where the node_modules folder is created (location where the Puppeteer and Puppeteer core have been installed). The details on Puppeteer installation is discussed in the Chapter of Puppeteer Installation. Right-click on the folder where the node_modules folder is created, then click on the New file button. Step 2 − Enter a filename, say testcase1.js. Step 3 − Add the below code within the testcase1.js file created. //adding Puppeteer library const pt = require(”puppeteer”) pt.launch().then(async browser => { //browser new page const p = await browser.newPage(); //set viewpoint of browser page await p.setViewport({ width: 1000, height: 500 }) //launch URL await p.goto(”https://www.tutorialspoint.com/index.htm”) //capture screenshot await p.screenshot({ path: ”tutorialspoint.png” }); //browser close await browser.close() }) Step 4 − Execute the code with the command given below − node <filename> So in our example, we shall run the following command − node testcase1.js After the command has been successfully executed, a new file called the tutorialspoint.png gets created within the page directory. It contains the captured screenshot of the page launched in the browser. Print Page Previous Next Advertisements ”;

Puppeteer – Basic Commands

Puppeteer – Basic Commands ”; Previous Next Some of the basic commands of Puppeteer are listed below − title() This command is used to obtain the title of the present page. Syntax The syntax is as follows − await page.title() url() This command is used to obtain the URL of the application currently launched in the browser. Syntax The syntax is as follows − await page.url() content() This command is used to obtain the page source code. Syntax The syntax is as follows − await page.content() To begin, follow Steps 1 to 2 from the Chapter of Basic Test on Puppeteer which are as follows − Step 1 − Create a new file within the directory where the node_modules folder is created (location where the Puppeteer and Puppeteer core have been installed). The details on Puppeteer installation is discussed in the Chapter of Puppeteer Installation. Right-click on the folder where the node_modules folder is created, then click on the New file button. Step 2 − Enter a filename, say testcase1.js. Step 3 − Add the below code within the testcase1.js file created. //adding Puppeteer library const pt = require(”puppeteer”); pt.launch().then(async browser => { //browser new page const p = await browser.newPage(); //set viewpoint of browser page await p.setViewport({ width: 1000, height: 500 }) //launch URL await p.goto(”https://www.tutorialspoint.com/questions/index.php”) //obtain page title console.log(“Page title: ” + await p.title()) //obtain URL console.log(“Url: ” + await p.url()) //browser close await browser.close() }) Step 4 − Execute the code with the command given below − node <filename> So in our example, we shall run the following command − node testcase1.js After the command has been successfully executed, the page title – The Best Technical Questions and Answers gets printed in the console. Also, the URL – www.tutorialspoint.com/questions/index.php gets printed in the console. The execution has happened in the headless mode. Print Page Previous Next Advertisements ”;