Create `have_disabled_field` matcher #982

Closed
sobrinho opened this Issue Feb 27, 2013 · 10 comments

Comments

Projects
None yet
4 participants
@sobrinho
Contributor

sobrinho commented Feb 27, 2013

Hi,

What do you think about creating a have_disabled_field matcher?

I often create it in my applications and maybe it fits on capybara by default since it's pretty common.

An example of usage:

fill_in 'Value', with: '100.00'
fill_in 'Discount', with: '10.00%'

expect(page).to have_disabled_field 'Total', with: '90.00'

The total field must not be editable and must have the value 90.00 in this example.

I have this one:

module Application                                                                                                                                                   
  module Matchers                                                                                                                                             
    extend RSpec::Matchers::DSL                                                                                                                               

    matcher :have_disabled_field do |name, options|                                                                                                           
      match do |page|                                                                                                                                         
        selector = XPath.descendant(:input)[XPath.attr(:id).equals(XPath.anywhere(:label)[XPath.string.n.contains(name)].attr(:for))]                         

        field = page.find(:xpath, selector)                                                                                                                   

        expect(field[:disabled]).to eq 'disabled'                                                                                                             

        if options && options.key?(:with)                                                                                                                     
          expect(field[:value]).to eq options[:with]                                                                                                          
        end                                                                                                                                                   
      end                                                                                                                                                     

      failure_message_for_should do |page|                                                                                                                    
        "expected page to have disabled field #{name.inspect}"                                                                                                
      end                                                                                                                                                     

      failure_message_for_should_not do |page|                                                                                                                
        "expected page not to have disabled field #{name.inspect}"                                                                                            
      end                                                                                                                                                     
    end                                                                                                                                                       
  end                                                                                                                                                         
end                                                                                                                                                           

RSpec.configure do |config|                                                                                                                                   
  config.include Application::Matchers, :type => :feature                                                                                                            
end

It is not strictly and the error message is useless but it's just an idea.

I would be happy to work in a pull request to add it but I'm not sure where to put it (capybara or XPath::HTML.disabled_field).

An second implementation would be an option on have_field:

fill_in 'Value', with: '100.00'
fill_in 'Discount', with: '10.00%'

expect(page).to have_field 'Total', disabled: true, with: '90.00'

I prefer the latter but it requires more effort since XPath::HTML.field ignores disabled elements.

@jnicklas

This comment has been minimized.

Show comment Hide comment
@jnicklas

jnicklas Feb 27, 2013

Collaborator

We'll probably make :disabled a filter, so that you can do: have_field("Blah", :disabled => true).

Collaborator

jnicklas commented Feb 27, 2013

We'll probably make :disabled a filter, so that you can do: have_field("Blah", :disabled => true).

@jnicklas

This comment has been minimized.

Show comment Hide comment
@jnicklas

jnicklas Feb 27, 2013

Collaborator

Any work towards that would be excellent, see #948.

Collaborator

jnicklas commented Feb 27, 2013

Any work towards that would be excellent, see #948.

@sobrinho

This comment has been minimized.

Show comment Hide comment
@sobrinho

sobrinho Feb 27, 2013

Contributor

@jnicklas I should remove this line? https://github.com/jnicklas/xpath/blob/master/lib/xpath/html.rb#L172

I'm a bit afraid to remove it since it will change the currently behavior of xpath, regardless of the behavior of capybara.

Contributor

sobrinho commented Feb 27, 2013

@jnicklas I should remove this line? https://github.com/jnicklas/xpath/blob/master/lib/xpath/html.rb#L172

I'm a bit afraid to remove it since it will change the currently behavior of xpath, regardless of the behavior of capybara.

@jnicklas

This comment has been minimized.

Show comment Hide comment
@jnicklas

jnicklas Feb 27, 2013

Collaborator

I'm cool with breaking compatibility in the XPath gem. We'll do a major release, just in case.

Collaborator

jnicklas commented Feb 27, 2013

I'm cool with breaking compatibility in the XPath gem. We'll do a major release, just in case.

@sobrinho

This comment has been minimized.

Show comment Hide comment
@sobrinho

sobrinho Feb 28, 2013

Contributor

Great!

I will work on this pull request tomorrow :)

Contributor

sobrinho commented Feb 28, 2013

Great!

I will work on this pull request tomorrow :)

@jnicklas jnicklas referenced this issue Mar 6, 2013

Closed

Release Capybara 2.1 #984

8 of 8 tasks complete
@grzuy

This comment has been minimized.

Show comment Hide comment
@grzuy

grzuy Mar 8, 2013

@sobrinho i like the idea!
Are you working on this already? If so looking forward to it :)
In case not, i probably can help working on a patch for this. Let me know.

grzuy commented Mar 8, 2013

@sobrinho i like the idea!
Are you working on this already? If so looking forward to it :)
In case not, i probably can help working on a patch for this. Let me know.

@sobrinho

This comment has been minimized.

Show comment Hide comment
@sobrinho

sobrinho Mar 9, 2013

Contributor

@grzuy nope, I was pretty busy this week.

Seems like @jnicklas already started to work on this feature :)

Contributor

sobrinho commented Mar 9, 2013

@grzuy nope, I was pretty busy this week.

Seems like @jnicklas already started to work on this feature :)

@grzuy

This comment has been minimized.

Show comment Hide comment
@grzuy

grzuy Mar 9, 2013

i see, awesome! :)

grzuy commented Mar 9, 2013

i see, awesome! :)

@jnicklas jnicklas closed this in 9f516cf Mar 16, 2013

@sobrinho

This comment has been minimized.

Show comment Hide comment
@sobrinho

sobrinho Mar 16, 2013

Contributor

Thanks @jnicklas! ❤️

Contributor

sobrinho commented Mar 16, 2013

Thanks @jnicklas! ❤️

@sigu

This comment has been minimized.

Show comment Hide comment
@sigu

sigu Jul 2, 2015

I am late to this party but thanks @jnicklas for this feature

sigu commented Jul 2, 2015

I am late to this party but thanks @jnicklas for this feature

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment