Using Capybara how would I would I overwrite an ex

2019-05-26 17:23发布

I am currently testing a form on a page with Capybara that has existing values filled out in the form's fields. I want to test being able to put new values into the fields and submitting the form with the new values.

When I try using Capybara's fill_in method it ignores the fields that have existing values. Is there anyway to overwrite fields with existing values in Capybara?

I'll include the form below:

<form accept-charset="UTF-8" action="/submissions/1/reviews/4" class=
  "simple_form edit_review" id="edit_review_4" method="post" novalidate="novalidate"
  name="edit_review_4">
    <div style="margin:0;padding:0;display:inline">
      <input name="utf8" type="hidden" value="&#10003;"><input name="_method" type=
      "hidden" value="put"><input name="authenticity_token" type="hidden" value=
      "gJ/KKAodeIJD8PPnRNeN4GaGb/yqvUDHrsnl9LqLP/c=">
    </div>

    <div class="input integer optional">
      <label class="integer optional control-label" for=
      "review_rating">Rating</label><input class="numeric integer optional" id=
      "review_rating" name="review[rating]" step="1" type="number" value="33">
    </div>

    <div class="input text required">
      <label class="text required control-label" for="review_comment"><abbr title=
      "required">*</abbr> Comment</label>
      <textarea class="text required" cols="40" id="review_comment" name=
      "review[comment]" rows="20">
hellofsdf
</textarea>
    </div><input class="btn" name="commit" type="submit" value="Update Review">
  </form>

The Capybara code I've tried is:

find(:xpath, "//*[(@id = 'review_comment')]").set "hello"

I've also tried a similar approach with the rating field since the rating is a text field and the comment is a text area, but I still can't get it to change.

2条回答
男人必须洒脱
2楼-- · 2019-05-26 17:57

using find with XSLT expression should do the trick

find(:xpath, "//input[@id='field_with_default_value_id']").set "my value"
查看更多
虎瘦雄心在
3楼-- · 2019-05-26 18:14

Use a javascript driver like phantomjs/poltergeist and then use javascript to set values like that.

page.evaluate_script("document.getElementById('#some-id').value = 'some-value'");

Similar to my answer on this question.

查看更多
登录 后发表回答