Class: Webdrone::Browser
- Inherits:
-
Object
- Object
- Webdrone::Browser
- Defined in:
- lib/webdrone/clic.rb,
lib/webdrone/conf.rb,
lib/webdrone/ctxt.rb,
lib/webdrone/exec.rb,
lib/webdrone/find.rb,
lib/webdrone/form.rb,
lib/webdrone/html.rb,
lib/webdrone/logg.rb,
lib/webdrone/mark.rb,
lib/webdrone/open.rb,
lib/webdrone/shot.rb,
lib/webdrone/text.rb,
lib/webdrone/vrfy.rb,
lib/webdrone/wait.rb,
lib/webdrone/xlsx.rb,
lib/webdrone/browser.rb
Class Attribute Summary collapse
Instance Attribute Summary collapse
-
#driver ⇒ Object
readonly
Returns the value of attribute driver.
Class Method Summary collapse
Instance Method Summary collapse
- #clic ⇒ Object
- #conf ⇒ Object
- #console(binding = nil) ⇒ Object
- #ctxt ⇒ Object
- #exec ⇒ Object
- #find ⇒ Object
- #form ⇒ Object
- #html ⇒ Object
-
#initialize(browser: 'firefox', create_outdir: true, outdir: nil, timeout: 30, developer: false, logger: true, quit_at_exit: true, maximize: true, error: :raise_report, win_x: nil, win_y: nil, win_w: nil, win_h: nil, use_env: true, chrome_options: nil, firefox_options: nil, firefox_profile: nil, remote_url: nil, headless: false) ⇒ Browser
constructor
A new instance of Browser.
- #logs ⇒ Object
- #mark ⇒ Object
- #maximize ⇒ Object
- #open ⇒ Object
- #quit ⇒ Object
- #shot ⇒ Object
- #text ⇒ Object
- #vrfy ⇒ Object
- #wait ⇒ Object
- #xlsx ⇒ Object
Constructor Details
#initialize(browser: 'firefox', create_outdir: true, outdir: nil, timeout: 30, developer: false, logger: true, quit_at_exit: true, maximize: true, error: :raise_report, win_x: nil, win_y: nil, win_w: nil, win_h: nil, use_env: true, chrome_options: nil, firefox_options: nil, firefox_profile: nil, remote_url: nil, headless: false) ⇒ Browser
Returns a new instance of Browser.
43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 |
# File 'lib/webdrone/browser.rb', line 43 def initialize(browser: 'firefox', create_outdir: true, outdir: nil, timeout: 30, developer: false, logger: true, quit_at_exit: true, maximize: true, error: :raise_report, win_x: nil, win_y: nil, win_w: nil, win_h: nil, use_env: true, chrome_options: nil, firefox_options: nil, firefox_profile: nil, remote_url: nil, headless: false) env_update(Kernel.binding) if use_env if create_outdir || outdir outdir ||= File.join('webdrone_output', Time.new.strftime('%Y%m%d_%H%M%S')) conf.outdir = outdir end outdir = File.join(Dir.pwd, outdir) if !outdir.nil? && !Pathname.new(outdir).absolute? if remote_url @driver = Selenium::WebDriver.for :remote, url: remote_url, desired_capabilities: browser.to_sym elsif !outdir.nil? && browser.to_sym == :chrome ||= Browser. .add_preference 'download.default_directory', outdir .add_argument '--disable-popup-blocking' .add_argument '--headless' if headless .add_argument '--start-maximized' if maximize maximize = false service = Selenium::WebDriver::Service.chrome(args: { log_path: "/tmp/chromedriver.#{$PID}.log", verbose: true }) @driver = Selenium::WebDriver.for browser.to_sym, options: , service: service elsif !outdir.nil? && browser.to_sym == :firefox ||= Browser. firefox_profile ||= Browser.firefox_profile .add_argument '-headless' if headless downdir = OS.windows? ? outdir.tr('/', '\\') : outdir firefox_profile['browser.download.dir'] = downdir .profile = firefox_profile @driver = Selenium::WebDriver.for browser.to_sym, options: else @driver = Selenium::WebDriver.for browser.to_sym end if quit_at_exit at_exit do @driver.quit rescue StandardError nil end end conf.error = error.to_sym conf.developer = developer conf.timeout = timeout.to_i if timeout conf.logger = logger if developer win_x = win_y = 0 win_w = 0.5 win_h = 1.0 end if win_x || win_y || win_w || win_h x, y, w, h = exec.script 'return [window.screenLeft ? window.screenLeft : window.screenX, window.screenTop ? window.screenTop : window.screenY, window.screen.availWidth, window.screen.availHeight];' win_x ||= x win_y ||= y if win_w.is_a? Float win_w = (w * win_w).to_i else win_w ||= w end if win_h.is_a? Float win_h = (h * win_h).to_i else win_h ||= h end begin @driver.manage.window.position = Selenium::WebDriver::Point.new win_x, win_y @driver.manage.window.resize_to(win_w, win_h) rescue StandardError => e puts "Ignoring error on window position/resize: #{e}" end elsif maximize self.maximize end end |
Class Attribute Details
.chrome_options ⇒ Object
32 33 34 35 36 37 38 39 40 |
# File 'lib/webdrone/browser.rb', line 32 def return @chrome_options if defined? @chrome_options @chrome_options = Selenium::WebDriver::Chrome::Options.new @chrome_options.add_preference 'download.prompt_for_download', false @chrome_options.add_preference 'credentials_enable_service:', false @chrome_options end |
.firefox_profile ⇒ Object
12 13 14 15 16 17 18 19 20 21 22 |
# File 'lib/webdrone/browser.rb', line 12 def firefox_profile return @firefox_profile if defined? @firefox_profile @firefox_profile = Selenium::WebDriver::Firefox::Profile.new @firefox_profile['startup.homepage_welcome_url.additional'] = 'about:blank' @firefox_profile['browser.download.folderList'] = 2 @firefox_profile['browser.download.manager.showWhenStarting'] = false @firefox_profile['browser.helperApps.neverAsk.saveToDisk'] = 'images/jpeg, application/pdf, application/octet-stream, application/download' @firefox_profile end |
Instance Attribute Details
#driver ⇒ Object (readonly)
Returns the value of attribute driver.
7 8 9 |
# File 'lib/webdrone/browser.rb', line 7 def driver @driver end |
Class Method Details
.firefox_options ⇒ Object
24 25 26 27 28 29 30 |
# File 'lib/webdrone/browser.rb', line 24 def return @firefox_options if defined? @firefox_options @firefox_options = Selenium::WebDriver::Firefox::Options.new @firefox_options end |
Instance Method Details
#console(binding = nil) ⇒ Object
129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 |
# File 'lib/webdrone/browser.rb', line 129 def console(binding = nil) return unless conf.developer binding ||= Kernel.binding.of_caller(1) old_error = conf.error old_developer = conf.developer begin conf.error = :raise conf.developer = false Webdrone.pry_console binding ensure conf.error = old_error conf.developer = old_developer end end |
#maximize ⇒ Object
121 122 123 |
# File 'lib/webdrone/browser.rb', line 121 def maximize @driver.manage.window.maximize end |
#quit ⇒ Object
125 126 127 |
# File 'lib/webdrone/browser.rb', line 125 def quit @driver.quit end |