Fork me on GitHub

Tuesday, August 26, 2008

Do we need to test hidden fields?

I got into this moral dilemma when I was writing the schnell-driver’s hidden field (input type="hidden") tests. When I was writing the schnell with htmlunit I used something close to Watir API and so I wrote the hidden field tests especially had the hidden set values as a part of the test suite. But when writing the webdriver porting I suddenly got into the question of the need to test the hidden field set value tests.

In normal programming terms when writing unit tests we will not test the private members of a class because we will be exercising them from the public interface available. So there is no need to test the private members and if we feel we need to do then it means that we have some hidden complexity in the private method’s code which needs to be refactored. In the same lines the hidden field can be considered as private member or more appropriately private variable of the form. The value which goes into a hidden field may be directly filled in to create the query string or they may be manipulated by JavaScript before being used in to form post. If we need to manipulate the hidden fields we need to know all these implementation details and ask our testing code to do them, which is not a good idea.

This leads me to a question. Do we need to test or more preferably set values in hidden fields when we test an application? In my opinion we should not as hidden fields are private members of html language. It is better to their working through the form post or the JavaScript than us to deal with them directly and test them individually.


Simon Stewart said...

I agree that hidden fields are HTML's equivalent of a private member in Java, which is why we don't make it easy to change the value directly in WebDriver. Having said that, there are times where it may be useful to edit them, and we do provide a (not terribly obvious) mechanism for doing so by executing javascript:

((JavascriptExecutor) driver).executeScript("arguments[0].value = arguments[1];", element, newValue);

Sai said...

Yes Simon... Even in programming languages it is possible to use some kind of reflection to work with private members directly. But it is rare to find such instances as they are not obvious and so not very encouraging to use in our day to day programming tasks.

Similarly I think updating hidden fields should not be made obvious. This will help to reduce the usage of this.

And thank you and the team for writing webdriver which made me think about this...