top of page
  • Writer's picturePerfectQA

Debugging Protractor Tests for Selenium Test Automation.

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

End-to-end testing is essential for applications to ensure their quality. This guarantees bugs and problems. Step-by-step debugging is the way to solve any bug that comes up when testing any application. Selenium automation tests are essential to check if a test run goes as expected.

Here, we will tell you 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 up during the testing of any application. This might be due to a module problem or some compatibility issues. The problems or bugs that come up during debugging can be:

  • Testing is tricky because it is dependent on the entire system.

  • Different operating systems and browsers necessitate the use of different WebDrivers for cross-platform testing.

  • Selenium test scenarios consist of a linked series of sequences that are dependent on the previous outcome, thus the 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 during 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 a WebDriver Failure error. This means that there is a failure in executing the requested command.

WebDriver Unexpected Failure. In a situation where the web driver update fails, the browser or OS crashes. This is an instance of WebDriver's Unexpected Failure.

Protractor Angular Failure. When the Protractor Framework is not able to find the required Angular libraries in the module, it coins the error 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 for a Protractor Timeout Failure.




Debugging Protractor Tests in Selenium

Protractor can extend the functionality of the node debugger, which is used by the majority of node js apps.This allows you to add additional statements needed in debugging mode from the terminal while debugging.

There are multiple debug Protractor tests. These are:

  • Pause Method

  • Debugger Method

  • Screen Shot Method

We have explained these methods here,


Pause Method to Debug Tests


The pause method is the easiest and most popular way to debug Protractor tests. This test is run by inserting the browser—pause () method at the point where we want to pause the test and check for 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. We can use this method to check specific code intervals. If a particular interval is not executed, then we can find the error in that particular interval. We don’t need to see the entire code for that error. Hence, this method is straightforward yet efficient.


// 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 of debugging. Selenium WebDriver can be programmed to take a screenshot using the browser.takeScreenshot(). This is a rather standard method on servers that are used continuously to run tests.

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


// 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();
        expect(‘Welcome to ‘+myText+’!’).toEqual(‘Welcome to Test Automation!’)
  });
});


Online Selenium Testing


These above methods can be executed on an online server that supports Selenium as well.

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


// test_config.js //
// The test_config.js file servers as a configuration file for out test case //
LT_USERNAME = process.env.LT_USERNAME || "irohitgoyal"; // Lambda Test User name
LT_ACCESS_KEY = process.env.LT_ACCESS_KEY || "r9JhziRaOvd5T4KCJ9ac4fPXEVYlOTealBrADuhdkhbiqVGdBg"; // Lambda Test Access key
exports.capabilities = {
  'build': ' Automation Selenium Webdriver Test Script ', // Build Name to be display in the test logs
  'name': ' Protractor Selenium Debugging Test on Chrome',  // The name of the test to distinguish amongst test cases //
  'platform':'Windows 10', //  Name of the Operating System
  'browserName': 'chrome', // Name of the browser
  'version': '79.0', // browser version to be used
  'visual': false,  // flag to check whether to take step by step screenshot
  'network':false,  // flag to check whether to capture network logs
  'console':false, // flag to check whether to capture console logs.
  'tunnel': false // flag to check if it is required to run the localhost through the tunnel
  };
// setting required config parameters //
exports.config = {
   directConnect: true,
   // Desired Capabilities that are passed as an argument to the web driver instance.
   capabilities: {
      'browserName': 'chrome'  // name of the browser used to test //
   },
   // Flavour of the framework to be used for our test case //
   framework: 'jasmine',
   // The patterns which are relative to the current working directory when  
protractor methods are invoked //
   specs: ['test_debug.js'],
// overriding default value of allScriptsTimeout parameter //
      allScriptsTimeout: 999999,
      jasmineNodeOpts: {
// overriding default value of defaultTimeoutInterval parameter //
      defaultTimeoutInterval: 999999
   },
   onPrepare: function () {
      browser.manage().window().maximize();
      browser.manage().timeouts().implicitlyWait(5000);
   }
};

Conclusion:


We will conclude the Protractor testing guide here. These are the methods that we employ in order to test a Selenium application while it is being debugged. Not all the methods would be suitable for your work type and requirements.

So experiment to see which method best suits your line of work.


Have a Worthy Debugging!


14 views0 comments
bottom of page