What is the best way to test the max length of a field?
For example, I want to validate that if a field has a max length of 20 char that a message will populate for user.
I know I can create a variable and pass in a value > 20 char but I was looking to see what other options may exist. Possibly use a library where I can have random values created.
Hi @bweston,
A couple of options I can think of:
- Generate Random String
- one of the keywords from FakerLibrary
Also Get Length and Length Should Be will probably be useful for checking the length of the string before inputting to the field and after reading back from the field
Hope that helps,
Dave.
@damies13 Your information is very helpful. I used the Get Length so I can verify what is assumed the length prior to testing and what I found out poses another issue. In my example the field length is 20 char but the Get Length returned 27. I read the documentation for Get Length but not sure I understand how Python returned a length greater than the accepted field length.
I decided to take a different approach as I am able to type more than 27 char into the field but only 20 will get saved to the DB. Once a user inputs 21 char a message will appear “field cannot be greater than 20”. In this case I thought I would use Generate Random String and validate the message.
I tested using this code but I am not having any luck. When I use Click Element it sees a different message “Description is required” and the test fails.
click element //*[@id="descriptionInput"]
generate random string 21 [NUMBERS]abcdef
page should contain Description can be a maximum of 20 characters
Hi @bweston,
*I’ll guess you’re using SeleniumLibrary? so I’ll use those keywords, but you can change them to the library you’re using.
You generated a random string, but you didn’t enter it into your field, actually you didn’t do anything with it.
The approach I would take is to generate the random string, enter it into your description field, check the page for the error you expected, then read back the content of the description field and check the length to verify only 20 char got accepted.
click element //*[@id="descriptionInput"]
${newdesc}= generate random string 21 [NUMBERS]abcdef
Input Text //*[@id="descriptionInput"] ${newdesc}
page should contain Description can be a maximum of 20 characters
${descvalue}= Get Text //*[@id="descriptionInput"]
${length}= Get Length ${descvalue}
Should Be Equal As Integers ${length} 20
Now depending on your requirements it might be valid for the description field to have more than 20 chars as long as only 20 got saved, so your next step would probably be to submit the form, then open the record that was created, then check the length of value for description when the record is opened.
Dave.
This solution is working well for me, thanks.