I’m writing a test suite with Capybara, and I need to test the length of a textbox in Ruby. It has a limit of 255 characters. Once the limit is reached you can’t type anymore. So what I’m looking for is to count the number of characters that has been typed into the text box to ensure it doesnt exceed 255 characters. This is being done in Ruby (I’ve only started learning code so all new to me, any help would be great).
The :xpath /html/body/div[4]/div[2]/div[7]
I’ve researched a lot and all the code I can find is how to create a text box and to put a limit of the characters but I’m looking for code that can test that it works.
Stuff I’ve found like a.count or content.count but I cant get it to work.
Any help would be appreciated. Thanks in advance.
Ruby code can help you generate the textbox you need, but it won’t track how the user is using it – at least not directly. That’s the job of the browser and the javascript you write to catch browser events. And yes, you can catch each key press and send the result back to your server using AJAX so that you can process it in ruby, but why would you want to do any of this?
What you probably want is a ruby helper that can generate some html to tell the browser to limit the textbox input to a certain count.
Something like this, perhaps:
For text_area something like this:
Or better yet, put that javascript in a helper .js file and add it using jQuery or the like.
UPDATED
To test the content length using RSpec/capybara with the xpath selector you provided: