Just like Selenium webdriver provides various Exception handling for Java, is there any way we can achieve same using Protractor.
If we want to handle element not found exception, then what is the best way to do it using Protractor?
Just like Selenium webdriver provides various Exception handling for Java, is there any way we can achieve same using Protractor.
If we want to handle element not found exception, then what is the best way to do it using Protractor?
Share Improve this question edited Aug 1, 2014 at 16:41 Artjom B. 61.9k25 gold badges134 silver badges229 bronze badges asked Mar 21, 2014 at 4:43 mohitmohit 7694 gold badges9 silver badges16 bronze badges 1- Hmm... Could you edit your question to add code and give us more details please? – glepretre Commented Mar 21, 2014 at 7:58
3 Answers
Reset to default 17Answer to this question is now in Protractor's FAQ
How can I catch errors such as ElementNotFound?
WebDriver throws errors when commands cannot be completed - e.g. not being able to click on an element which is obscured by another element. If you need to retry these actions, try using webdriverjs-retry. If you would just like to catch the error, do so like this
Adapted to your question:
elm.isPresent().then(function(present) {
/* no webdriver js errors here */}
if (present) {
/* element exists */
} else {
/* element doesn't exist */
}
, function(err) {
/* error handling here, i.e. element doesn't if got ElementNotFound
but, eventually and less likely, other issues will fall in here too like
NoSuchWindowsError or ElementStaleError etc...
*/
});
Kudos to @Leo Gallucci for his adaptation to the OP's question:
I faced this issue today and was hoping to go for a clean-looking solution like this :
/* Function to check for three possible DOM elements; return the element which exists, and get the text contents. */
this.getMySelector = function(){
if (element(by.css('.mySelector')).isPresent()){
return element(by.css('.mySelector'));
}
else if (element(by.css('.mySelector2')).isPresent()){
return element(by.css('.mySelector2'));
}
else{
return element(by.css('.mySelector3'));
}
}
however, it was always hitting the first if()
and never checking other conditions. Turns out I needed to chain the promises for my scenario:
this.getMySelector = function(){
element(by.css('.mySelector')).isPresent().then(function (pres) {
if (pres){
defer.fulfill( by.css('.mySelector')).getText() );
}
else{
element(by.css('.mySelector2')).isPresent().then(function (pres) {
if (pres){
defer.fulfill(..);
}
}
}
}
}
// From calling test-spec.js file
getMySelector.then(function(text)){
console.log('Now I got the text ==> ' + text);
}
Try, Catch has the following syntax in Protractor. The below code will first find an element by Id 'IdTextBoxCode'. Then the code to enter code 'codeTextBox.sendKeys(code);' is in TRY block. If the code throws exception(in this case, if the element with Id 'IdTextBoxCode' is not found), then it will go to the catch block and the error handling function.
browser.driver.findElement(by.id(browser.params.loginPage.IdTextBoxCode)).then(function(codeTextBox)
{
try
{
console.log("Entering Code: "+code);
codeTextBox.sendKeys(code);
}
catch(err) {
console.log('In catch block');
}
}, function(err) {
console.info('Code Text Box not displayed on page. Proceeding with default Code');
});