Learn to Debug Your Code

with Chrome Developer Tools

GDI Logo


Follow along in Google Chrome at
jennifer-mann.github.io/gdi-developer-tools

Welcome!

Girl Develop It is here to provide affordable and accessible programs to learn software through mentorship and hands-on instruction.


Some "rules"

  • We are here for you!
  • Every question is important.
  • Help each other.
  • Have fun.

Welcome!

Tell us about yourself.

  • Who are you?
  • What do you hope to get out of the class?
  • If you could plan your ultimate vacation, where would you go?

Debugging, then

Debugging client-side code was hard for a long time.

CSS had a lot of this:

div.className {
  border: 2px solid green;
}

body {
  background: red !important;
}

and JavaScript had a lot of this:

( a.k.a. alert(); all the things! )

Debugging, then

So basically:

Inexact

Time-consuming

Totally not fun!

Debugging, now

Web debugging got infinitely better in early 2006.

Webkit Introduced the “Web Inspector”

and

Joe Hewitt introduced Firebug for Firefox



IE didn’t have equivalent debugging tools until IE8 was released in 2009.

Debugging, now

Screencap of this presentation in dev tools.

Debugging, now

Chrome Developer Tools is…

  • a Document Object Model (DOM) inspector
  • a DOM manipulator
  • an emulator
  • a Javascript REPL
  • a debugger
  • a network profiler
  • an IDE?

… and more!

Enough History, Dive In

Open Developer Tools

Hamburger menu > More Tools > Developer Tools | option/alt + command/control + i

Mac: option+command+i
Win: alt+control+i

Overview

Screencap of overall dev tools panel

Workflow Customization

Placement

Screencap of panel docked to bottom of window Screencap of panel docked to right of window Screencap of panel in own window

Workflow Customization

Toggle Console

Screencap of console in drawer Screencap of console hidden

Mac: Esc
Win: Esc

Workflow Customization

Settings

Screencap of settings

Working with the DOM

The Elements Tab

What is the DOM?

Think of it as your HTML…

…+ anything JS adds.

The Elements Tab is a visual representation of how the browser built the DOM.

You can explore the nested levels by clicking the arrows next to elements.

Inspecting Elements

You can inspect any element on the page to learn more about it.

Right click > Inspect Element
Turn on Inspector Mode, then select element

Manipulating the DOM

Use the context menu (right-click) to:

  • Delete elements
  • Change element attributes
  • Edit as HTML

Drag and drop to reorder elements

CSS Debugging

Screencap of CSS pane

When an element is selected, all of the CSS rules that are applied show in the Styles section, ordered by specificity.

You can see what is applied, what is overridden, and what is invalid.

CSS Specificity

Specificity refers to how CSS rules are applied based on ordering, inheritance, and the cascade (the 'C' in CSS).

The Basics: Where Do The Styles Live?

  • Anything with !important (most important)
  • Inline Styles / modified by JS
  • External Stylesheet
  • User Agent Stylesheet (least important)

CSS Specificity

Order matters, so do selectors. Code as simply as possible. Complex selectors modify specificity in complex ways!

The Basics: What's the Selector?

Halp!
div {
  color: lime;
}
#special {
  color: gray;
}
.orange {
  color: orange;
}

CSS Specificity

Further reading:

Beginners: Specifics on CSS Specificity @ CSS-Tricks

Advanced: A Specificity Battle @ CSS-Tricks

CSS Prototyping

Manipulating CSS attributes

You can manipulate CSS by changing attributes, adding them, using a color picker, and more!

CSS Prototyping

Adding CSS style rules

You can add new selectors by clicking the plus icon, or inline styles in the element.style section.


Force CSS state

You can also force element states to easily modify the styles that are applied there by clicking the pin icon.

(think link styling)

Animations

Animations pane

The Animations section gives you tools to debug CSS animations. Open it by clicking the stacked diamonds icon.

Device Mode

Set screen size, pixel ratio, throttle network, and more…

Screencap of emulation

Responsive Breakpoints

See how CSS applies with different media queries and quickly jump to those different sizes.

Screencap of emulation mobile Screencap of emulation tablet

Let's Develop It!

girldevelopit.com
  • Reorder the nav
  • Figure out the size of the 'Fork me on GitHub' image
  • Change the nav hover states

Basic JavaScript Debugging

The Console

The Console is a JavaScript REPL. You can open the Console tab or open it as a drawer on other tabs.

Console as REPL Press Esc while DevTools is focused

The Console API

There is also a Console API that makes debugging much more streamlined than a bunch of alert() messages.

console.log('A debugging message to show');
console.info('This message shows up a little different!');
console.warn('Another fancy message!');
console.clear(); // clear the console

It's best practice to remove any Console API calls before releasing code.

Stacks

If a message is repeated, rather than printing every time, the messages are stacked.

for (var i = 0; i < 10; i++) {
  if (i % 2 === 0) {
    console.log("Hi");
  } else {
    console.log("Hey");
  }
}

Filters

You can use filters to sort through the different types of messages in the console.

Filtering the console

Autocomplete

When you type in the Console, the Console automatically displays options of available or previously used methods.

Autocomplete console methods

Track Exceptions

Errors in the console display with a link to the file name with a line you can navigate to.

Line number and file name of exceptions

The Console

There are many ways to use the Console API, and there’s much more to learn.

Using the Console @ Google Developer DevTools docs

Intentional 404

The Sources Tab

Our JavaScript errors, warnings, and logs link to the exact code that triggered them in the Sources Tab*

Source Error screenshot

* Minified JS makes it difficult to debug, so use unminified code when testing, but switch to minified for production

debugger;

Ever wish you could just make your JS stop at a certain point so you can study your variables?

debugger; triggers a special state so that you can do that.

Debugger screenshot

Press the blue arrow at the top of the page to continue JS execution, or the arrow over a dot to jump to the next line of your script.

JS Breakpoints

JS breakpoint screenshot

You can set breakpoints in the Sources Tab to stop your JS when it gets to a certain point.

DOM Breakpoints

DOM breakpoint screenshot

It may be important to know when your DOM is changed by JS. You can set DOM breakpoints too.

The Profile Tab

The Profile Tab allows you to see the CPU usage of your JavaScript. This lets you check for poor performance.

Profile screenshot

The Timeline Tab

The Timeline Tab allows you to see a lot more information about your page’s performance.

Check out the timeline from our slowFunction().

Timeline screenshot

Warning: Timelines are resource hogs! Stick to recording a few seconds at a time.

Assets and Connections

The Network Tab

Web pages are rarely made from a single HTML file. The Network Tab shows us all of the different requests our page makes.

Network Panel screenshot

Checking Out Requests

Each request in the Network tab shows important information, including:

  • Status Code
  • Request Method
  • Response
  • Headers

Learn more about what all those mean at
HTTP Protocol @ Tutsplus

Network Performance Checking

You can use the timeline on the Network tab to learn about performance of your page:

  • Bottlenecks: is an asset causing loading to hang?
  • Are you trying to access things that don't exist?
  • Do you have a huge image that takes forever to download?
  • What's the experience like for slow connections?

The Resources Tab

The Resources Tab shows us additional information and data our page has access to, like cookies, local storage, and session storage.

It also shows all the files comprising your page, and updates as additional requests are made (e.g. lazy loaded images).

Resource Panel screenshot

The Security Tab

The Security Tab shows us information about the SSL Encryption, and security certificates for the various domains the page requests resources from.

Security Panel screenshot

The Audits Tab

The Audits Tab shows us information about overall performance of the page, and how we might be able to speed up loading times.

Audits Panel screenshot

Resources

Questions?