0

Thing I want to do

  I want to assign a value to the value of the iput tag of the hidden attribute as shown below in SystemSpec.

     <input id = "review_rating" type = "hidden" name = "review[rating]">

What I tried

  • Set the visible attribute to false.
    find('#review_rating', visible:false).set('5')
  • Try name attribute instead of id as the first argument
    find('input[name="review[rating]"]', visible:false).set('5')

If you write first option , FeatureSepc will pass the test.

  • 1
    Having two ids on the same page of the same value, in this case `review_rating` will cause issues. Id's are supposed to be unique per page(only one per page). Also, the convention for html/css properties is to use kabob-case, ie, `review-rating` – Int'l Man Of Coding Mystery Dec 20 '19 at 09:04
  • you can target the type via `$("#review_rating[type='hidden']").val(5)` [Example](https://jsfiddle.net/bn5ga24o/) – engineersmnky Dec 20 '19 at 15:43

1 Answers1

0

The issue is you shouldn't be able to update a hidden field via conventional behaviour, so it's disallowed. See the discussion here for more info.

The question is why you want to test this - is the hidden field manipulated by another element on the page? If so, find and interact (click?) that.

On the other hand, using xpath selectors / Capybara's first method is meant to work for this:

find(:xpath, "//input[name="review[rating]"]").set("5")
first('input[name="review[rating]"]', visible: false).set("5")

...but that would likely mean tweaking your code to ensure the selector is unique, as suggested in Mike Heft's comment.

Otherwise, you can use plain old JS to solve the problem. See here for ideas for selecting hidden elements using JS, though again, perhaps best to restructure with a unique selector:

page.execute_script("document.querySelector('input[name="review[rating]"]').value = '5'");

Hope something in here helps - let me know how you get on :)

SRack
  • 11,495
  • 5
  • 47
  • 60
  • 1
    Thank you. I have solved the problem. The things you thought me, I should why I write test code. I will think again about this. – Takaya Sugiyama Dec 21 '19 at 01:19