Watir, Rspec, and radio buttons.

278 views
Skip to first unread message

James

unread,
Feb 9, 2010, 10:58:18 AM2/9/10
to Watir General
I'm trying to check if a radio button is checked or not, using the
Rspec "should" command, but it's not acting as I would expect.

My code:

puts questionframe.radio(:id, "rlbYesNo_0").checked?
questionframe.radio(:id, "rlbYesNo_0").checked? should equal true

It outputs "true" for the first line, but for the second line, it
gives this error:

expected #<TrueClass:2> => true
got #<String:103356170> => "Yesno"

It does return true when I print it to the screen, but when using
"should" to check it, it somehow thinks it returns the text "Yesno".

Can anyone explain what's going on and what I can do to fix this?

Thanks,
James

Charley Baker

unread,
Feb 9, 2010, 11:23:15 AM2/9/10
to watir-...@googlegroups.com
Wrong matcher, use be_true instead:

questionframe.radio(:id, "rlbYesNo_0").checked? should be_true



-Charley
Lead Developer, Watir, http://watir.com



--
You received this message because you are subscribed to the Google Groups "Watir General" group.
To post to this group, send email to watir-...@googlegroups.com
Before posting, please read the following guidelines: http://wiki.openqa.org/display/WTR/Support
To unsubscribe from this group, send email to watir-genera...@googlegroups.com
For more options, visit this group at http://groups.google.com/group/watir-general

James

unread,
Feb 9, 2010, 11:48:43 AM2/9/10
to Watir General
Hmm, that's not working for me.

I have:
questionframe.radio(:id, "rlbYesNo_0").checked? should(be_true)

(I also tried it without parentheses and got the same error, plus a
warning that I should have parentheses.)

This is the output:
1)
ArgumentError in 'Yesno should answer Yes on first question'
wrong number of arguments (1 for 0)
yesno_spec.rb:45:in `checked?'
yesno_spec.rb:45:
yesno_spec.rb:9:


If I check for "false", I get a similar error as before:
I have:
questionframe.radio(:id, "rlbYesNo_0").checked? should(be_false)

My output:
2)
'Yesno should change answer from Yes to no' FAILED
expected false, got "Yesno"
yesno_spec.rb:54:
yesno_spec.rb:9:


Any ideas?


On Feb 9, 11:23 am, Charley Baker <charley.ba...@gmail.com> wrote:
> Wrong matcher, use be_true instead:
>
> questionframe.radio(:id, "rlbYesNo_0").checked? should be_true
>
> -Charley

> Lead Developer, Watir,http://watir.com


>
> On Tue, Feb 9, 2010 at 8:58AM, James <jgcpal...@gmail.com> wrote:
> > I'm trying to check if a radio button is checked or not, using the
> > Rspec "should" command, but it's not acting as I would expect.
>
> > My code:
>
> >    puts questionframe.radio(:id, "rlbYesNo_0").checked?
> >    questionframe.radio(:id, "rlbYesNo_0").checked? should equal true
>
> > It outputs "true" for the first line, but for the second line, it
> > gives this error:
>
> > expected #<TrueClass:2> => true
> >     got #<String:103356170> => "Yesno"
>
> > It does return true when I print it to the screen, but when using
> > "should" to check it, it somehow thinks it returns the text "Yesno".
>
> > Can anyone explain what's going on and what I can do to fix this?
>
> > Thanks,
> > James
>
> > --
> > You received this message because you are subscribed to the Google Groups
> > "Watir General" group.
> > To post to this group, send email to watir-...@googlegroups.com
> > Before posting, please read the following guidelines:
> >http://wiki.openqa.org/display/WTR/Support
> > To unsubscribe from this group, send email to

> > watir-genera...@googlegroups.com<watir-general%2Bunsu...@googlegroups.com>

Charley Baker

unread,
Feb 9, 2010, 12:13:23 PM2/9/10
to watir-...@googlegroups.com
Oopsy daisy. I left out a dot. Note the dot before should.


questionframe.radio(:id, "rlbYesNo_0").checked?.should be_true


that.should work :)

-c

James

unread,
Feb 9, 2010, 12:21:43 PM2/9/10
to Watir General
That worked perfectly, thanks!

James

On Feb 9, 12:13 pm, Charley Baker <charley.ba...@gmail.com> wrote:
> Oopsy daisy. I left out a dot. Note the dot before should.
>
> questionframe.radio(:id, "rlbYesNo_0").checked?.should be_true
>
> that.should work :)
>
> -c
>

> On Tue, Feb 9, 2010 at 9:48AM, James <jgcpal...@gmail.com> wrote:
> > Hmm, that's not working for me.
>
> > I have:
> > questionframe.radio(:id, "rlbYesNo_0").checked? should(be_true)
>
> > (I also tried it without parentheses and got the same error, plus a
> > warning that I should have parentheses.)
>
> > This is the output:
> > 1)
> > ArgumentError in 'Yesno should answer Yes on first question'
> > wrong number of arguments (1 for 0)
> > yesno_spec.rb:45:in `checked?'
> > yesno_spec.rb:45:
> > yesno_spec.rb:9:
>
> > If I check for "false", I get a similar error as before:
> > I have:
> > questionframe.radio(:id, "rlbYesNo_0").checked? should(be_false)
>
> > My output:
> > 2)
> > 'Yesno should change answer from Yes to no' FAILED
> > expected false, got "Yesno"
> > yesno_spec.rb:54:
> > yesno_spec.rb:9:
>
> > Any ideas?
>

> > <watir-general%2Bunsu...@googlegroups.com<watir-general%252Buns...@googlegroups.com>

marekj

unread,
Feb 9, 2010, 12:39:20 PM2/9/10
to watir-...@googlegroups.com
On Tue, Feb 9, 2010 at 11:21 AM, James <jgcp...@gmail.com> wrote:
> That worked perfectly, thanks!
>
> James
>
> On Feb 9, 12:13 pm, Charley Baker <charley.ba...@gmail.com> wrote:
>> Oopsy daisy. I left out a dot. Note the dot before should.
>>
>> questionframe.radio(:id, "rlbYesNo_0").checked?.should be_true
>>
>>

I vote for:
questionframe.radio(:id, "rlbYesNo_0").should be_checked
(or radio.should be_set)
to negate
questionframe.radio(:id, "rlbYesNo_0").should_not be_checked

more BDD flavor in this one.
the matcher will call .checked? method automatically part of a be_ pattern
I think any predicate in watir can be used in rspec, so exists? can e
queried with element.should exist (Bret has fixed that)
and include? will be should include('some value')

more about predicates in rspec here
http://rspec.rubyforge.org/rspec/1.3.0/classes/Spec/Matchers.html


marekj

Watirloo: Semantic Page Objects in UseCases
http://github.com/marekj/watirloo/
Support Watir Project http://pledgie.com/campaigns/2982

Charley Baker

unread,
Feb 9, 2010, 1:22:26 PM2/9/10
to watir-...@googlegroups.com
Actually that's a good point, I definitely agree it's cleaner syntax. We use that same syntax all over, rarely check for checked, but certainly use it for exists, include, displayed, etc.

Follow Marek's advice. :)

-c

--
You received this message because you are subscribed to the Google Groups "Watir General" group.
To post to this group, send email to watir-...@googlegroups.com
Before posting, please read the following guidelines: http://wiki.openqa.org/display/WTR/Support
To unsubscribe from this group, send email to watir-genera...@googlegroups.com

James

unread,
Feb 9, 2010, 3:21:32 PM2/9/10
to Watir General
Thanks guys!

I'm having similar issues trying to match the value of a text field.

textfield.value should equal "test"

gives similar problems I was having before. What should I be using?
And is there a list somewhere that I can find all these matchers? I'm
having trouble finding a comprehensive resource on this.

Thanks,
James


On Feb 9, 1:22 pm, Charley Baker <charley.ba...@gmail.com> wrote:
> Actually that's a good point, I definitely agree it's cleaner syntax. We use
> that same syntax all over, rarely check for checked, but certainly use it
> for exists, include, displayed, etc.
>
> Follow Marek's advice. :)
>
> -c
>

> On Tue, Feb 9, 2010 at 10:39AM, marekj <marekj....@gmail.com> wrote:


> > On Tue, Feb 9, 2010 at 11:21AM, James <jgcpal...@gmail.com> wrote:
> > > That worked perfectly, thanks!
>
> > > James
>

> > > On Feb 9, 12:13pm, Charley Baker <charley.ba...@gmail.com> wrote:
> > >> Oopsy daisy. I left out a dot. Note the dot before should.
>
> > >> questionframe.radio(:id, "rlbYesNo_0").checked?.should be_true
>
> > I vote for:
> > questionframe.radio(:id, "rlbYesNo_0").should be_checked
> > (or radio.should be_set)
> > to negate
> > questionframe.radio(:id, "rlbYesNo_0").should_not be_checked
>
> > more BDD flavor in this one.
> > the matcher will call .checked? method automatically part of a be_ pattern
> > I think any predicate in watir can be used in rspec, so exists? can e
> > queried with element.should exist (Bret has fixed that)
> > and include? will be should include('some value')
>
> > more about predicates in rspec here
> >http://rspec.rubyforge.org/rspec/1.3.0/classes/Spec/Matchers.html
>
> > marekj
>
> > Watirloo: Semantic Page Objects in UseCases
> >http://github.com/marekj/watirloo/

> > Support Watir Projecthttp://pledgie.com/campaigns/2982


>
> > --
> > You received this message because you are subscribed to the Google Groups
> > "Watir General" group.
> > To post to this group, send email to watir-...@googlegroups.com
> > Before posting, please read the following guidelines:
> >http://wiki.openqa.org/display/WTR/Support
> > To unsubscribe from this group, send email to

> > watir-genera...@googlegroups.com<watir-general%2Bunsu...@googlegroups.com>

James

unread,
Feb 9, 2010, 3:46:16 PM2/9/10
to Watir General
Ok, solved it myself:

textfield.value.should == "test"

is what I needed to do.

Alan Baird

unread,
Feb 9, 2010, 4:26:09 PM2/9/10
to watir-...@googlegroups.com

So charley, does that mean that the rspec predicate matcher for exists? would be ...
obj.should be_exists ?

On Feb 9, 2010 2:46 PM, "James" <jgcp...@gmail.com> wrote:

Ok, solved it myself:

textfield.value.should == "test"

is what I needed to do.


On Feb 9, 3:21 pm, James <jgcpal...@gmail.com> wrote:
> Thanks guys!
>

> I'm having similar issues ...

James

unread,
Feb 9, 2010, 4:52:23 PM2/9/10
to Watir General
According to http://rspec.rubyforge.org/rspec/1.1.9/classes/Spec/Matchers.html
it would just be

obj.should exist

On Feb 9, 4:26 pm, Alan Baird <aba...@bairdsnet.net> wrote:
> So charley, does that mean that the rspec predicate matcher for exists?
> would be ...
> obj.should be_exists ?
>

> On Feb 9, 2010 2:46PM, "James" <jgcpal...@gmail.com> wrote:
>
> Ok, solved it myself:
>
> textfield.value.should == "test"
>
> is what I needed to do.
>

Alan Baird

unread,
Feb 9, 2010, 5:54:15 PM2/9/10
to watir-...@googlegroups.com
>According to >http://rspec.rubyforge.org/rspec/1.1.9/classes/Spec/Matchers.html
>it would just be
>
>obj.should exist

James - I agree that it should be this way, but the way I'm reading this line leads me to believe otherwise:

" All you need to do is write +should be_+ followed by the predicate without the question mark, and RSpec will figure it out from there. For example:

[].should be_empty => [].empty? #passes
[].should_not be_empty => [].empty? #fails"

Am I missing something?

Alan

This email message and any attachments are for the sole use of the intended recipients and may contain proprietary and/or confidential information which may be privileged or otherwise protected from disclosure. Any unauthorized review, use, disclosure or distribution is prohibited. If you are not an intended recipient, please contact the sender by reply email and destroy the original message and any copies of the message as well as any attachments to the original message.

marekj

unread,
Feb 9, 2010, 7:33:21 PM2/9/10
to watir-...@googlegroups.com
On Tue, Feb 9, 2010 at 4:54 PM, Alan Baird <alan....@riskmetrics.com> wrote:
>>According to >http://rspec.rubyforge.org/rspec/1.1.9/classes/Spec/Matchers.html
>>it would just be
>>
>>obj.should exist
>
> James - I agree that it should be this way, but the way I'm reading this line leads me to believe otherwise:
>
> " All you need to do is write +should be_+ followed by the predicate without the question mark, and RSpec will figure it out from there. For example:
>
>  [].should be_empty => [].empty? #passes
>  [].should_not be_empty => [].empty? #fails"
>
> Am I missing something?
>
> Alan

Alan, the matchers are malleable and usually written in a way a person
would talk.
empty? => should be_empty (because some thing is empty)
exists? => should exist (becuase there is no need to force be_ here)

Rspec gives you a mechanism to define your matchers:
so if you have a method like object.blinks? that returns true or false
you can make a matcher

object.should blink
or
object.should_not blink

I did mention in my earlier post that element.should exist was a fix
to rspec contributed by Bret a while back (too lazy to find the
relevant code)

Alan Baird

unread,
Feb 10, 2010, 1:38:01 PM2/10/10
to watir-...@googlegroups.com

Marek-thanks...I got it to work as you suggested...exists and even include work for me without using '_be'

Alan

On Feb 9, 2010 6:33 PM, "marekj" <marekj.com@gmail.com> wrote:

On Tue, Feb 9, 2010 at 4:54 PM, Alan Baird <alan....@riskmetrics.com> wrote:

>>According to >http:...

Alan, the matchers are malleable and usually written in a way a person
would talk.
empty? => should be_empty  (because some thing is empty)
exists? => should exist (becuase there is no need to force be_ here)

Rspec gives you a mechanism to define your matchers:
so if you have a method like object.blinks? that returns true or false
you can make a matcher

object.should blink
or
object.should_not blink

I did mention in my earlier post that element.should exist was a fix
to rspec contributed by Bret a while back (too lazy to find the
relevant code)




marekj

Watirloo: Semantic Page Objects in UseCases
http://github.com/marekj/watirloo/

Support Watir Project http://pledgie.com/campaigns/2982

--

You received this message because you are subscribed to the Google Groups "Watir General" group.

To ...

Jarmo Pertman

unread,
Feb 11, 2010, 7:17:54 AM2/11/10
to Watir General
Another cool thing with RSpec is the "has" methods. So, for example:

class Person
def has_brain?
true
end
end

person = Person.new
person.should have_brain

Neat, eh?

That's the beauty of RSpec (and other similar DSL-s) and that's why i
have been using RSpec instead of Test::Unit for a long time already.

Jarmo

On Feb 10, 8:38 pm, Alan Baird <aba...@bairdsnet.net> wrote:
> Marek-thanks...I got it to work as you suggested...exists and even include
> work for me without using '_be'
>
> Alan
>

> On Feb 9, 2010 6:33 PM, "marekj" <marekj....@gmail.com> wrote:
>
> On Tue, Feb 9, 2010 at 4:54 PM, Alan Baird <alan.ba...@riskmetrics.com>


> wrote:>>According to >http:...
>
> Alan, the matchers are malleable and usually written in a way a person
> would talk.
> empty? => should be_empty  (because some thing is empty)
> exists? => should exist (becuase there is no need to force be_ here)
>
> Rspec gives you a mechanism to define your matchers:
> so if you have a method like object.blinks? that returns true or false
> you can make a matcher
>
> object.should blink
> or
> object.should_not blink
>
> I did mention in my earlier post that element.should exist was a fix
> to rspec contributed by Bret a while back (too lazy to find the
> relevant code)
>
> marekj
>
> Watirloo: Semantic Page Objects in UseCaseshttp://github.com/marekj/watirloo/
>

> Support Watir Projecthttp://pledgie.com/campaigns/2982

Reply all
Reply to author
Forward
0 new messages