Class: Sahi::Browser
- Inherits:
-
Object
- Object
- Sahi::Browser
- Defined in:
- lib/sahi.rb
Instance Attribute Summary collapse
-
#domain_name ⇒ Object
Returns the value of attribute domain_name.
-
#popup_name ⇒ Object
Returns the value of attribute popup_name.
-
#print_steps ⇒ Object
Returns the value of attribute print_steps.
-
#proxy_host ⇒ Object
Returns the value of attribute proxy_host.
-
#proxy_port ⇒ Object
Returns the value of attribute proxy_port.
-
#sahisid ⇒ Object
Returns the value of attribute sahisid.
Instance Method Summary collapse
-
#add_url_mock(url_pattern, clazz = nil) ⇒ Object
make specific url patterns return dummy responses.
- #browser_js=(js) ⇒ Object
- #check_nil(s) ⇒ Object
- #check_proxy ⇒ Object
-
#chrome? ⇒ Boolean
returns true if browser is Google Chrome.
-
#clear_last_alert ⇒ Object
resets the last alerted message.
-
#clear_last_confirm ⇒ Object
resets the last confirm message.
-
#clear_last_downloaded_filename ⇒ Object
clear last downloaded file’s name.
-
#clear_last_prompt ⇒ Object
clears the last prompted message.
-
#close ⇒ Object
closes the browser.
-
#domain(name) ⇒ Object
represents a domain section of window.
- #domain? ⇒ Boolean
- #exec_command(cmd, qs = {}) ⇒ Object
- #execute_step(step) ⇒ Object
-
#expect_confirm(message, input) ⇒ Object
set an expectation to press OK (true) or Cancel (false) for specific confirm message.
-
#expect_prompt(message, input) ⇒ Object
set an expectation to set given value for specific prompt message.
-
#fetch(expression) ⇒ Object
evaluates a javascript expression on the browser and fetches its value.
-
#fetch_boolean(expression) ⇒ Object
evaluates a javascript expression on the browser and returns true if value is true or “true”.
-
#firefox? ⇒ Boolean
returns true if browser is Firefox.
-
#ie? ⇒ Boolean
returns true if browser is Internet Explorer.
-
#initialize(*args) ⇒ Browser
constructor
Takes browser_type as specified in sahi/userdata/config/browser_types.xml (name of browserType) - RECOMMENDED.
- #is_ready? ⇒ Boolean
-
#last_alert ⇒ Object
returns the message last alerted on the browser.
-
#last_confirm ⇒ Object
returns the last confirm message.
-
#last_downloaded_filename ⇒ Object
get last downloaded file’s name.
-
#last_prompt ⇒ Object
returns the last prompted message.
- #method_missing(m, *args, &block) ⇒ Object
-
#navigate_to(url, force_reload = false) ⇒ Object
navigates to the given url.
-
#open ⇒ Object
opens the browser.
-
#opera? ⇒ Boolean
returns true if browser is Opera.
-
#popup(name) ⇒ Object
represents a popup window.
- #popup? ⇒ Boolean
-
#remove_url_mock(url_pattern) ⇒ Object
reverse effect of add_url_mock.
- #response(url, qs = {}) ⇒ Object
-
#safari? ⇒ Boolean
returns true if browser is Safari.
-
#save_downloaded(file_path) ⇒ Object
Save the last downloaded file to specified path.
-
#speed=(ms) ⇒ Object
sets the speed of execution.
-
#strict_visibility_check=(check) ⇒ Object
sets strict visibility check.
-
#title ⇒ Object
return window title.
-
#wait(timeout) ⇒ Object
waits for specified time (in seconds).
Constructor Details
#initialize(*args) ⇒ Browser
Takes browser_type as specified in sahi/userdata/config/browser_types.xml (name of browserType) - RECOMMENDED
OR
Takes browser_path, browser_options and browser_executable - NOT RECOMMENDED Various browser options needed to initialize the Browser object are:
Internet Explorer 6&7: browser_path = “C:\Program Files\Internet Explorer\iexplore.exe” browser_options = “”
browser_executable = "iexplore.exe"
Internet Explorer 8: browser_path = “C:\Program Files\Internet Explorer\iexplore.exe” browser_options = “-nomerge”
browser_executable = "iexplore.exe"
Firefox:
browser_path = "C:\\Program Files\\Mozilla Firefox\\firefox.exe"
= "-profile $userDir/browser/ff/profiles/sahi0 -no-remote"
browser_executable = "firefox.exe"
Chrome: userdata_dir = “D:/sahi/sf/sahi_993/userdata” # path to Sahi’s userdata directory.
browser_path = "C:\\Documents and Settings\\YOU_THE_USER\\Local Settings\\Application Data\\Google\\Chrome\\Application\\chrome.exe"
= "--user-data-dir=# {userdata_dir}\browser\chrome\profiles\sahi$threadNo"
browser_executable = "chrome.exe"
Safari: browser_path = “C:\Program Files\SafariSafari.exe” browser_options = “”
browser_executable = "safari.exe"
Opera: browser_path = “C:\Program Files\Opera\opera.exe” browser_options = “”
browser_executable = "opera.exe"
65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 |
# File 'lib/sahi.rb', line 65 def initialize(*args) @proxy_host = "localhost" @proxy_port = 9999 if args.size == 3 @browser_path = args[0] @browser_options = ars[1] @browser_executable = args[2] elsif args.size == 1 @browser_type = args[0] end @popup_name = nil @domain_name = nil @sahisid = nil @print_steps = false end |
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(m, *args, &block) ⇒ Object
150 151 152 |
# File 'lib/sahi.rb', line 150 def method_missing(m, *args, &block) return ElementStub.new(self, m.to_s, args) end |
Instance Attribute Details
#domain_name ⇒ Object
Returns the value of attribute domain_name.
25 26 27 |
# File 'lib/sahi.rb', line 25 def domain_name @domain_name end |
#popup_name ⇒ Object
Returns the value of attribute popup_name.
25 26 27 |
# File 'lib/sahi.rb', line 25 def popup_name @popup_name end |
#print_steps ⇒ Object
Returns the value of attribute print_steps.
25 26 27 |
# File 'lib/sahi.rb', line 25 def print_steps @print_steps end |
#proxy_host ⇒ Object
Returns the value of attribute proxy_host.
25 26 27 |
# File 'lib/sahi.rb', line 25 def proxy_host @proxy_host end |
#proxy_port ⇒ Object
Returns the value of attribute proxy_port.
25 26 27 |
# File 'lib/sahi.rb', line 25 def proxy_port @proxy_port end |
#sahisid ⇒ Object
Returns the value of attribute sahisid.
25 26 27 |
# File 'lib/sahi.rb', line 25 def sahisid @sahisid end |
Instance Method Details
#add_url_mock(url_pattern, clazz = nil) ⇒ Object
make specific url patterns return dummy responses. Look at _addMock documentation.
294 295 296 297 |
# File 'lib/sahi.rb', line 294 def add_url_mock(url_pattern, clazz=nil) clazz = "MockResponder_simple" if !clazz execute_step("_sahi._addMock(#{Utils.quoted(url_pattern)}, #{Utils.quoted(clazz)})") end |
#browser_js=(js) ⇒ Object
167 168 169 |
# File 'lib/sahi.rb', line 167 def browser_js=(js) exec_command("setBrowserJS", {"browserJS"=>js}) end |
#check_nil(s) ⇒ Object
171 172 173 |
# File 'lib/sahi.rb', line 171 def check_nil(s) return (s == "null") ? nil : s end |
#check_proxy ⇒ Object
81 82 83 84 85 86 87 |
# File 'lib/sahi.rb', line 81 def check_proxy() begin response("http://#{@proxy_host}:#{@proxy_port}/_s_/spr/blank.htm") rescue raise "Sahi proxy is not available. Please start the Sahi proxy." end end |
#chrome? ⇒ Boolean
returns true if browser is Google Chrome
320 321 322 |
# File 'lib/sahi.rb', line 320 def chrome?() return fetch_boolean("_sahi._isChrome()") end |
#clear_last_alert ⇒ Object
resets the last alerted message
244 245 246 |
# File 'lib/sahi.rb', line 244 def clear_last_alert() execute_step("_sahi._clearLastAlert()") end |
#clear_last_confirm ⇒ Object
resets the last confirm message
254 255 256 |
# File 'lib/sahi.rb', line 254 def clear_last_confirm() execute_step("_sahi._clearLastConfirm()") end |
#clear_last_downloaded_filename ⇒ Object
clear last downloaded file’s name
284 285 286 |
# File 'lib/sahi.rb', line 284 def clear_last_downloaded_filename() execute_step("_sahi._clearLastDownloadedFileName()") end |
#clear_last_prompt ⇒ Object
clears the last prompted message
269 270 271 |
# File 'lib/sahi.rb', line 269 def clear_last_prompt() execute_step("_sahi._clearLastPrompt()") end |
#close ⇒ Object
closes the browser
176 177 178 179 180 181 182 183 |
# File 'lib/sahi.rb', line 176 def close() if popup?() execute_step("_sahi._closeWindow()"); else exec_command("kill"); #Process.kill(9, @pid) if @pid end end |
#domain(name) ⇒ Object
represents a domain section of window.
214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 |
# File 'lib/sahi.rb', line 214 def domain(name) if (@browser_type != null) win = Browser.new(@browser_type) else win = Browser.new(@browser_path, @browser_options, @browser_executable) end win.proxy_host = @proxy_host win.proxy_port = @proxy_port win.sahisid = @sahisid win.print_steps = @print_steps win.popup_name = @popup_name win.domain_name = name return win end |
#domain? ⇒ Boolean
234 235 236 |
# File 'lib/sahi.rb', line 234 def domain?() return @domain_name != nil end |
#exec_command(cmd, qs = {}) ⇒ Object
112 113 114 115 |
# File 'lib/sahi.rb', line 112 def exec_command(cmd, qs={}) res = response("http://#{@proxy_host}:#{@proxy_port}/_s_/dyn/Driver_" + cmd, {"sahisid"=>@sahisid}.update(qs)) return res end |
#execute_step(step) ⇒ Object
126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 |
# File 'lib/sahi.rb', line 126 def execute_step(step) if popup?() step = "_sahi._popup(#{Utils.quoted(@popup_name)})." + step end if domain?() step = "_sahi._domain(#{Utils.quoted(@domain_name)})." + step end #puts step exec_command("setStep", {"step" => step}) i = 0 while (i < 500) sleep(0.1) i+=1 check_done = exec_command("doneStep") done = "true".eql?(check_done) error = check_done.index("error:") == 0 return if done if (error) raise check_done end end end |
#expect_confirm(message, input) ⇒ Object
set an expectation to press OK (true) or Cancel (false) for specific confirm message
259 260 261 |
# File 'lib/sahi.rb', line 259 def expect_confirm(, input) execute_step("_sahi._expectConfirm(#{Utils.quoted() }, #{input})") end |
#expect_prompt(message, input) ⇒ Object
set an expectation to set given value for specific prompt message
274 275 276 |
# File 'lib/sahi.rb', line 274 def expect_prompt(, input) execute_step("_sahi._expectPrompt(#{Utils.quoted()}, #{Utils.quoted(input) })") end |
#fetch(expression) ⇒ Object
evaluates a javascript expression on the browser and fetches its value
155 156 157 158 159 |
# File 'lib/sahi.rb', line 155 def fetch(expression) key = "___lastValue___" + Time.now.getutc.to_s; execute_step("_sahi.setServerVarPlain('"+key+"', " + expression + ")") return check_nil(exec_command("getVariable", {"key" => key})) end |
#fetch_boolean(expression) ⇒ Object
evaluates a javascript expression on the browser and returns true if value is true or “true”
162 163 164 |
# File 'lib/sahi.rb', line 162 def fetch_boolean(expression) return fetch(expression) == "true" end |
#firefox? ⇒ Boolean
returns true if browser is Firefox
315 316 317 |
# File 'lib/sahi.rb', line 315 def firefox?() return fetch_boolean("_sahi._isFF()") end |
#ie? ⇒ Boolean
returns true if browser is Internet Explorer
310 311 312 |
# File 'lib/sahi.rb', line 310 def ie?() return fetch_boolean("_sahi._isIE()") end |
#is_ready? ⇒ Boolean
108 109 110 |
# File 'lib/sahi.rb', line 108 def is_ready? return "true".eql?(exec_command("isReady")) end |
#last_alert ⇒ Object
returns the message last alerted on the browser
239 240 241 |
# File 'lib/sahi.rb', line 239 def last_alert() return fetch("_sahi._lastAlert()") end |
#last_confirm ⇒ Object
returns the last confirm message
249 250 251 |
# File 'lib/sahi.rb', line 249 def last_confirm() return fetch("_sahi._lastConfirm()") end |
#last_downloaded_filename ⇒ Object
get last downloaded file’s name
279 280 281 |
# File 'lib/sahi.rb', line 279 def last_downloaded_filename() return fetch("_sahi._lastDownloadedFileName()") end |
#last_prompt ⇒ Object
returns the last prompted message
264 265 266 |
# File 'lib/sahi.rb', line 264 def last_prompt() return fetch("_sahi._lastPrompt()") end |
#navigate_to(url, force_reload = false) ⇒ Object
navigates to the given url
122 123 124 |
# File 'lib/sahi.rb', line 122 def navigate_to(url, force_reload=false) execute_step("_sahi._navigateTo(\"" + url + "\", "+ (force_reload.to_s()) +")"); end |
#open ⇒ Object
opens the browser
90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 |
# File 'lib/sahi.rb', line 90 def open() check_proxy() @sahisid = Time.now.to_f start_url = "http://sahi.example.com/_s_/dyn/Driver_initialized" if (@browser_type != null) exec_command("launchPreconfiguredBrowser", {"browserType" => @browser_type, "startUrl" => start_url}) else exec_command("launchAndPlayback", {"browser" => @browser, "browserOptions" => @browser_options, "browserExecutable" => @browser_executable, "startUrl" => start_url}) end i = 0 while (i < 500) i+=1 break if is_ready? sleep(0.1) end end |
#opera? ⇒ Boolean
returns true if browser is Opera
330 331 332 |
# File 'lib/sahi.rb', line 330 def opera?() return fetch_boolean("_sahi._isOpera()") end |
#popup(name) ⇒ Object
represents a popup window. The name is either the window name or its title.
197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 |
# File 'lib/sahi.rb', line 197 def popup(name) if (@browser_type != null) win = Browser.new(@browser_type) else win = Browser.new(@browser_path, @browser_options, @browser_executable) end win.proxy_host = @proxy_host win.proxy_port = @proxy_port win.sahisid = @sahisid win.print_steps = @print_steps win.popup_name = name win.domain_name = @domain_name return win end |
#popup? ⇒ Boolean
230 231 232 |
# File 'lib/sahi.rb', line 230 def popup?() return @popup_name != nil end |
#remove_url_mock(url_pattern) ⇒ Object
reverse effect of add_url_mock
300 301 302 |
# File 'lib/sahi.rb', line 300 def remove_url_mock(url_pattern) execute_step("_sahi._removeMock(#{Utils.quoted(url_pattern)})") end |
#response(url, qs = {}) ⇒ Object
117 118 119 |
# File 'lib/sahi.rb', line 117 def response(url, qs={}) return Net::HTTP.post_form(URI.parse(url), qs).body end |
#safari? ⇒ Boolean
returns true if browser is Safari
325 326 327 |
# File 'lib/sahi.rb', line 325 def safari?() return fetch_boolean("_sahi._isSafari()") end |
#save_downloaded(file_path) ⇒ Object
Save the last downloaded file to specified path
289 290 291 |
# File 'lib/sahi.rb', line 289 def save_downloaded(file_path) execute_step("_sahi._saveDownloadedAs(#{Utils.quoted(file_path)})") end |
#speed=(ms) ⇒ Object
sets the speed of execution. The speed is specified in milli seconds
186 187 188 |
# File 'lib/sahi.rb', line 186 def speed=(ms) exec_command("setSpeed", {"speed"=>ms}) end |
#strict_visibility_check=(check) ⇒ Object
sets strict visibility check. If true, Sahi APIs ignores elements which are not visible
191 192 193 |
# File 'lib/sahi.rb', line 191 def strict_visibility_check=(check) execute_step("_sahi._setStrictVisibilityCheck(#{check})") end |
#title ⇒ Object
return window title
305 306 307 |
# File 'lib/sahi.rb', line 305 def title() return fetch("_sahi._title()") end |
#wait(timeout) ⇒ Object
waits for specified time (in seconds). if a block is passed, it will wait till the block evaluates to true or till the specified timeout, which ever is earlier.
336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 |
# File 'lib/sahi.rb', line 336 def wait(timeout) total = 0; interval = 0.2; if !block_given? sleep(timeout) return end while (total < timeout) sleep(interval); total += interval; begin return if yield rescue Exception=>e puts e end end end |