I have a problem with Poltergeist. I can’t get the driver to locate a field on a page. What I want to do is to execute some js to make sure that the input field is empty before I start adding new text. I have a field with a label os "Skill list" and id of "user_skill_list". In order to make the step more generic I want to find the field by the label name and then get the id.
And(/^I delete the content of "([^"]*)"$/) do |label|
field_id = page.find_field(label).native.attributes['id'].value
page.execute_script("$('#{field_id}').val('');")
end
Here's the error message:
@javascript
Scenario: Update user by removing # features/godmin/skills.feature:36
Given "Thomas" skills are "rspec, testing" # features/step_definitions/godmin_steps.rb:63
And I click on "Edit" for "Thomas" # features/step_definitions/godmin_steps.rb:43
And I delete the content of "Skill list" # features/step_definitions/godmin_steps.rb:69
Unable to find field "Skill list" (Capybara::ElementNotFound)
./features/step_definitions/godmin_steps.rb:70:in `/^I delete the content of "([^"]*)"$/'
features/godmin/skills.feature:39:in `And I delete the content of "Skill list"'
```
My cucumber set up is pretty basic.
# features/support/env.rb
...
require 'capybara/poltergeist'
Capybara.javascript_driver = :poltergeist
Capybara.default_max_wait_time = 5
...
Thank you. Please let me know if I should clarify my question.
Assuming your html is something like
<label for="user_skill_list">Skill list</label>
<input id="user_skill_list"/>
Then
page.find_field('Skill list').set('')
should find the field and clear it. If it's not finding the element there are a couple of possibilities.
The element isn't visible on the page
There is CSS being applied that is changing the case of "Skill list"
There are typos in your html so the label isn't actually associated with the field. You can test this by just doing page.find_field('user_skill_list').set('')
so the label isn't involved
Note: you can get the id by just doing page.find_field('Skill list')[:id]
rather than having to resort to .native.xxxx - although it's not really needed for this use case