You are writing some automated tests with Selenium, that require you to fill in some text fields in a form. You are pretty confident you typed the values you expected to type, into the field you expected to type into. But, here are just 3 reasons why you should write some code that checks that you actually wrote what you thought, where you thought, before submitting the form you are trying to fill in.
Before jumping to the “why we should check those values”, let me give you a few short tips that will help you when working with text fields:
- before typing into a field, make sure you clear the field beforehand. That way, when you start typing, you are more confident that you don’t have any residual text present along with what you want to type. This is equivalent to
- checking what was typed into a field is normally done by checking the “value” attribute of the text field. Therefore, if you want to read the value of the text written into the field, you should do something like:
Now, why would you want to make sure you typed what you think you typed:
- Many times your fields will come with some kind of suggestions that tell the user what pattern should be used for typing into that field. Sometimes, this suggestion is displayed right inside the field, maybe as a more grayed out text. In this case, in order to make sure that the text you typed does not include the suggestion too, make sure to check the value of the text from inside the field.
- If you are mistaken about what you typed into a field, and in fact you typed something else, your test is not relevant because you are not sending the data you thought you were sending into the system. So you mistakenly think that for a particular String, the system works fine, but in fact that String never made it into the system.
- When you need to type into many text fields to fill in all the data in a form, you sometimes just copy paste the sendkeys command (the one that writes text inside a field), and then you just rename the field names. For example, you want to write First Name, Last Name. If you write the code for filling in the First Name, than you just copy paste that code, and forget to change the second occurrence of the code with the webElement corresponding to the Last Name, you will in fact never type into the Last Name field. You will just type into the First Name field twice, replacing what you typed initially with the text from the second occurrence of the code. Even worse, if the application under test would have these as mandatory fields, and there is a bug in the system, you might not realize that the “mandatory” part of the system has failed, since: you thought you typed into all mandatory fields, but you did not, and the system did not prompt an error.