Selenium. Cucumber. Ruby. Constants / Variables inside Feature file.

492 views
Skip to first unread message

Vlad Wbox

unread,
Jan 9, 2015, 5:52:21 PM1/9/15
to cu...@googlegroups.com
Using WebDriver with Cucumber, with Ruby.

I have this step inside my feature file:
Then I click the category Age
I have this code in steps.rb file

Then /^Click the category ([^"]*)$/ do |x|

$driver.get "https://www.example.com"

element = $driver.find_element :xpath => "//a[@class='mainNavLink' and text() =  '#{x}']"

element.click

end


So this way if I want to click any other category, I don't need to write another step definition for that category. I just write new step

Then I click the category Height
The problem here is the following. Driver is taking "Age" string from step in feature file and looking for element with that text. But what if the text changes later? 
Let's say it will become "How old". Then I will have to rewrite all steps that click that category.

So I want to make Age a constant. I define it in a separate file (elements.rb) as:

AgeCategory = "Age"

And I now write my feature file as this

Then I click the category AgeCategory
This way if text "Age" changes, I will only have to change the definition of my constant in elements.rb file.

But if I try to execute this now, the code pick up the string value "AgeCategory" and not the value "Age" of the Constant "AgeCategory".

Any solutions for that?
Thx!


Matthew Metzger

unread,
Jan 10, 2015, 12:12:33 PM1/10/15
to cu...@googlegroups.com
I recommend using the site prism gem for this: https://github.com/natritmeyer/site_prism

Site prism will allow you to create a class for your page, and element mappings reside in the class. You reference or anywhere in your step definitions you need, and have a single place to update it if anything changes, without having to come up with all sorts of constants as you proposed.

Rob Park

unread,
Jan 11, 2015, 12:16:23 PM1/11/15
to cu...@googlegroups.com
On Sat, Jan 10, 2015 at 12:12 PM, Matthew Metzger <matthew....@gmail.com> wrote:
I recommend using the site prism gem for this: https://github.com/natritmeyer/site_prism

Site prism will allow you to create a class for your page, and element mappings reside in the class. You reference or anywhere in your step definitions you need, and have a single place to update it if anything changes, without having to come up with all sorts of constants as you proposed.

Or you could avoid Capybara and just use the page-object gem (https://github.com/cheezy/page-object)
Personally, I find the interface much cleaner.

@robpark

Selenium Framework

unread,
Jan 11, 2015, 6:46:40 PM1/11/15
to cu...@googlegroups.com
If a gem is not required, I generally avoid it, because it adds an extra layer of [possibly not well written] code that I have to deal with every time something breaks. Especially when there are multiple touch points of failure in Test Automation projects. I implement page-object framework, data driven or keyword driven by using plain ruby concepts. That way if something breaks, I am able to identify the line of code much easier than digging through a gem code and NOT being sure where it might have failed. 

My 2 cents. And btw all of the major framework models are implemented as below.


Cheers!

Andrew Premdas

unread,
Jan 12, 2015, 12:09:27 AM1/12/15
to cu...@googlegroups.com
I don't want to start any sort of flame war here, but do want to point out that that using page-objects is not a 'requirement' for writing simple elegant features.  IMO the concept of pages is the wrong one for testing web applications, the things we should be thinking about are resources.

Anyhow different strokes for different folks and all that

All best

Andrew

--
Posting rules: http://cukes.info/posting-rules.html
---
You received this message because you are subscribed to the Google Groups "Cukes" group.
To unsubscribe from this group and stop receiving emails from it, send an email to cukes+un...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.



--
------------------------
Andrew Premdas

Rob Park

unread,
Jan 12, 2015, 9:52:45 AM1/12/15
to cu...@googlegroups.com
On Mon, Jan 12, 2015 at 12:09 AM, Andrew Premdas <apre...@gmail.com> wrote:
I don't want to start any sort of flame war here, but do want to point out that that using page-objects is not a 'requirement' for writing simple elegant features.  IMO the concept of pages is the wrong one for testing web applications, the things we should be thinking about are resources.

Anyhow different strokes for different folks and all that

All best

Andrew

Fair enough. Especially with Single Page Apps, I end up using the term "Page" for something that's just a component on the page. 
Probably time to review some naming there. :) 

Vlad Wbox

unread,
Jan 12, 2015, 11:11:17 AM1/12/15
to cu...@googlegroups.com
To be honest I don't see much difference with using site_prism or defining constants. Main problem is that element name will not be hard-coded in ruby step definition
 
It will be like this: Then /^Click the category ([^"]*)$/ do |x|

Yes I can define category Age in site_prism.

But Ruby will need to pickup code, defined in site_prism or in constandf definition.
But as of now it uses just the string value "Age".

So there should be some code tweak here for ruby to do this, I guess.

Then /^Click the category ([^"]*)$/ do |x|

element = $driver.find_element :xpath => "//a[@class='mainNavLink' and text() =  '#{x}']"

element.click

end

 
Reply all
Reply to author
Forward
0 new messages