How To Debug Protractor Tests for Selenium Test Automation?

Image for post
Image for post

End to end testing of web applications is pivotal to ensure it’s quality. This is why you need to make sure that all the issues and bugs are addressed. When you encounter issues while testing, the best approach is step by step debugging the code. Debugging can be a great way to ensure that your Selenium automation tests run as intended and there are no false positives or negatives.

In this Protractor tutorial, I’ll get you started on how to debug Protractor tests, which is one of the most popular JavaScript testing frameworks. If you want to learn more about how to write test scripts in Protractor, you can refer to our previous article on cross browser testing with a protractor.

What Are The Problems To Debug Protractor tests?

  • The testing of a web application is tricky due to its dependency on the entire system.
  • You’ll require a different WebDrivers for various operating systems and browsers for performing cross browser testing.
  • The Selenium test automation scenarios follow a sequence of actions and the output of the current test cases serves as the input of the further test cases and hence there is a dependency.
  • The long error messages encountered while performing automation tests might be tough to comprehend.
  • It becomes difficult to distinguish between errors and issues which are either related to browsers or test scenario processes.

What Are The Types Of Failures You Need To Debug In Protractor Tests?

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

Here I’ll further explain these failures in this Protractor tutorial.

Expectation Failure

WebDriver Failure

WebDriver Unexpected Failure

Protractor Angular Failure

Protractor Timeout Failure

How To Debug Protractor Tests In Selenium?

You can debug Protractor tests works is by utilizing the following methods stated in this Protractor tutorial:

  • Pause Method
  • Debugger Method
  • Screen Shot Method

Pause Method To Debug Protractor Tests

As an example of this Protractor Tutorial, I’ll use the script shown below.

test_debug.js

In the script test_debug.js, I have specified a test scenario where we locate an element in the web application using the locator by.binding() with (‘myTestString’) but the launched URL i.e. (https://google.com) in the browser does not have the element with the specified locator.

When the script shown above in this Protractor tutorial is executed, this will result in a failure with NoSuchElementError. Hence, in order to find the root cause of the issue, it is necessary to debug the script diligently.

Now, I’ll show you how to use the browser.pause() method to debug this failure for Protractor testing. Before proceeding with the changes, I’ll make necessary changes for the configuration in the test_config.js file as shown below:

test_config.js

This is the configuration file used by protractor for managing any config parameter used globally within the web application.

Please note that we have increased the timeout in the above config file for the parameters all Scripts Timeout and default timeout interval to 999999. By default, the timeout interval set is 11 sec and 30 secs respectively.

Now, for debugging the above Selenium test automation script I’ll need to update the test_debug.js file to add browser.pause() in the place where we would like to pause our test for debugging i.e. after loading the URL. The updated script looks as below:

To script is executed with the below command which will also start the debugger.

$ protractor test_config.js

Here in the output:

Image for post
Image for post

When the above code is executed and the pause command is hit, we can see it pauses the code at that point and the debugger is started after launching the URL in the browser.

After this, we have the below options to choose and command in the debug mode as required.

C: Press the C key and hit enter to move forward in the execution i.e. the next immediate step in the flow is executed by the protractor. If the C is not pressed the test will not move forward halt due to timeout. Also, we can continue using C until a failing statement is encountered.

repl: Using repl command in the terminal allows us to enter the interactive mode which is required in order to send out web driver commands to the browser and executes the protractor statements at run time. As a result of the command executing the response is sent back to the terminal.

For example: The issue in the statement that is causing the error in our script is the element (by.binding(‘’myTestString’)).getText(). Therefore, I’ll use the repl to enter the interactive mode and use the correct locator. You can refer to this article on locators in Protractor to know more about how to use locators with Selenium Protractor.

Image for post
Image for post

Ctrl + C: In order to exit the test from the pause state you need to type Ctrl + C to resume the test.

Debugger Method To Debug Protractor Tests

The protractor testing script is executed with the debug option as shown in the below command. This command will also start the debugger.

$ protractor debug test_config.js

While using the debug method, we can also choose to type C command in the terminal similar to the one used in the pause method for continuing forward in the test code. But unlike the pause method, it can only be used once in case of the debugger method.

Screenshot Method To Debug Protractor Tests

test_debug.js

Debug Protractor Tests On Online Selenium Grid Platform

test_config.js

test_debug.js

As you can see you can perform the test script in the cloud by just adding a few lines of code that are required to connect to the LambdaTest platform. You are required to generate the desired capability matrix and through this, you can specify the environment on which you would like to execute our tests. Also, you need to add the LambdaTest username and access key which uniquely identifies with the LambdaTest platform. Here is the link to visit LambdaTest Selenium desired capabilities generator.

Image for post
Image for post

We can see that our Selenium test automation script got executed successfully on the platform and you can also execute the same set of commands that we used on the terminal while using the pause and debugger methods to debug Protractor tests. Below is the output on running the test:

Wrapping It Up!

Do share your view on this Protractor tutorial with us in the comment section down below. Also, help us to share this article with your friends. That’s all folks! Happy Debugging!

Originally published at LambdaTest

Written by

Product Growth at @lambdatesting (www.lambdatest.com)

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store