Search code examples
protractorjasmine-node

In Protractor using Javascript, can we use try/catch block for writing Test Validations?


Through many blogs, i found that error handling can be handled through promises in protractor.In page object model the promises can be implemented in page file. would like to know , in Spec file where we write the Test validations code, will try/catch work in js file?

can some one please share the sample try/catch code in spec file of protractor.? sample code:

it("try catch",function() {
  try {
    // calling page object methods
    homepage.clickingPosseLink()
    homepage.entercredintal(browser.params.username, browser.params.password);
    var number=homepage.textvalidation();
    //sometimes number will be blank from UI---- to handle this scenario i need try catch code
    if(number>200){
      //functionality code
    }
  }catch(err){
    console.log('Number retrieved from UI is blank');
  }
})

if number is blank,it should throw a exception message.But no action is happening because it is written in if loop. expected is if any exception occur, it should catch and print the user defined message. Mentioned above is sample code. Main part is if exceptions occurs how to handle in it block using try catch block? Please suggest if the above approach is correct?If any other way can be implemented, please pinch in your valuable comments.


Solution

  • You even don't need to use try-catch.

    I assume that textvalidation returns number type.

    it("try catch",function(){
        homepage.clickingPosseLink();
        homepage.entercredintal(browser.params.username,browser.params.password);
        const number = homepage.textvalidation();
        const maxNumber = 200
        if(number > maxNumber){
            //some code
        } else if (number <= maxNumber){
            //some code
        } else {
           console.log(`Number retrieved from UI is "${number}"`);
        }
    }