Debugging Protractor Tests for Selenium Test Automation.

Debugging is a process to make sure that your tests run in the decided manner. So let us see our ways to help you debug Protractor tests.

End to end testing is essential to applications to ensure their quality. Addressing bugs and issues is ensured by this. Step by Step debug is the way to solve any bug which comes through when testing any application. Selenium automation test is essential to check if a test run goes on as expected.

Here, we will tell you about how to debug protractor tests for selenium test automation. However, before we dive into debugging, let us see some stuff related to Debugging.

Problems during Debugging Protractor Tests

Bugs come during testing any application. This might be due to any module problem or some compatibility issues. The problems or bugs which comes during debugging can be:

  • Testing is tricky because it is dependent on the entire system
  • Different OS and browsers for cross-platform testing require Different WebDrivers.
  • Selenium test scenarios have a series of sequences connected which depends on the previous outcome and hence a dependency.
  • Sometimes the error messages are very lengthy hence tough to understand.
  • Distinguishing between compatibility issues and application issues is a tough nut to crack.

Types of Failure to debug in protractor tests

While testing, there are some major types of failures to be encountered. These failure scenarios are :

  • Expectation Failure
  • WebDriver Failure
  • WebDriver Unexpected Failure
  • Protractor Angular Failure
  • Protractor Timeout Failure

Expectation Failure
Failure in normal flow execution is one of the most common test failures. Expected failure is a result of this.

WebDriver Failure
When a browser requests an element, and it is missing, it causes WebDriver Failure error. This means that there is a failure in executing the requested command.

WebDriver Unexpected Failure
In a situation when the web driver update fails, the browser or OS crashes. This situation is an example of WebDriver Unexpected Failure.

Protractor Angular Failure
When the Protractor Framework is not able to find the required Angular libraries in the module coins up Protractor Angular Failure.

Protractor Timeout Failure
In a situation where the test suite is stuck in a time loop for an extended period,the data is not returned; this calls out for a Protractor Timeout Failure.

Debugging Protractor Tests in Selenium 

Protractor can expand the functionality of node debugger used by most of the node js apps. This gives the ability to add additional statements required in debugging mode from the terminal during debugging.

There are multiple debug Protractor tests. These are:

  • Pause Method
  • Debugger Method
  • Screen Shot Method

We will explain these methods now-

Pause Method to Debug Tests

Pause method is the easiest and the most popular ways to debug Protractor tests. This test is executed by adding browser—pause () method at the position we want to pause the test and check errors.

The script for this method is executed using-

$ protractor test_config.js

When this code is used and executed, whenever we press the pause key, we see intervals of runtime.

By this method, we can go to check particular intervals of a code. If a particular interval is not executed, then we can find the error in the particular interval. We don’t need to see the entire code for that error. Hence this method is straightforward yet efficient.

Code-
// test_debug.js //

// describing our test scenario for protractor framework //

describe(‘ Sample Protractor Test Suite for debugging example ‘,function(){

// information about the test case

  it(‘An Example to perform Debug during Testing’,function(){

// launching the url in browser //

     browser.get(“http://www.google.com”);

     browser.pause();

     element(by.model(‘testName’)).sendKeys(‘Test Automation’);

        // It performs the check whether the element exist or not //

        var myText = element(by.binding(‘myTestString’)).getText();

        expect(‘Welcome to ‘+myText+’!’).toEqual(‘Welcome to Test Automation!’)

  });

});

Screenshot Method
Screenshots. This method may sound weird, but yes, it still exists as a method. It is a rather exciting way for debugging. Selenium WebDriver can be programmed to take a screenshot using browser.takeScreenshot(). This is a rather standard method on servers which are used continuously to run tests. 

This test results in getting a screenshot in PNG format with base64 encoding.

Code-
// test_debug.js //

// the variable declared at the beginning of the test script:

var myscript = require(‘fs’);

// function that defines how to write screenshot to a file

function writeScreenShot(data, filename) {

   var mystream = myscript.createWriteStream(filename);

   mystream.write(new Buffer(data, ‘base64’));

   mystream.end();

}

// describing our test scenario for protractor framework //

describe(‘ Sample Protractor Test Suite for debugging example ‘,function(){

// information about the test case

  it(‘An Example to perform Debug during Testing’,function(){

// launching the url in browser //

   browser.get(“http://www.google.com”);

   browser.takeScreenshot().then(function (png) {

   writeScreenShot(png, ‘exception.png’);

});

     element(by.model(‘testName’)).sendKeys(‘Test Automation’);

        // It performs the check whether the element exist or not //

        var myText = element(by.binding(‘myTestString’)).getText();

26

        expect(‘Welcome to ‘+myText+’!’).toEqual(‘Welcome to Test Automation!’)

27

  });

28

});

29

Online Selenium Testing

These above methods can execute on an online server as well, which supports Selenium.

For this, we just need minimal changes in the code, and it works fine. LambdaTest hub is a perfect place for this thing.

Conclusion

We will conclude the Protractor testing guide here. These are the methods which we employ in order to test a Selenium Application under debugging. Not all the methods would be suitable for your work type and requirement. 

So try and check which method is suiting your line of work the most.

Share on facebook
Facebook
Share on twitter
Twitter
Share on linkedin
LinkedIn
Share on pinterest
Pinterest