step definition to test the existence of an element ...

1,172 views
Skip to first unread message

Nellboy

unread,
Dec 12, 2010, 10:00:31 AM12/12/10
to Cukes
Hi

I would like to test the existence of an element on the page ...

e.g:

Then the element ".form" should exist

could anyone point me in the right direction ?

thanks

Paul

Nellboy

unread,
Dec 12, 2010, 10:02:01 AM12/12/10
to Cukes
Just as I posted, I found this:

http://makandra.com/notes/831-test-that-a-css-selector-is-present-with-cucumber

is this the best possible way?

cheers

Paul

Nellboy

unread,
Dec 12, 2010, 10:12:12 AM12/12/10
to Cukes
ok, much simpler solution. Apologies for all the posts, still
learning here:

Then /^the element "([^"]*)" should exist$/ do |element|
response.should have_selector(element)
end

Then /^the element "([^"]*)" should not exist$/ do |element|
response.should_not have_selector(element)
end



On Dec 12, 3:02 pm, Nellboy <nell...@gmail.com> wrote:
> Just as I posted,  I found this:
>
> http://makandra.com/notes/831-test-that-a-css-selector-is-present-wit...

Aslak Hellesøy

unread,
Dec 12, 2010, 10:38:07 AM12/12/10
to cu...@googlegroups.com
On Dec 12, 2010, at 4:12 PM, Nellboy <nel...@gmail.com> wrote:

> ok, much simpler solution. Apologies for all the posts, still
> learning here:
>
> Then /^the element "([^"]*)" should exist$/ do |element|
> response.should have_selector(element)
> end
>
> Then /^the element "([^"]*)" should not exist$/ do |element|
> response.should_not have_selector(element)
> end
>
>

Is this the abstraction level in your steps? It sounds kind of nerdy
and elaborate ;-)

How about Then I should see "OMFG"

Using Gherkin as a dumb api-to-human translator leads to test scripts
(features and scenarios) that are not only horrible to read (and
type), but also very hard to maintain, as a minor CSS tweak now fails
half of your cukes.

Aslak

>
> On Dec 12, 3:02 pm, Nellboy <nell...@gmail.com> wrote:
>> Just as I posted, I found this:
>>
>> http://makandra.com/notes/831-test-that-a-css-selector-is-present-wit...
>>
>> is this the best possible way?
>>
>> cheers
>>
>> Paul
>>
>> On Dec 12, 3:00 pm, Nellboy <nell...@gmail.com> wrote:
>>
>>
>>
>>
>>
>>
>>
>>> Hi
>>
>>> I would like to test the existence of an element on the page ...
>>
>>> e.g:
>>
>>> Then the element ".form" should exist
>>
>>> could anyone point me in the right direction ?
>>
>>> thanks
>>
>>> Paul
>

> --
> You received this message because you are subscribed to the Google Groups "Cukes" group.
> To post to this group, send email to cu...@googlegroups.com.
> To unsubscribe from this group, send email to cukes+un...@googlegroups.com.
> For more options, visit this group at http://groups.google.com/group/cukes?hl=en.
>

Chuck van der Linden

unread,
Dec 13, 2010, 2:35:36 PM12/13/10
to Cukes
without knowing what you are using to drive or interact with the
browser, it's hard to say what's your best way here..

For example if you were using Watir, the the guts of the step might
look like

browser.form(:how, what).should exist

(how and what are how to identify the element, such
as :id, :class, :name, or :index for the :how; and an appropriate
value for the 'what' )
> > > Paul- Hide quoted text -
>
> - Show quoted text -

Paul Nelligan

unread,
Dec 13, 2010, 5:03:35 PM12/13/10
to cu...@googlegroups.com
Aslak,  is this any different to posting ... 'Then I should see 'some text' within 'some selector' ?

this is contained within web_steps.rb for cucumber with Rails.  People far more experienced in these matters than me wrote this,  so I'm not going to question it.

I'd like to get your opinions here though

cheers

Rob Hunter

unread,
Dec 13, 2010, 5:48:07 PM12/13/10
to cu...@googlegroups.com, Cukes


For Capybara, the guts of the step definition would look like:
page.should have_css(".form")


Like Chuck says, it depends on the technology you're using to interact with the Web.

Cucumber itself doesn't know anything about web pages, HTML or selectors. For that, you'll want software like Capybara or WebRat. I like Capybara.

Like Aslak says, Cucumber is just there as thin translation layer between your specific domain language and Ruby code.

The feature side should use language like:
When I place an order for "My First e-Book"
Then the shipping charge on my order should be $0.00

It's the step definition's job to define these human steps in terms of selectors, events and HTML assertions.

It takes some time to get used to, and there are a lot of bad examples out there -- the web_steps that the cucumber-rails bridge generates are famous for setting people off in the wrong direction.

Good luck!

-- 
Rob Hunter
(Sent from my phone)

Matt Wynne

unread,
Dec 13, 2010, 6:08:43 PM12/13/10
to cu...@googlegroups.com
On 13 Dec 2010, at 22:48, Rob Hunter wrote:



For Capybara, the guts of the step definition would look like:
page.should have_css(".form")


Like Chuck says, it depends on the technology you're using to interact with the Web.

Cucumber itself doesn't know anything about web pages, HTML or selectors. For that, you'll want software like Capybara or WebRat. I like Capybara.

Like Aslak says, Cucumber is just there as thin translation layer between your specific domain language and Ruby code.

The feature side should use language like:
When I place an order for "My First e-Book"
Then the shipping charge on my order should be $0.00

It's the step definition's job to define these human steps in terms of selectors, events and HTML assertions.

It takes some time to get used to, and there are a lot of bad examples out there -- the web_steps that the cucumber-rails bridge generates are famous for setting people off in the wrong direction.


cheers,
Matt

07974 430184

aslak hellesoy

unread,
Dec 13, 2010, 6:21:29 PM12/13/10
to cu...@googlegroups.com
On Mon, Dec 13, 2010 at 10:03 PM, Paul Nelligan <nel...@gmail.com> wrote:
> Aslak,  is this any different to posting ... 'Then I should see 'some text'
> within 'some selector' ?

Not really - they are both equally bad.

> this is contained within web_steps.rb for cucumber with Rails.  People far
> more experienced in these matters than me wrote this,  so I'm not going to
> question it.

How embarassing, I apologise. Maybe I've been too liberal accepting
patches. Or maybe I realised how bad this is after I accepted it,
that's more likely.

Anyway, I'll yank it from a future release so people don't get tempted
to write CSS selectors in their features (what a crazy idea). There is
a patch floating around that allows you to map a "named" UI element to
a selector. -Similar to how we refer to pages in When /I go to (.*)/.

Reply all
Reply to author
Forward
0 new messages