Debugging Protractor tests in Visual Studio Code is an efficient way to identify and resolve issues within your end-to-end testing workflow. VS Code's powerful built-in debugger, combined with a properly configured launch.json
file, allows you to step through your Protractor tests, inspect variables, and control execution flow directly within your IDE.
Getting Started: Setting Up Protractor Debugging in VS Code
To debug your Protractor tests in Visual Studio Code, you primarily need to configure a launch.json
file that tells VS Code how to start Protractor in debug mode. Here's a step-by-step guide:
Prerequisites
Before you begin, ensure you have the following installed:
- Visual Studio Code: The latest version.
- Node.js: Protractor runs on Node.js.
- Protractor: Installed either locally within your project (
npm install protractor
) or globally (npm install -g protractor
).
Step-by-Step Debug Configuration
-
Access the Debug View:
Click on the Run and Debug icon (which looks like a bug with a play symbol) in the Activity Bar (the vertical bar on the far left side of VS Code). This will open the Debug view, where you can manage your debugging sessions. -
Configure Launch Settings:
In the Debug view's top bar, click on the gear icon (⚙️). From the dropdown menu that appears, select the Node.js environment. This action indicates that you intend to debug a Node.js application, which Protractor essentially is. -
Generate
launch.json
:
After selecting the Node.js environment, VS Code will automatically generate alaunch.json
file. This file is placed in a new.vscode
folder within your workspace's root directory. Thelaunch.json
file is where you define specific configurations for debugging different parts of your application. -
Set Up Your
launch.json
for Protractor:
This is the most crucial step where you adapt the generatedlaunch.json
to correctly launch Protractor. You'll need to define a configuration that points to the Protractor binary and your Protractor configuration file.Here's an example
launch.json
configuration for debugging Protractor tests:{ "version": "0.2.0", "configurations": [ { "type": "node", "request": "launch", "name": "Debug Protractor Tests", // Path to the Protractor binary. // Adjust if Protractor is globally installed (e.g., "/usr/local/bin/protractor") // or if your project structure is different. "program": "${workspaceFolder}/node_modules/protractor/bin/protractor", // Arguments to pass to Protractor, typically your configuration file. "args": ["${workspaceFolder}/protractor.conf.js"], // Current working directory for Protractor. "cwd": "${workspaceFolder}", // Launch the debugger in the integrated terminal for better output. "console": "integratedTerminal", // Enable source maps if you're using TypeScript, Babel, etc. "sourceMaps": true, // Skip debugging Node.js internal files and node_modules. "skipFiles": ["<node_internals>/**", "node_modules/**"] } ] }
Key
launch.json
Properties for Protractor:Property Description Example Value type
Specifies the debugger type. For Protractor, it's node
."node"
request
Defines whether you're launching a program ( launch
) or attaching to an already running one (attach
). For starting Protractor, uselaunch
."launch"
name
A user-friendly name for your debug configuration, which will appear in the debug dropdown. "Debug Protractor Tests"
program
The absolute path to the Protractor executable. It's usually found in your node_modules
directory if installed locally. For global installations, you might point to/usr/local/bin/protractor
or equivalent."${workspaceFolder}/node_modules/protractor/bin/protractor"
args
An array of command-line arguments passed to the program
. This is typically the path to your Protractor configuration file (protractor.conf.js
).["${workspaceFolder}/protractor.conf.js"]
cwd
The current working directory for the debugging session. Setting it to "${workspaceFolder}"
ensures relative paths in your config file are resolved correctly."${workspaceFolder}"
console
Determines where the debugger output appears. integratedTerminal
is often preferred as it mimics the standard command-line execution and provides interactive capabilities."integratedTerminal"
sourceMaps
Set to true
if you are using TypeScript or a similar transpiler and want to debug your original source code.true
skipFiles
An array of glob patterns to skip while debugging. This is useful to avoid stepping into core Node.js modules or third-party node_modules
code, focusing only on your application's logic.["<node_internals>/**", "node_modules/**"]
-
Save the Configuration:
After making the necessary modifications to yourlaunch.json
file, save it (Ctrl+S or Cmd+S). The debugger is now configured and ready to be used.
Starting the Debugger and Using Debugging Features
Once your launch.json
is configured:
- Select the Configuration: In the Debug view, select "Debug Protractor Tests" (or whatever name you gave your configuration) from the dropdown menu at the top.
- Start Debugging: Click the green "Start Debugging" button (the play icon). VS Code will launch Protractor, and your tests will begin to run.
Essential Debugging Techniques
- Breakpoints: Click in the gutter to the left of a line number in your test files (
.spec.js
or.ts
) to set a breakpoint. When execution reaches this line, it will pause. - Stepping Controls: Use the debugger controls in the top bar (or keyboard shortcuts) to navigate your code:
- Continue (F5): Resume execution until the next breakpoint or the end of the program.
- Step Over (F10): Execute the current line and move to the next, skipping functions calls.
- Step Into (F11): Enter into a function call on the current line.
- Step Out (Shift+F11): Exit the current function and return to the calling function.
- Variables, Watch, Call Stack, and Breakpoints Panels: These panels in the Debug view allow you to inspect the values of variables, add expressions to watch, view the execution path, and manage your breakpoints.
- Debug Console: The Debug Console allows you to evaluate expressions, execute code, and interact with your application's state while debugging. This is incredibly powerful for Protractor, especially when combined with
browser.pause()
.
Practical Debugging Tips for Protractor
browser.pause()
: Insertbrowser.pause()
within your Protractor tests to temporarily halt test execution in the browser. While paused, you can use the Debug Console to inspect element properties (element(by.id('myId')).getText()
), evaluate browser-side JavaScript, or even execute Protractor commands directly. This gives you a live view of the browser state.debugger;
statement: You can insert thedebugger;
statement directly into your JavaScript or TypeScript test files. When the code execution hits this line and the debugger is attached, it will automatically pause, similar to a breakpoint.- Source Maps: If you're writing tests in TypeScript (or another language that compiles to JavaScript), ensure
sourceMaps
is enabled in yourtsconfig.json
andlaunch.json
to debug your original.ts
files, not the compiled.js
output. - Isolate Failing Tests: When debugging, it's often helpful to comment out other tests and focus on a single failing spec to simplify the debugging process.
By leveraging these features, you can effectively pinpoint and resolve issues in your Protractor tests, leading to more robust and reliable end-to-end automation.