What can it do?
Finding an web element using XPATH is not a right choice to make as it slows down the element finding mechanism. But, due to the lack of CSS support for multiple use cases, still XPATH use to gather some space in your testing world.
Worry not! Here comes Protractor-CSS-Booster! 🐣
protractor-css-booster is lightweight plugin to help you to find out web elements by using special custom selectors. All the custom locator uses CSS selector as an input to accomplish the task for you.
Now, you can find web elements such as:
- Finding Grand Parent Element
- Finding Parent Element
- Finding Next Sibling
- Finding Previous Sibling
- Finding any Following Sibling
- Finding First Child Element
- Finding Last Child Element
And, guess what, everything you can find using 💥 CSS Selectors 💥
IE Support added from version 1.0.9
Installation
Install this module locally with the following command to be used as a (dev-)dependency:
npm install --save protractor-css-boosternpm install --save-dev protractor-css-booster
Usage
protractor-css-booster supports NodeJS 8 or higher
Configuration
protractor-css-booster can be used as a plugin in your protractor configuration file with the following code:
exports.config =;
Using CSS Booster
You can now have the flexibility to use protractor-css-booster in two ways - 1. using css selector 2. using prototype function (in this case you need to use await / resolve promise by "then") Refer below examples
Suppose your HTML
<div> <h2>Thor's Biodata</h2> <div id='borr'> <p id="odin"> <a id="thor"> baby thor </a> </p> </div> <div> <ul id="schoolBook"> <li id="name">Thor</li> <li id="age">Million Years</li> <li id="home">Asgard</li> </ul> </div> </div>
find GrandParent:
This Locator helps to identify the grand parent element of a target element
You can find the grand parent by:
; // returns <div id='borr'> <p id="odin"> <a id="thor"> baby thor </a></p></div>target.click; //proceed with your desired operation
Or you can use as by-locator style,
; // returns <div id='borr'> <p id="odin"> <a id="thor"> baby thor </a></p></div>
find Parent:
This Locator helps to identify the parent element of a target element
You can find the parent by:
; // returns <p id="odin"><a id="thor"> baby thor </a> </p>target.click; //proceed with your desired operation
Or you can use as by-locator style,
; // returns <p id="odin"><a id="thor"> baby thor </a> </p>target.click; //proceed with your desired operation
find Next Sibling:
The nextSiblingOf locator returns the node immediately following the specified node, in the same tree level.
You can find the next sibling by:
; // returns <li id="age">Million Years</li>target.click; //proceed with your desired operation
Or you can use as by-locator style,
; // returns <li id="age">Million Years</li>target.click; //proceed with your desired operation
find Previous Sibling:
The prevSiblingOf locator returns the previous element of the specified element, in the same tree level.
You can find the previous sibling by:
; // returns <li id="name">Thor</li>target.click; //proceed with your desired operation
Or you can use as by-locator style,
; // returns <li id="name">Thor</li>target.click; //proceed with your desired operation
find Following Sibling:
The followingSibling locator returns the following sibling of a given element. This should always be used with element chain as the second element finder.
It will help you to omit the xpath's //following-sibling:: dependency
You can find the following sibling by:
; // returns <li id="age">Million Years</li>target.click; //proceed with your desired operation
Or you can use as by-locator style,
; // returns <li id="age">Million Years</li>
find First Child:
The firstChildOf locator returns the first child element of the specified element.
You can find the first child by:
; // returns <li id="name">Thor</li>target.click; //proceed with your desired operation
Or you can use as by-locator style,
; // returns <li id="name">Thor</li>target.click; //proceed with your desired operation
find Last Child:
The lastChildOf locator returns the last child element of the specified element.
You can find the last child by:
; // returns <li id="home">Asgard</li>target.click; //proceed with your desired operation
Or you can use as by-locator style,
; // returns <li id="home">Asgard</li>target.click; //proceed with your desired operation
Sample Test Files:
Using prototype function sample test cases can be found here
using as "by-locator" sample test cases can be found here
Tell me your issues
you can raise any issue here
Contribution
Any pull request is welcome keeping proper code structure.
If works for you, kindly give a star. 🌟
- Copyright © 2019- Abhinaba Ghosh