Search code examples
ruby-on-railscapybara

How to get placeholder value using assert_selector in rails


How do I get the placeholder value from an input bar using assert_selector in rails ?

<input placeholder="hello world"  value="<%= params[:query] %>" required>

I want to check is placeholder has hello in it


Solution

  • you could use xpath and verify that there's an input element whose's placeholder contains 'hello'

    assert_selector(:xpath, './/input[contains(@placeholder,"hello")]')
    

    if you want to check exactly an input with id

    assert_selector(:xpath, './/input[@id="input_id"][contains(@placeholder,"hello")]')
    

    im not sure there's a helper method that check regex something like ~= /.*hello.*/, if you want to do that, you still have another choice

    placeholder = find('#input_id')['placeholder'] # or use :xpath
    assert_match /.*hello.*/, placeholder