Testing JavaScript applications is complex and requires a lot of time and effort. Testing JavaScript applications requires the tester to test the application on different browsers (Internet Explorer, Firefox, Safari, Chrome, and so on). This is because the JavaScript code that runs on a specific browser will not necessarily work on another browser.
Testing existing JavaScript web applications (with many web pages) on new browsers that are not supported by the application code is not a flexible process. Supporting a new unsupported browser means allocating more time for testing the application again on this new browser and for the new/regression defects to be fixed by the developers. Let's see a simple Broken JavaScript example, which illustrates this idea. In this example, the user enters his/her name and then clicks on the Welcome button. After that the welcome message appears.
The following code snippet shows the broken JavaScript example:
<!DOCTYPE html> <html> <head> <title>Broken JavaScript Example</title> <script type=»text/javascript»> function welcome() { var userName = document.getElementById(«userName»).value; document.getElementById(«welcomeMessage»).innerText = «Welcome « + userName + «!»; } </script> </head> <body> <h1>Broken JavaScript Example</h1> <label>Please enter your name:</label> <input id=»userName» type=»text» /><br/> <input type=»button» onclick=»welcome()» value=»Welcome»></input><br/><br/> <div id=»welcomeMessage»/> </body> </html>
Tip
Downloading the example code
You can download the example code files for all Packt books you have purchased from your account at http://www.PacktPub.com. If you purchased this book elsewhere, you can visit http://www.PacktPub.com/support and register to have the files e-mailed directly to you.
If you run the code shown in the previous code snippet, you will find that it works fine in Internet Explorer (IE) and Safari while it does not work in Firefox (to be more specific, this example works on Internet Explorer 8 and Safari 5.1, while it will not work on Firefox 3.6). The reason behind this problem is that the innerText
property is not supported in Firefox. This is one of the hundreds of examples that show a code that works in a specific browser while it does not work in another one.
As a result of these complexities, testing JavaScript code requires a good unit testing tool, which provides mechanisms to overcome these complexities. The good JavaScript unit testing tool should be able to execute the test cases across all of the browsers, should have an easy setup, should have an easy configuration, and should be fast in executing the test cases.