Module: Percy
- Defined in:
- lib/percy.rb,
lib/version.rb
Constant Summary collapse
- CLIENT_INFO =
"percy-selenium-ruby/#{VERSION}".freeze
- ENV_INFO =
"selenium/#{Selenium::WebDriver::VERSION} ruby/#{RUBY_VERSION}".freeze
- PERCY_DEBUG =
ENV['PERCY_LOGLEVEL'] == 'debug'
- PERCY_SERVER_ADDRESS =
ENV['PERCY_SERVER_ADDRESS'] || 'http://localhost:5338'
- LABEL =
"[\u001b[35m" + (PERCY_DEBUG ? 'percy:ruby' : 'percy') + "\u001b[39m]"
- RESONSIVE_CAPTURE_SLEEP_TIME =
ENV['RESONSIVE_CAPTURE_SLEEP_TIME']
- VERSION =
'1.1.0'.freeze
Class Method Summary collapse
- ._clear_cache! ⇒ Object
- .capture_responsive_dom(driver, options) ⇒ Object
- .change_window_dimension_and_wait(driver, width, height, resize_count) ⇒ Object
-
.fetch(url, data = nil) ⇒ Object
Make an HTTP request (GET,POST) using Ruby’s Net::HTTP.
-
.fetch_percy_dom ⇒ Object
Fetch the @percy/dom script, caching the result so it is only fetched once.
- .get_browser_instance(driver) ⇒ Object
- .get_serialized_dom(driver, options) ⇒ Object
- .get_widths_for_multi_dom(options) ⇒ Object
- .log(msg, lvl = 'info') ⇒ Object
-
.percy_enabled? ⇒ Boolean
Determine if the Percy server is running, caching the result so it is only checked once.
- .responsive_snapshot_capture?(options) ⇒ Boolean
-
.snapshot(driver, name, options = {}) ⇒ Object
Take a DOM snapshot and post it to the snapshot endpoint.
Class Method Details
._clear_cache! ⇒ Object
218 219 220 221 222 223 |
# File 'lib/percy.rb', line 218 def self._clear_cache! @percy_dom = nil @percy_enabled = nil @eligible_widths = nil @cli_config = nil end |
.capture_responsive_dom(driver, options) ⇒ Object
100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 |
# File 'lib/percy.rb', line 100 def self.capture_responsive_dom(driver, ) widths = get_widths_for_multi_dom() dom_snapshots = [] window_size = get_browser_instance(driver).window.size current_width = window_size.width current_height = window_size.height last_window_width = current_width resize_count = 0 driver.execute_script('PercyDOM.waitForResize()') widths.each do |width| if last_window_width != width resize_count += 1 change_window_dimension_and_wait(driver, width, current_height, resize_count) last_window_width = width end sleep(RESONSIVE_CAPTURE_SLEEP_TIME.to_i) if defined?(RESONSIVE_CAPTURE_SLEEP_TIME) dom_snapshot = get_serialized_dom(driver, ) dom_snapshot['width'] = width dom_snapshots << dom_snapshot end change_window_dimension_and_wait(driver, current_width, current_height, resize_count + 1) dom_snapshots end |
.change_window_dimension_and_wait(driver, width, height, resize_count) ⇒ Object
78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 |
# File 'lib/percy.rb', line 78 def self.change_window_dimension_and_wait(driver, width, height, resize_count) begin if driver.capabilities.browser_name == 'chrome' && driver.respond_to?(:execute_cdp) driver.execute_cdp('Emulation.setDeviceMetricsOverride', { height: height, width: width, deviceScaleFactor: 1, mobile: false, },) else get_browser_instance(driver).window.resize_to(width, height) end rescue StandardError => e log("Resizing using cdp failed, falling back to driver for width #{width} #{e}", 'debug') get_browser_instance(driver).window.resize_to(width, height) end begin wait = Selenium::WebDriver::Wait.new(timeout: 1) wait.until { driver.execute_script('return window.resizeCount') == resize_count } rescue Selenium::WebDriver::Error::TimeoutError log("Timed out waiting for window resize event for width #{width}", 'debug') end end |
.fetch(url, data = nil) ⇒ Object
Make an HTTP request (GET,POST) using Ruby’s Net::HTTP. If ‘data` is present, `fetch` will POST as JSON.
198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 |
# File 'lib/percy.rb', line 198 def self.fetch(url, data = nil) uri = URI("#{PERCY_SERVER_ADDRESS}/#{url}") response = if data http = Net::HTTP.new(uri.host, uri.port) http.read_timeout = 600 # seconds request = Net::HTTP::Post.new(uri.path) request.body = data.to_json http.request(request) else Net::HTTP.get_response(uri) end unless response.is_a? Net::HTTPSuccess raise StandardError, "Failed with HTTP error code: #{response.code}" end response end |
.fetch_percy_dom ⇒ Object
Fetch the @percy/dom script, caching the result so it is only fetched once
174 175 176 177 178 179 |
# File 'lib/percy.rb', line 174 def self.fetch_percy_dom return @percy_dom unless @percy_dom.nil? response = fetch('percy/dom.js') @percy_dom = response.body end |
.get_browser_instance(driver) ⇒ Object
48 49 50 51 52 53 54 55 |
# File 'lib/percy.rb', line 48 def self.get_browser_instance(driver) # this means it is a capybara session if driver.respond_to?(:driver) && driver.driver.respond_to?(:browser) return driver.driver.browser.manage end driver.manage end |
.get_serialized_dom(driver, options) ⇒ Object
57 58 59 60 61 62 |
# File 'lib/percy.rb', line 57 def self.get_serialized_dom(driver, ) dom_snapshot = driver.execute_script("return PercyDOM.serialize(#{.to_json})") dom_snapshot['cookies'] = get_browser_instance(driver). dom_snapshot end |
.get_widths_for_multi_dom(options) ⇒ Object
64 65 66 67 68 69 70 71 72 73 74 75 76 |
# File 'lib/percy.rb', line 64 def self.get_widths_for_multi_dom() user_passed_widths = [:widths] || [] # Deep copy mobile widths otherwise it will get overridden all_widths = @eligible_widths['mobile']&.dup || [] if user_passed_widths.any? all_widths.concat(user_passed_widths) else all_widths.concat(@eligible_widths['config'] || []) end all_widths.uniq end |
.log(msg, lvl = 'info') ⇒ Object
181 182 183 184 185 186 187 188 189 190 191 192 193 194 |
# File 'lib/percy.rb', line 181 def self.log(msg, lvl = 'info') msg = "#{LABEL} #{msg}" begin fetch('percy/log', {message: msg, level: lvl}) rescue StandardError => e if PERCY_DEBUG puts "Sending log to CLI Failed #{e}" end ensure if lvl != 'debug' || PERCY_DEBUG puts msg end end end |
.percy_enabled? ⇒ Boolean
Determine if the Percy server is running, caching the result so it is only checked once
138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 |
# File 'lib/percy.rb', line 138 def self.percy_enabled? return @percy_enabled unless @percy_enabled.nil? begin response = fetch('percy/healthcheck') version = response['x-percy-core-version'] if version.nil? log('You may be using @percy/agent ' \ 'which is no longer supported by this SDK. ' \ 'Please uninstall @percy/agent and install @percy/cli instead. ' \ 'https://www.browserstack.com/docs/percy/migration/migrate-to-cli') @percy_enabled = false return false end if version.split('.')[0] != '1' log("Unsupported Percy CLI version, #{version}") @percy_enabled = false return false end response_body = JSON.parse(response.body) @eligible_widths = response_body['widths'] @cli_config = response_body['config'] @percy_enabled = true true rescue StandardError => e log('Percy is not running, disabling snapshots') log(e, 'debug') @percy_enabled = false false end end |
.responsive_snapshot_capture?(options) ⇒ Boolean
128 129 130 131 132 133 134 135 |
# File 'lib/percy.rb', line 128 def self.responsive_snapshot_capture?() # Don't run responsive snapshot capture when defer uploads is enabled return false if @cli_config&.dig('percy', 'deferUploads') [:responsive_snapshot_capture] || [:responsiveSnapshotCapture] || @cli_config&.dig('snapshot', 'responsiveSnapshotCapture') end |
.snapshot(driver, name, options = {}) ⇒ Object
Take a DOM snapshot and post it to the snapshot endpoint
17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 |
# File 'lib/percy.rb', line 17 def self.snapshot(driver, name, = {}) return unless percy_enabled? begin driver.execute_script(fetch_percy_dom) dom_snapshot = if responsive_snapshot_capture?() capture_responsive_dom(driver, ) else get_serialized_dom(driver, ) end response = fetch('percy/snapshot', name: name, url: driver.current_url, dom_snapshot: dom_snapshot, client_info: CLIENT_INFO, environment_info: ENV_INFO, **,) unless response.body.to_json['success'] raise StandardError, data['error'] end body = JSON.parse(response.body) body['data'] rescue StandardError => e log("Could not take DOM snapshot '#{name}'") log(e, 'debug') end end |