Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Need help on try-catch

I am trying to use try-catch block in my protractor test, please see the code below:

try {
    element(by.id('usernameas')).sendKeys(data);
}
catch(err) {
    console.log('error occured');
} 

I am deliberately passing wrong locator to check whether it is going in catch block or not, currently it is giving me error NoSuchElementError on command prompt and test execution stops rather than going into catch block.

Please suggest.

like image 923
user3121600 Avatar asked Jan 02 '14 12:01

user3121600


People also ask

How do I get a try catch error?

JavaScript try and catch The try statement allows you to define a block of code to be tested for errors while it is being executed. The catch statement allows you to define a block of code to be executed, if an error occurs in the try block.

Can you put a try catch in a try catch?

Yes, we can declare a try-catch block within another try-catch block, this is called nested try-catch block.


2 Answers

The call to element(locator).sendKeys returns a promise which is either resolved or rejected. The promise is part of the test's control flow.

The call to element(locator) itself does not throw an error, it is the promise which is rejected. If you fail to find an element you actually want your entire test to fail, since the scneario cannot be completed.

To get the error message you can use the promise callbacks, as demonstrated below.

Important note: if you handle the promise failure by yourself your test won't fail, so you should better rethrow it

try {
    element(by.id('usernameas')).sendKeys(data).then(function() {
        console.log('keys sent successfully');
    }, function(err) {
        console.error('error sending keys ' + err);
        throw err;
    });
}
catch(err) {
    console.log('error occured');
}

The console output is (trimmed):

error sending keys NoSuchElementError: no such element
  (Session info: chrome=31.0.1650.63)
  (Driver info: chromedriver=2.8.241075,platform=Windows NT 6.1 S .....
like image 126
Eitan Peer Avatar answered Oct 20 '22 19:10

Eitan Peer


I ran into this problem recently and noticed that you DONT need the try/catch block. In Protractor, you can achieve the try/catch like following:

try {   <---------------------------- Traditional TRY/CATCH method
  loadWebApp();
  login();
  openUserPreferences();
  changePassword();
 } catch (err) {
  console.error(
      "An error was thrown! " + err);
 }

 loadWebApp().
    then(login).
    then(openUserPreferences).
    then(changePassword).
    then(null, function(err) {  <----------------- PROTRACTOR equivalent of try/catch
      console.error(
          "An error was thrown! " + err);
    });

Here's the source where I got this info from: https://code.google.com/p/selenium/wiki/WebDriverJs#Promises
under Value Propagation and Chaining

So again, you don't need to explicitly add a try/catch.

In short, the reason this method works is because a promise can either be RESOLVED or REJECTED and in case of a rejected or failed promise, this line [ then(null, function(err) { ... } ] will act as the CATCH block.

Also notice that the then(null, function(err))( is NOT taking any callback but only an errBack; so basically, this is saying we don't care about whether the promise gets resolved, we only care about whether it fails and thus the NULL for callback and the function(error) for the errBack.
No need to wrap this in a try/catch then throw the error as suggested above by the accepted answer (@Eitan Peer).

Hope this helps someone out there struggling with Protractor as I did.

like image 42
pelican Avatar answered Oct 20 '22 18:10

pelican