Testing the Client Side Logic

an article added by: Sonja Lande at 05312007


In: Root » Computers and technology » AJAX » Testing the Client Side Logic

French Spanish Portuguese Italian German Japanese Chinese Korean Russian Arabic

Problem You want to effectively test your application’s client-side logic. Theory Testing GUI code tends not to be a productive task because of the complications that arise. The main complication is how to test the correctness of a user interface. Imagine a situation where clicking a button causes a table to be filled with data. Now imagine that when a check box is checked and the button is clicked again, a different table is filled with content. The fact that clicking the same button results in two different generated outputs is frustrating and complicates GUI testing. One of the main reasons why testing a GUI is complicated is because the GUI is a black box, and the user cannot access the individual GUI elements. RealPlayer is used to play some media file. Imagine running a media-file generation service. Testing the format of the media is not a problem and is akin to testing the contract. You also need to test if a client such as RealPlayer can consume, process, and display the media. Figuring this out requires taking a snapshot and then checking if the bits and bytes are displayed properly. Thus, you’re left wondering how to test the functionality of your data stream when played in RealPlayer. The simplest but probably most expensive and error-prone solution is to have a human look at RealPlayer and say, “Yes, the content is being played,” or “No, the content has problems.” A human could carry out the same tests repeatedly and then verify whether the tests worked.

The problem with getting a human to do this is that humans make errors when doing the same thing over and over again. Humans are not incapable of doing the same thing over and over again, but they get bored and the mind wanders. Humans are much better at spotting problems if the menial work is replaced with automation. Automating the testing of RealPlayer is difficult, because RealPlayer is not under your control. It is programmed using a traditional programming language. Traditional programming languages create black boxes that a user cannot get around. In the old days of Windows 3.1, an application could access another application’s window handles. That ability made it possible to do interesting things, but the cost was that hackers could wreak havoc on an application. Today, applications are not allowed to access another application’s GUI elements willy-nilly. Solution The only effective way to test a GUI is to have access to the source code and test the layer that the GUI calls. The idea is not to test the GUI, but rather to test the logic that the GUI calls. Of course, this only works if the GUI layer contains absolutely no logic and no state. This is more complicated because it means that the GUI is a copy of the state held in memory. Many developers consider this a good thing, and I’m not about to debate the merits or problems of this strategy. Instead, I want to highlight that Ajax and DHTML applications are unique in that they allow you to have your cake and eat it too. With DHTML, two completely unrelated HTML pages can communicate with each other and slice and dice each other’s data. In a traditional programming approach, you wouldn’t like to have other applications changing your look and feel to suit their needs. Yet this is an inherent behavior of DHTML. This feature can be put to good use when creating tests.

The browser window in the upper left-hand corner is the test controller, which contains a number of buttons used to test individual features. The test controller button Test Get Document is used open a new instance of the HTML window to be tested. It assigns the instance to the script variable testWindow. When clicked, the button Test Add executes the method TestAdd, which then calls a method DoAdd defined in the new instance of the HTML window. This is a unique feature of DHTML, in that one HTML window can reference elements in another HTML window even though both windows are unrelated. The test controller uses the identical testing routines as outlined in the “Coding the Contract Using Test-Driven Development Techniques” section. The test controller example uses the calculator example. To be able to test the calculator application, copy and modify the empty template file that represents a test. Add the three tests used to verify the correctness of the calculator. There were two unique contracts in the contract recipe.

The additional test is the test to open a new window that will load the initial HTML page used to add two numbers. The modification of the template test page involves adding some tests, as well as some user interface elements used to instantiate the tests. I won’t focus on the user interface elements, as the details have already been explained in the “Coding the Contract Using Test-Driven Development Techniques” section. Instead, I’ll focus on the tests that are executed, as they are unique in that they don’t use the XMLHttpRequest object directly. The tests execute functionality in the other HTML page. In the following implementation of the testsToRun variable, note that the declaration has been abbreviated for clarity purposes.

legal disclaimer

Our website is not responsible for the information contained by this article. Web-articles is a free articles resource.
Suggestion: If you need fresh, daily updated content for your website, feel free to use our service. Click here for more information.

related articles

1. Understanding the Definition and Philosophy of REST
Understanding the Definition and Philosophy of REST REST is a controversial topic among Web service enthusiasts, because it’s considered to stand for the opposite of what Web services and SOA are trying to achieve. The problem with this thinking is that REST is not in contradiction with the abstract definition of SOA and Web services. REST is in contradiction with technologies such as SOAP, WSDL, and WS-* specifications. The following offers a quick definition of REST:...

2. The Easiest Way to Get Started with Ajax and REST
The Easiest Way to Get Started with Ajax and REST Problem You want to know the best way to get started with writing Ajax and REST. Solution When developing an Ajax and REST application, you must decide on the tools and frameworks you’ll use. The choice is simple: Use whatever you’re using today, and write some Ajax applications. You don’t need to change the tools you’re using today. Whether you’re using ASP.NET, JavaServer Pages (JSP), PHP, Ruby, or Python, you...

3. Testing a Dynamic Contract with Ajax
Coding the Contract Using Test-Driven Development Techniques Coding the contract using agile and test-driven development techniques requires writing a number of tests and implementing aMock URL layer. Problem You want to code the contract using these development techniques. Solution To demonstrate, let’s define a use case, implement the use case as a contract, write a test case(s) to implement the contract, implement the contract in the Mock URL, and finally...

4. Understanding JavaScript and Types
Understanding JavaScript and Types Problem You want to work around the fact that JavaScript does not have types declared for its variables. Theory JavaScript code does not have any variables with a declared type. The lack of typed variables is apparent when you declare functions. That said, not having typed variable declarations does not mean JavaScript has no types or no type safety. Let’s start out with the simple declaration of a function, as illustrated by the following ex...

5. Coding Using Conventions and Not Configurations
Coding Using Conventions and Not Configurations Problem You want to make your JavaScript constructs more efficient by applying the Rails “convention over configuration” principle to them. Theory You may already be familiar with the programming platform Ruby on Rails, which is used to build Web applications. The focus of this recipe is not Ruby on Rails, but one aspect of Ruby on Rails namely, convention over configuration (see http://en.wikipedia.org/wiki/ Ruby_on_Rails for m...

6. Advantage of parameterless functions in JavaScript
Using Parameterless Functions Problem You want to take advantage of parameterless functions in JavaScript. Theory JavaScript functions for the most part have parameters. You may think that the previous sentence states the obvious after all, without parameters, what data could be passed to a function? JavaScript has the ability to declare functions that have no parameters, even though the caller of the function has passed parameters to the function. For example, let’s look at...

7. JavaScripot Functions
Treating Functions Like Objects Problem You want to take advantage of the fact that functions are objects (remember, everything is an object in JavaScript). Theory Many people think that a function is some keyword used in JavaScript. A function is also an object that can be manipulated. Knowing that a function is an object makes it very interesting from the perspective of writing JavaScript code, because the code can treat the function like another other object. This mean...

8. Implementing an Error and Exception Handling Strategy
Implementing an Error and Exception Handling Strategy Problem You want to implement a clean error and exception handling strategy in your applications, to make them run more smoothly. Theory Of course, you might argue that one error is a dialog box and the other is generated in the JavaScript console. The fact that one browser uses a dialog box to show an error and the other does not is a browser issue, not an error issue. A concise way of classifying the two errors is to ...

9. Understanding the Behavior of Variables When Implementing Recursion
Understanding the Behavior of Variables When Implementing Recursion Problem You want to implement recursion in JavaScript, and you also want to understand how variables will behave under those circumstances. Theory In JavaScript, you do not need to declare the variable type, or even declare the variable. For example, the following code works perfectly: if( counter == 1) { buffer = "counter is 1"; } document.getEle...

10. Using Functions to Initialize and Make Decisions JavaScript
Using Functions to Initialize and Make Decisions Problem You want to use functions to initialize and make decisions. Theory Usually when you write a piece of code where a change of logic needs to take place based on a context, you use a decision structure. For example, say you are implementing a light switch using a program. You turn on the light if the light is off, and you turn off the light if the light is on. The behavior of the program is determined by the conditions. One example behavior t...