An Overview of Mobile Debugging Techniques – Part One
Join the DZone community and get the full member experience.Join For Free
an overview of mobile debugging techniques – parttwo
safari remote debugging
the first option we'll look at is specific to ios devices, but can still be useful for debugging in general. in ios6, apple introduced a new feature called safari remote debugging. this allows you to use the safari web browser to debug web applications being run on ios devices (both iphones and ipads). this also includes phonegap applications. unfortunately, this option is only available to folks running mac os x.
to use this feature, your ios device must be physically connected to your laptop. next, you need to enable it (one time) on the device itself. open the settings app, go to safari, and then click the advanced menu. here is a screenshot from an ios7 device:
simply enable web inspector and this device will be ready for remote debugging. before continuing on, open up safari on your device and visit any web page.
next, open up safari. if you don't see the develop menu, go into your safari preferences, select the advanced tab, and turn it on there.
in the develop menu you will see a new menu option that matches the name of your ios device. (note that if you have multiple devices connected you will see all of them, including the ios simulator if you have that running.)
if you mouse over the device name, you'll see the name of the web page visible in safari, or a list of urls if you have multiple tabs opened.
select the url and safari will open up a set of dev tools:
you now have the ability to interact with your mobile safari directly from the desktop. if you're primarily a chrome user, the menu options here may not be terribly obvious. (the next version of safari will be updating the developer tools ui so what you have now may be a bit different.)
going from left to right, the menu icons are:
resources: this provides roughly the same view as the elements panel in chrome's dev tools.
storage: this is similar to the resources panel in chrome's dev tools. note that unlike chrome's dev tools, the web inspector only seems to support cookies and web sql databases. local storage is missing from the ui. (although you could use the console to inspect these values.)
issue: reports on issues found in the current request. you can find similar information in chrome's console, but this focused view is pretty handy.
breakpoint: where you set and view breakpoints.
finally, the console.
let's take a closer look at the tabs that i think are going to be the most valuable to you in terms of debugging. to be clear, you should be aware of the capabilities in all the different tabs, but for this article we will focus on some of the more important ones.
as i mentioned above, you can also run code from here as well. this could be useful for manually firing functions or even just checking variables.
here's a simple example. i've used the console to check the value of navigator.useragent and run an alert. notice that it fires on the device (the simulator in this case) itself.
the resources tab gives you access to the dom. as you select code in the panel, you will see it highlighted in real time on the device.
even more interesting is that you can edit the html live on the device. double clicking on the text of the h1 tag lets us edit and modify it.
selecting the icons on the upper right hand side give you access to the tag node and style properties. under the style property menu you can actually modify the design. this is especially helpful on mobile devices. in the example below i've just added a color, but you could play with margins, padding, and size values to try to improve the design of your web site.
in the instrument panel, the network requests are probably the first useful tool you will need. in complex ajax-based applications, being able to trace and monitor network requests is incredibly helpful.
the first thing you should be aware of though is that network requests that fail. 404s for example, will not show up here. instead they will show up in the console only. chrome on the desktop will show the failed network requests in both the networks tab and the console.
notice the arrow on the selected row? clicking it will load the raw result of the request:
i can then simply click a line to add a breakpoint. i've done so on line 7 below:
now when i execute this on the device, it will actually pause execution and let you step through the function one step at a time.
chrome remote debugging
for the next portion of this article, we'll focus on chrome remote debugging. as you can probably guess, this is focused on android devices and the chrome browser. it is important that you remember that there is a difference between the stock android browser and chrome for android. the debugging supported by chrome on the desktop is for chrome on the device. that's probably obvious but don't forget. as of a report from approximately 6 months ago, the stock android browser was in use around 10 times more than chrome for android! despite that, you can still make use of chrome remote debugging.
to begin, you install the adb extension for chrome ( https://chrome.google.com/webstore/detail/dpngiggdglpdnjdoaefidgiigpemgage ). this extension adds a new icon to the upper right hand corner of your chrome browser:
in this case, i've got an android device connected already. (note - unlike the ios remote debugger, you can't use this feature with the android emulator.) clicking on it exposes these options:
startand stop adb refer to the debugging service and as long as it is already started you can just leave it be. view inspection targets gives you a list of all connected devices.
next, on your android device, open up chrome, go to settings, then advanced, developer tools, and enable usb web debugging.
once you've done that, return to your desktop, select view inspection targets, and note how the desktop chrome can tell what site you have open on the device. in fact, if you have multiple sites open in different tabs, you can see a list of them on your desktop:
you can see a few options for each tab. reload and close do exactly what you would imagine. the interesting option here is inspect. clicking inspect opens a new window with the familiar chrome dev tools ui:
(blockquote - what is the schemadump tab you ask? that's actually an extension i wrote myself. chrome lets you write extensions that do many things, including extend the built in developer tools. the same extensions that work for my desktop will work for android as well!)
we won't go over the tabs as the assumption is that most folks are pretty familiar with chrome already. if not, you can check out this free devtools course on codeschool . as you can imagine, the console is connected to the device. running a command on the desktop will execute on the mobile device.
also, much like the safari debugger, you can select dom items in the elements panel and see them update live on the device. you can also modify these elements to test out css modifications and other changes. (pardon the lame camera picture but there was no way to select both the device dom being highlighted and the desktop!)
as you can imagine, chrome's developer tools are very powerful and familiar to most developers. having these available to you for testing mobile devices will be incredibly useful!
in this article we focused on the "big boys", ios and chrome remote debugging. certainly there are other mobile platforms out there and other tools available to you. in the next article, we'll discuss two more tools, adobe's edge inspect and weinre.
an overview of mobile debugging techniques – parttwo
this article was commissioned by intel and the tizen mobile web development team.
Opinions expressed by DZone contributors are their own.