Created
October 6, 2010 11:27
-
-
Save jarib/613205 to your computer and use it in GitHub Desktop.
watir page objects
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require "rubygems" | |
require "watir-webdriver" | |
class BrowserContainer | |
def initialize(browser) | |
@browser = browser | |
end | |
end | |
# | |
# site object - returns pages | |
# | |
class Google < BrowserContainer | |
def front_page | |
FrontPage.new(@browser) | |
end | |
def result_page | |
ResultPage.new(@browser) | |
end | |
end | |
# | |
# page object | |
# | |
class FrontPage < BrowserContainer | |
def open | |
@browser.goto "http://google.com" | |
end | |
def search_for(query) | |
search_field.set(query) | |
search_button.click | |
end | |
private | |
def search_field | |
@browser.text_field(:name => 'q') | |
end | |
def search_button | |
@browser.button(:name => "btnG") | |
end | |
end | |
class ResultPage < BrowserContainer | |
def results | |
# ... some code to fetch results here ... | |
end | |
end | |
# | |
# actual script | |
# | |
site = Google.new(Watir::Browser.new(:firefox)) | |
site.front_page.search_for("watir-webdriver") | |
site.result_page.results.each do |result| | |
p result | |
end | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment