Pages

How to check if an element is present or not in protractor

 isPresent() in protractor

>> In protractor, isPresent() method is used to check whether element is currently present or not in DOM.
>> isPresent() resolves to whether the element is in DOM or not and doesn't throw exception if it is visible or not on UI.
>> isPresent() will return boolean values (true/false).

>> If value is true then element is present in DOM and if value is false then element is not present in DOM.

Please check below code script:

First write xpath with help of DOM

isPresent() in protractor
isPresent()



Here we have two buttons, 1 button is present and other button is not present.

describe('Protractor - use isPresent()', function () {
it('Code script to how to use isPresent()', function () {
browser.get("https://keeplearners.blogspot.com/2018/03/Angular-elements.html");
var buttonPresent = element(by.css("input#displaying"));
var ispresent = isElementPresent(buttonPresent);
ispresent.then(function (value) {
console.log("Is First button present=" + value);
});

var buttonNotPresent = element(by.css("input#not-displaying"));
var isnotpresent = isElementPresent(buttonPresent);
isnotpresent.then(function (value) {
console.log("Is second button present=" + value);
});
});

function isElementPresent(element) {
return element.isPresent().then(function (present) {
console.log("is Element present=" + present);
return present;
});
}
});


Output:
It will return whether button(s) is present or not.
As both elements are present in DOM(able to match the finder) it will present as true for both elements.
isPresent() in protractor
isPresent()

Please comment below to feedback or ask questions.

No comments:

Post a Comment

Please comment below to feedback or ask questions.