how to replace text in input field with Capybara - capybara

For my integration test of editing a record, I try to replace the existing text in a form input field with a new text:
find("input[#id='course_title']").set("a safer workplace")
However,everytime I check the page with
save_and_open_page
the text in the input field is not replaced with the new test.
how can I replace the text value of an input field a new text value in Capybara?
Thanks for your help,
Anthony

There are several ways to do this, try one of these:
fill_in('course_title', :with => 'a safer workplace')
find("input#course_title]").set("a safer workplace")
find(:xpath, "//input[#id='course_title']").set("a safer workplace")

Related

Cabybara find element based on element name

Hi i am trying to select input field and add value into stripe's iframe, based on element name.
this won't work
page.find(:name, 'exp-date')
i was able to select root div with id selector, but i can't select input field which is child node somewhere in root div
find(:id, "root")
Any idea how to target those fields
Use the capybara find_field method to find field inputs. Checkout the docs here:
http://www.rubydoc.info/github/jnicklas/capybara/Capybara/Node/Finders#find_field-instance_method
Would look something like: find_field('attributes[form_field_attribute_you_need]')
Not sure if this works with an iframe, but you should be able to use:
within('#target_form_id') do #or another selector
fill_in 'field_name_or_id', with: 'value'
fill_in 'another_field', with: 'another_value'
end
You can use a CSS selector here:
find('input[name="exp-date"]', match: :first).set('my value')

Is it possible to fill_in a number field wo/ labels with Capybara?

I have a number field, that takes a :product_quantity with a button after it saying "Add to Cart". My test works without adjusting the number_field but when I uncomment this line:
product_from_shop.fill_in 'quantity', with: 5
Stuff blows up. Capybara::ElementNotFound
It's about this line in my view:
<%= f.number_field :product_quantity, value: booking.product_quantity, min: 1, max: 999, title: 'quantity' %>
I tried several approaches, also searching for the class or id. Non work. There is no label, that would clutter my view.
title worked fine in other cases, without a form.
Also: I am using capybara webkit, because it uses JS.
fill_in uses the :fillable_field selector which will find any visible input or textarea area element except of type submit, image, radio, checkbox, or file. It will find those elements id, name or placeholder attributes or by the text in an associated label element. Since you don't have an associated label you're down to id, name or placeholder attribute. Assuming product_from_shop is a wrapper element on the page you're using to scope this fill_in then
product_from_shop.fill_in '<something>_product_quantity', with: 5
should find and fill_in the element by id - will depend on what model type the form is for (just look at the actual HTML to see what id was assigned).
If the product quantity field is the only fillable field inside product_from_shop you could also do
product_from_shop.fill_in with: 5 # same as product_from_shop.fill_in nil, with: 5
If none of those will work for you, you can move to using something like
product_from_shop.find(<more complicated query>).set '5'
To clarify your comment about title working previously - The title attribute is only matched when finding links and buttons.
Assuming you are using form_for in Rails for this, then it should be generating an id that you can use. I am not familiar with the product_from_shop.fill_in form, I typically use page, but try one of these:
page.fill_in :product_from_shop_product_quantity, with: 5
or
page.fill_in "product_from_shop_product_quantity", with: 5

Capybara fill_in for HTML5 input range

I'm implementing a basic slider using the new HTML5 input type of Range.
No javascript or CSS involved.
Here's an example HTML5 input-type Range
It used to be a text box so in Capybara I could just say
fill_in "rating_scale", with: "57"
Now I need a way to tell it to fill_in the range or to click and move the range's thumb.
Has anyone done this successfully?
That is what I ended up doing...
find(:xpath, "//input[#id='rating_scale']").set 57
I assume that the slider is using Javascript so you will need a driver for Capybara to interact with Javascript, like Poltergeist.
here is a capybara helper to change an input range based on it's name,
assuming you use Poltergeist:
def range_select(name, value)
selector = %-input[type=range][name=\\"#{name}\\"]-
script = %-$("#{selector}").val(#{value})-
page.execute_script(script)
end
There is a monkey-patched drag_by implementation which could work very well for this if you are using a javascript rangeslider (although it doesn't set a specific value unfortunately, just drags by a specific amount).

How do I submit a form with a particular field?

Need to select and submit a form on a page containing many different forms, which has a hidden field with a particular value.
I know there's form.fields_with() to select form fields, and page.form_with(), to select forms with particular attributes, but I want to select a form which has a hidden field with the value attribute 'xxxxx', for example.
Is there a way of doing this in Mechanize? Or am I stuck using xpath or a hack solution? The XPath for what I want is
xpath("//form[div/input/#value='xxx']").click_button
Of course I cannot click_button on an xpath, though.
You could do:
page.form_with :form_node => page.at(xpath)

How do I check the value inside a text feild while using ruby selenium testing?

I am running a selenium test using ruby and was wondering how I can assert the value inside a text field?
I have a page where once it's loaded has text inside the editable text field and I was wondering how I can check if the text is present?
assert_equal "myValue", #selenium.get_value("id=myField")

Resources