3

Is there a way for an input field to be cleared before entering some text into it. I'm fairly new to writing capybara with rspec and I have some trouble with masked input fields.

Here's the method I tried:
find('AddressZip').clear
fill_in('AddressZip', :with => '77487')

I tried searching for similar methods, but haven't found anything. Help would be much appreciated.

cloudrunner
  • 479
  • 1
  • 7
  • 10

3 Answers3

3

I don't know if there are any better solutions but I've been using this

 page.execute_script("$('#{field_id}').val('');")
RobertH
  • 528
  • 3
  • 13
  • Thanks for the response, appreciate it. Turns out there is a much simpler method, clicking the input field first and then using the fill_in method will work. – cloudrunner Aug 13 '13 at 19:28
3

To my knowledge, execute_script ignores scopes defined using whithin. It's verbose too.

Better to use either

fill_in locator, with: ''

or

find(complex_selector).set ''

as described in the accepted answer for this question

Community
  • 1
  • 1
Cec
  • 1,726
  • 3
  • 18
  • 31
2

Normally clearing a text field is as simple as fill_in locator, with: '' but this did not work for me with a certain field controlled by a Vue.js component. After trying 5 different workarounds proposed on SO I ended up having to resort to using the send_keys method with :backspace, E.G:

10.times { find(:css, "input[name$='title']").send_keys(:backspace) }

Arctodus
  • 5,743
  • 3
  • 32
  • 44