Debugging and Testing Using External Tools
There’s a very active partner community supporting Cordova with additional tools for Cordova developers. In this section, I introduce a couple of the more popular tools that help developers test and debug Cordova applications. This is by no means a complete list of options; refer to the PhoneGap Tools page (http://phonegap.com/tool) for information on additional tools that might be available. Some built-in debugging tools are also available with several of the mobile SDKs. These tools are covered in the individual chapters for each mobile OS (Chapters 7 through 10).
Debugging Applications with Weinre
Web Inspector Remote (weinre) is a community-built remote debugger for web pages. It was donated to the PhoneGap project and is currently implemented as part of the PhoneGap Build service. You can find the download files and instructions at http://people.apache.org/~pmuellr/weinre/docs/latest.
For Cordova development, it allows you to remotely debug a web application running in a Cordova container on a physical device or a device simulator. Weinre consists of a debug server, debug client, and debug target. The debug server runs on Macintosh or Windows, and the debug client runs in any compatible desktop browser.
To configure weinre, you need to perform a series of steps. The process begins with the server installation. Weinre is Node.js-based, and since we already have Node installed for the Cordova CLI, you can install the server using the following command:
npm install –g weinre
Unfortunately, on Macintosh weinre may not like your security configuration, so even though it’s not recommended, you may have to install weinre using sudo using the following command:
sudo npm install –g weinre
After the installation completes, you should see a message similar to the following:
├── firstname.lastname@example.org (email@example.com)
└── firstname.lastname@example.org (email@example.com, firstname.lastname@example.org, email@example.com, firstname.lastname@example.org)
With the installation completed, you can start weinre by issuing the following command in the terminal window:
When the server starts, it will indicate that it is running by displaying a message in the terminal window similar to the following:
2013-06-22T17:00:50.564Z weinre: starting server at http://localhost:8080
With the weinre server started, you use a browser-based client application to interact with the server and Cordova client application. Open your browser of choice (I recommend using Safari or Chrome) and point it to the URL shown on the server console when the weinre server started. For my development environment, I simply use:
The browser will connect to the weinre server and open the weinre debug client, which will display a page similar to the one shown in Figure 6.8.
Figure 6.8 Weinre Debug Client Startup Page
With the server and client running, you can now connect a Cordova application to the debug server by adding the following script tag to body section of the Cordova application’s index.html file:
You need to replace the debug_server portion of the URL with the correct host name or IP address for the debug server (the system running the weinre server). This makes the application into a weinre debug target and provides the Cordova application with the code needed to upload information to the weinre server as the application runs.
When using weinre with a device simulator, you can usually point the Cordova application to the local weinre server instance using
The Android emulator, however, does not have the ability to connect to host-side resources using localhost, so for the Android emulator you must use the host address http://10.0.2.2, as shown in the following example:
When using weinre to debug a Cordova application running on a physical device, the device must be able to connect to your debug server. That means that the device must be able to “see” the server on the local network (most likely over a Wi-Fi connection), or the system running the weinre server must have a public facing IP address. Using a server host name of localhost will not work on a physical device; you must use an actual host name or IP address that is visible to the device.
After you have added the script tag to the Cordova application’s index.html file, run the application in the simulator or on a device. Nothing special will appear on the device screen—you can’t tell that the weinre debug client is running. However, if you switch to the browser running the weinre debug client and click the first link, shown in Figure 6.8 (the one labeled “debug client user interface”), you will initially see a page similar to the one shown in Figure 6.9.
Figure 6.9 Weinre Debug Client
In this example, the figure is indicating that no targets have connected yet, but as soon as I start my Cordova application, as long as it can connect to the weinre server, the debug client page will update and display the content shown in Figure 6.10.
Figure 6.10 Weinre Debug Client with an Application Connected
The debug client provides the means to view and optionally manipulate many of the page elements and other aspects of your application’s web content.
At this point, the different buttons across the top of the debug client are available to provide you with information about the debug target. For example, in Figure 6.11 you see the contents of the Elements page; it shows you the current HTML5 content running within the debug target.
Figure 6.11 Weinre Debug Client Resources Area
One of the cool features of weinre is that as you highlight the different code sections shown in Figure 6.11, weinre will highlight the corresponding content within the web application. So, for the HelloWorld3 application shown in Figure 6.11, highlighting the paragraph tag <p id= "appInfo">_</p> reveals, in the debug target, the section of the page shown in Figure 6.12. In this example, I kept the content of the paragraph tag collapsed in the debug client. You can click the black triangle to the left of the <p> element to see the complete HTML content.
Figure 6.12 Weinre Target Highlighting HTML Content
Using the debug client, you can access the following content areas:
- Resources: Local resources used by the application, such as databases, local storage, and session storage
- Network: Information about requests made using the XMLHTTPRequests (XHR)
- Timeline: Events that occur within the target application
- Console: Information written to the console using the console object described earlier in the chapter
The available documentation for Weinre is pretty light, but since the project’s capabilities are based on the Google Chrome Developer Tools, you can find additional information on the Google Code website at http://code.google.com/chrome/devtools/docs/overview.html.
Testing Applications Using the Ripple Emulator
The Ripple Emulator is a tool you can use to help with the initial testing of your Cordova application. Ripple is a browser-based emulator that can be used to emulate several different systems. Originally created by Tiny Hippos, which was then acquired by Research In Motion (now called BlackBerry), Ripple is now an incubator project at Apache. The problem with Ripple is that it’s been in beta for a very long time (almost two years by my counting), and the emulator is way behind on its Cordova support (supporting Cordova 2.0 when Cordova 2.8 was just released). Because of those limitations, I don’t go into too much detail about how Ripple works.
Ripple emulates the execution of the Cordova APIs within the browser container. You can use Ripple for quick testing of Cordova application features and UI during development, then switch to packaging/building Cordova applications and testing them on actual devices or device simulators for more thorough testing. Ripple is not designed to replace testing on real devices or simulators.
Since Ripple was a BlackBerry project for a while, it has a lot of features that help BlackBerry developers. You can, for example, test your BlackBerry WebWorks applications using Ripple, then package them into WebWorks applications directly from the browser. You can learn more about Ripple’s capabilities at https://developer.blackberry.com/html5/documentation/getting_started_with_ripple_1866966_11.html.
The emulator installs as a Google Chrome plugin, so you will need to install Chrome from www.google.com/chrome before you begin. Because Ripple is an incubator project and may become a full Apache project at any time, any URL I give you now may be invalid by the time you read this. So, to install Ripple, you should point your browser to http://emulate.phonagep.com. Follow the links on that page to download and install Ripple in your instance of the Chrome browser.
Once you have Ripple installed, you must enable file access for Ripple. In Chrome, open the settings page, then select the Extensions section. In the list of plugins that appears, enable the “Allow access to file URLs” option, shown in Figure 6.13.
Figure 6.13 Enabling Ripple File Access in the Chrome
Once the browser is configured, open your application’s index.html file in the browser. You can press Ctrl-O on Windows or Command-O on Macintosh to open the File Open dialog. Once the page has loaded, you need to enable Ripple for the selected page. To do this, click the Ripple icon to the right of the browser’s address bar to open a window allowing you to enable Ripple for the loaded page. You can also append ?enableripple=true to the end of any URL to enable Ripple emulation for that page.
With Ripple enabled, the browser will display a page that prompts you to identify which type of emulation you wish to enable, as shown in Figure 6.14. As you can see, Ripple can emulate Apache Cordova plus several other platforms and frameworks. Click the Cordova 2.0 button to continue.
Figure 6.14 Ripple Emulation Platform Selection Page
At this point, Ripple will display a page with the content from the index.html file rendered within the boundaries of a simulated smartphone screen, as shown in Figure 6.15. Wrapped around the simulated smartphone are properties panes that can be used to configure options and status for the simulated smartphone, such as simulated device screen resolution, accelerometer, network, geolocation, and more.
Figure 6.15 Ripple Emulator Running a Cordova Application
You can click on each of the tabs to expand the options for the tab and make changes to the simulated device’s configuration. At this point, you would simply click around within the simulated smartphone screen and interact with the options presented within your application. When you find a problem or a change you want to make within the Cordova application, simply return to your HTML editor, make the necessary changes, write the changes to disk, then reload the page in the Chrome browser to continue with testing.