The Browser class controls different browsers via Sahi's proxy.
Narayan Raman (narayan@sahipro.com)
Copyright © 2006 V Narayan Raman
Download Sahi from sahipro.com/ . Java 1.5 or greater is needed to run Sahi.
Start Sahi: cd sahiuserdatabin; start_sahi.bat;
or
cd sahi/userdata/bin; start_sahi.sh;
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" browser_options = "-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" browser_options = "--user-data-dir=# {userdata_dir}\browser\chrome\profiles\sahi$threadNo" browser_executable = "chrome.exe"
Safari:
browser_path = "C:\\Program Files\\Safari\Safari.exe" browser_options = "" browser_executable = "safari.exe"
Opera:
browser_path = "C:\\Program Files\\Opera\\opera.exe" browser_options = "" browser_executable = "opera.exe"
# File 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
make specific url patterns return dummy responses. Look at _addMock documentation.
# File sahi.rb, line 327 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
# File sahi.rb, line 200 def browser_js=(js) exec_command("setBrowserJS", {"browserJS"=>js}) end
# File sahi.rb, line 204 def check_nil(s) return (s == "null") ? nil : s end
# File 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
returns true if browser is Google Chrome
# File sahi.rb, line 363 def chrome?() return fetch_boolean("_sahi._isChrome()") end
resets the last alerted message
# File sahi.rb, line 277 def clear_last_alert() execute_step("_sahi._clearLastAlert()") end
resets the last confirm message
# File sahi.rb, line 287 def clear_last_confirm() execute_step("_sahi._clearLastConfirm()") end
clear last downloaded file's name
# File sahi.rb, line 317 def clear_last_downloaded_filename() execute_step("_sahi._clearLastDownloadedFileName()") end
clears the last prompted message
# File sahi.rb, line 302 def clear_last_prompt() execute_step("_sahi._clearLastPrompt()") end
closes the browser
# File sahi.rb, line 209 def close() if popup?() execute_step("_sahi._closeWindow()"); else exec_command("kill"); #Process.kill(9, @pid) if @pid end end
returns element attributes of all elements of type attr matching the identifier within relations
# File sahi.rb, line 191 def collect(els, attr=nil) if(attr == nil) return els.collect_similar() else return fetch("_sahi.collectAttributes(#{Utils.quoted(attr)}, #{Utils.quoted(els.to_type())}, #{els.to_identifiers()})").split(",___sahi___") end end
represents a domain section of window.
# File sahi.rb, line 247 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
# File sahi.rb, line 267 def domain?() return @domain_name != nil end
# File 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
# File sahi.rb, line 150 def execute_sahi(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, 'addSahi' => true}) 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
# File 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
set an expectation to press OK (true) or Cancel (false) for specific confirm message
# File sahi.rb, line 292 def expect_confirm(message, input) execute_step("_sahi._expectConfirm(#{Utils.quoted(message) }, #{input})") end
set an expectation to set given value for specific prompt message
# File sahi.rb, line 307 def expect_prompt(message, input) execute_step("_sahi._expectPrompt(#{Utils.quoted(message)}, #{Utils.quoted(input) })") end
evaluates a javascript expression on the browser and fetches its value
# File sahi.rb, line 179 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
evaluates a javascript expression on the browser and returns true if value is true or “true”
# File sahi.rb, line 186 def fetch_boolean(expression) return fetch(expression) == "true" end
returns true if browser is Firefox
# File sahi.rb, line 358 def firefox?() return fetch_boolean("_sahi._isFF()") end
returns true if browser is Internet Explorer
# File sahi.rb, line 353 def ie?() return fetch_boolean("_sahi._isIE()") end
# File sahi.rb, line 108 def is_ready? return "true".eql?(exec_command("isReady")) end
returns the message last alerted on the browser
# File sahi.rb, line 272 def last_alert() return fetch("_sahi._lastAlert()") end
returns the last confirm message
# File sahi.rb, line 282 def last_confirm() return fetch("_sahi._lastConfirm()") end
get last downloaded file's name
# File sahi.rb, line 312 def last_downloaded_filename() return fetch("_sahi._lastDownloadedFileName()") end
returns the last prompted message
# File sahi.rb, line 297 def last_prompt() return fetch("_sahi._lastPrompt()") end
# File sahi.rb, line 174 def method_missing(m, *args, &block) return ElementStub.new(self, m.to_s, args) end
opens the browser
# File 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
returns true if browser is Opera
# File sahi.rb, line 373 def opera?() return fetch_boolean("_sahi._isOpera()") end
represents a popup window. The name is either the window name or its title.
# File sahi.rb, line 230 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
# File sahi.rb, line 263 def popup?() return @popup_name != nil end
reverse effect of #add_url_mock
# File sahi.rb, line 333 def remove_url_mock(url_pattern) execute_step("_sahi._removeMock(#{Utils.quoted(url_pattern)})") end
# File sahi.rb, line 117 def response(url, qs={}) return Net::HTTP.post_form(URI.parse(url), qs).body end
returns true if browser is Safari
# File sahi.rb, line 368 def safari?() return fetch_boolean("_sahi._isSafari()") end
Save the last downloaded file to specified path
# File sahi.rb, line 322 def save_downloaded(file_path) execute_step("_sahi._saveDownloadedAs(#{Utils.quoted(file_path)})") end
return selection text
# File sahi.rb, line 344 def selection_text(win = nil) if(win != nil) return fetch("_sahi._getSelectionText(#{win.to_s()})") else return fetch("_sahi._getSelectionText()") end end
sets the speed of execution. The speed is specified in milli seconds
# File sahi.rb, line 219 def speed=(ms) exec_command("setSpeed", {"speed"=>ms}) end
sets strict visibility check. If true, Sahi APIs ignores elements which are not visible
# File sahi.rb, line 224 def strict_visibility_check=(check) execute_step("_sahi._setStrictVisibilityCheck(#{check})") end
return window title
# File sahi.rb, line 338 def title() return fetch("_sahi._title()") end
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.
# File sahi.rb, line 379 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