[Ruby] Setting up global variables & Objects in a cucumber project accessible in page objects

1,669 views
Skip to first unread message
Message has been deleted

PTeng

unread,
May 22, 2013, 5:22:08 PM5/22/13
to cu...@googlegroups.com
Further to above

I have a page object class with the below method

Class LoginPage
Include PageObject

...

def login_with (userid,password)
p 'Logging in now ...'
self.password = password
self.userid = userid
self.login

wait.until { html_text = MyBrowser::browser.page_source
html_text.include?("Login Page") == false
}
p 'Creating menu page'
MenuPage.new(browser)
end

I want "wait" the global wait to to available to me that was setup in env.rb or else where ?

Anthony Green

unread,
Aug 28, 2013, 6:22:38 AM8/28/13
to cu...@googlegroups.com


On Wednesday, 22 May 2013 15:47:12 UTC+1, PTeng wrote:
Hello All,
 
I am trying to set up a cucumber project using selenium-webdriver and page-object gems in ruby. I want to set up some global objects such as browser instance, browser properties among other things. Does any one know of a way that it can be done (or there are some design patterns) so that my code is DRY. I want these global objects to be available in step definitions and page-objects. Right now I find myself not being able to use them in page-object classes. some of the things that I might need to be available globally
 
browser = Selenium::WebDriver.for :firefox
wait = Selenium::WebDriver::Wait.new(:timeout => 15)

I've started seeing occurrences of this question more and more amongst our teams, where they want to use a driver to make control and make assertions about domain concepts:
ie
media_player.play video_of_foo
media_player.should be_playing video_of_foo / video_of_foo.should be_playing

where #play and #playing? are delegated to driver like Capybara, or Sikuli.

I've seen various solutions to this: global variable, mixin, constructor injection + delegation.

I've been wondering if a good approach might be to use  constructor injection + delegation for the 'control' aspect and the observer pattern for the assertion aspect.

Thoughts? TIA

Anthony Green



Andrew Premdas

unread,
Aug 28, 2013, 4:03:32 PM8/28/13
to cu...@googlegroups.com
On 22 May 2013 15:47, PTeng <punee...@googlemail.com> wrote:
Hello All,
 
I am trying to set up a cucumber project using selenium-webdriver and page-object gems in ruby. I want to set up some global objects such as browser instance, browser properties among other things. Does any one know of a way that it can be done (or there are some design patterns) so that my code is DRY. I want these global objects to be available in step definitions and page-objects. Right now I find myself not being able to use them in page-object classes. some of the things that I might need to be available globally
 
browser = Selenium::WebDriver.for :firefox
wait = Selenium::WebDriver::Wait.new(:timeout => 15)

Caveat, I don't use page objects, and don't think you need them when using Cukes.

This is basic ruby, when you create a class you change the value of self, which is why you cannot access these things. So if you want your PageObject classes to have access to things outside the class, you need to pass those things in when you create an instance of the class.

In general your classes can get access to everything by passing Cucumber world into them. So you when you instantiate your PageObject pass in self (assuming you are instantiating it where self is Cucumber::World) e.g. in a step def

Given "A page object" do
  PageObject.new(self)
end

Now whether you should do this is another matter entirely. In general in step definitions I treat classes as immutable plain objects, I don't let them do things with the outside world. I'm not convinced that PageObjects need to do things with the outside world either, but then I don't know much about PageObjects

HTH, all best

Andrew
 
-PTeng

--
-- Rules --
 
1) Please prefix the subject with [Ruby], [JVM] or [JS].
2) Please use interleaved answers http://en.wikipedia.org/wiki/Posting_style#Interleaved_style
3) If you have a question, don't reply to an existing message. Start a new topic instead.
 
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 https://groups.google.com/d/forum/cukes?hl=en
---
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/groups/opt_out.
 
 



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

Jeff Morgan

unread,
Aug 31, 2013, 7:59:56 AM8/31/13
to cu...@googlegroups.com
PTeng,

There are a few things I will mention here.  It is not considered good coding practice to use global variables the way you are trying.  And, at least for the examples you provide, it is not necessary.  The page-object gem has several methods built-in that provide the ability to wait for different events to occur prior to proceeding.  I would suggest using them.

There is a case for having data shared across different page objects.  I have run into cases where I needed to share data from page to page.  In these cases, I needed to ensure I used the exact same data on later pages that were used in previous ones.  The way I solved this was by creating simple class that acted sort of like an in-memory database.  This class included Singleton and was backed by the data_magic gem.  Since it was a singleton I was able to access it in each page.

-Cheezy

Matt Wynne

unread,
Sep 30, 2013, 9:32:48 AM9/30/13
to cu...@googlegroups.com
WAT?

Can you unpack that last sentence and explain what you mean?


Thoughts? TIA

Anthony Green




--
-- Rules --
 
1) Please prefix the subject with [Ruby], [JVM] or [JS].
2) Please use interleaved answers http://en.wikipedia.org/wiki/Posting_style#Interleaved_style
3) If you have a question, don't reply to an existing message. Start a new topic instead.
 
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 https://groups.google.com/d/forum/cukes?hl=en
---
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/groups/opt_out.
Reply all
Reply to author
Forward
0 new messages