Class: Applitools::Selenium::VisualGridEyes
- Inherits:
-
Object
- Object
- Applitools::Selenium::VisualGridEyes
- Extended by:
- Forwardable
- Defined in:
- lib/applitools/selenium/visual_grid/visual_grid_eyes.rb
Constant Summary collapse
- DOM_EXTRACTION_TIMEOUT =
seconds or 5 minutes
300- USE_DEFAULT_MATCH_TIMEOUT =
-1
Instance Attribute Summary collapse
-
#api_key ⇒ Object
Returns the value of attribute api_key.
-
#config ⇒ Object
Returns the value of attribute config.
-
#current_config ⇒ Object
Returns the value of attribute current_config.
-
#current_url ⇒ Object
Returns the value of attribute current_url.
-
#driver ⇒ Object
Returns the value of attribute driver.
-
#fetched_cache_map ⇒ Object
Returns the value of attribute fetched_cache_map.
-
#opened ⇒ Object
Returns the value of attribute opened.
-
#proxy ⇒ Object
Returns the value of attribute proxy.
-
#server_url ⇒ Object
Returns the value of attribute server_url.
-
#test_list ⇒ Object
Returns the value of attribute test_list.
-
#visual_grid_manager ⇒ Object
Returns the value of attribute visual_grid_manager.
Instance Method Summary collapse
- #abort_if_not_closed ⇒ Object
- #check(tag, target) ⇒ Object
-
#check_frame(options = {}) ⇒ Applitools::MatchResult
Validates the contents of an iframe and matches it with the expected output.
-
#check_region(element, how = nil, what = nil, options = {}) ⇒ Object
Takes a snapshot of the application under test and matches a region of a specific element with the expected region output.
-
#check_region_in_frame(options = {}) ⇒ Applitools::MatchResult
Validates the contents of a region in an iframe and matches it with the expected output.
-
#check_window(tag = nil, match_timeout = USE_DEFAULT_MATCH_TIMEOUT) ⇒ Object
Takes a snapshot of the application under test and matches it with the expected output.
- #close(throw_exception = true) ⇒ Object
- #collect_selenium_regions(target) ⇒ Object
- #configure {|config| ... } ⇒ Object
- #element_or_region(target_element) ⇒ Object
- #ensure_config ⇒ Object
- #eyes_connector ⇒ Object
- #get_all_test_results ⇒ Object
- #get_regions_x_paths(target) ⇒ Object
- #get_viewport_size(web_driver = driver) ⇒ Object
-
#initialize(visual_grid_manager, server_url = nil) ⇒ VisualGridEyes
constructor
A new instance of VisualGridEyes.
- #open(*args) ⇒ Object
- #open? ⇒ Boolean
- #set_viewport_size(value) ⇒ Object
- #setup_size_mode(target_element) ⇒ Object
-
#test(options = {}) {|driver| ... } ⇒ Object
Use this method to perform seamless testing with selenium through eyes driver.
Constructor Details
#initialize(visual_grid_manager, server_url = nil) ⇒ VisualGridEyes
Returns a new instance of VisualGridEyes.
22 23 24 25 26 27 28 |
# File 'lib/applitools/selenium/visual_grid/visual_grid_eyes.rb', line 22 def initialize(visual_grid_manager, server_url = nil) ensure_config self.server_url = server_url if server_url self.visual_grid_manager = visual_grid_manager self.test_list = Applitools::Selenium::TestList.new self.opened = false end |
Instance Attribute Details
#api_key ⇒ Object
Returns the value of attribute api_key.
14 15 16 |
# File 'lib/applitools/selenium/visual_grid/visual_grid_eyes.rb', line 14 def api_key @api_key end |
#config ⇒ Object
Returns the value of attribute config.
11 12 13 |
# File 'lib/applitools/selenium/visual_grid/visual_grid_eyes.rb', line 11 def config @config end |
#current_config ⇒ Object
Returns the value of attribute current_config.
11 12 13 |
# File 'lib/applitools/selenium/visual_grid/visual_grid_eyes.rb', line 11 def current_config @current_config end |
#current_url ⇒ Object
Returns the value of attribute current_url.
11 12 13 |
# File 'lib/applitools/selenium/visual_grid/visual_grid_eyes.rb', line 11 def current_url @current_url end |
#driver ⇒ Object
Returns the value of attribute driver.
11 12 13 |
# File 'lib/applitools/selenium/visual_grid/visual_grid_eyes.rb', line 11 def driver @driver end |
#fetched_cache_map ⇒ Object
Returns the value of attribute fetched_cache_map.
11 12 13 |
# File 'lib/applitools/selenium/visual_grid/visual_grid_eyes.rb', line 11 def fetched_cache_map @fetched_cache_map end |
#opened ⇒ Object
Returns the value of attribute opened.
14 15 16 |
# File 'lib/applitools/selenium/visual_grid/visual_grid_eyes.rb', line 14 def opened @opened end |
#proxy ⇒ Object
Returns the value of attribute proxy.
14 15 16 |
# File 'lib/applitools/selenium/visual_grid/visual_grid_eyes.rb', line 14 def proxy @proxy end |
#server_url ⇒ Object
Returns the value of attribute server_url.
14 15 16 |
# File 'lib/applitools/selenium/visual_grid/visual_grid_eyes.rb', line 14 def server_url @server_url end |
#test_list ⇒ Object
Returns the value of attribute test_list.
12 13 14 |
# File 'lib/applitools/selenium/visual_grid/visual_grid_eyes.rb', line 12 def test_list @test_list end |
#visual_grid_manager ⇒ Object
Returns the value of attribute visual_grid_manager.
11 12 13 |
# File 'lib/applitools/selenium/visual_grid/visual_grid_eyes.rb', line 11 def visual_grid_manager @visual_grid_manager end |
Instance Method Details
#abort_if_not_closed ⇒ Object
223 224 225 |
# File 'lib/applitools/selenium/visual_grid/visual_grid_eyes.rb', line 223 def abort_if_not_closed test_list.each(&:abort_if_not_closed) end |
#check(tag, target) ⇒ Object
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 120 121 |
# File 'lib/applitools/selenium/visual_grid/visual_grid_eyes.rb', line 82 def check(tag, target) script = " var callback = arguments[arguments.length - 1]; return (\#{Applitools::Selenium::Scripts::PROCESS_RESOURCES})().then(JSON.stringify).then(callback, function(err) {callback(err.stack || err.toString())});\n END\n script = <<-END\n \#{Applitools::Selenium::Scripts::PROCESS_PAGE_AND_POLL} return __processPageAndPoll();\n END\n begin\n sleep wait_before_screenshots\n\n # script_result = driver.execute_async_script(script).freeze\n\n dom_snapshot_start_time = Time.now()\n result = {}\n script_result = nil\n loop do\n script_result = driver.execute_script(script)\n begin\n result = Oj.load(script_result)\n break if result['status'] == 'SUCCESS'\n dom_snapshot_time = Time.now - dom_snapshot_start_time\n raise Applitools::EyesError, 'Timeout error while getting dom snapshot!' if dom_snapshot_time > DOM_EXTRACTION_TIMEOUT\n rescue Oj::ParseError => e\n Applitools::EyesLogger.warn e.message\n end\n end\n\n mod = Digest::SHA2.hexdigest(script_result)\n\n region_x_paths = get_regions_x_paths(target)\n\n test_list.each do |t|\n t.check(tag, target, result['value'].dup, visual_grid_manager, region_x_paths, size_mod, region_to_check, mod)\n end\n test_list.each { |t| t.becomes_not_rendered }\n rescue StandardError => e\n Applitools::EyesLogger.error e.message\n test_list.each { |t| t.becomes_tested }\n end\nend\n" |
#check_frame(options = {}) ⇒ Applitools::MatchResult
Validates the contents of an iframe and matches it with the expected output.
314 315 316 317 318 319 |
# File 'lib/applitools/selenium/visual_grid/visual_grid_eyes.rb', line 314 def check_frame( = {}) = { timeout: USE_DEFAULT_MATCH_TIMEOUT, tag: nil }.merge!() frame = [:frame] || [:frame_element] || [:name_or_id] target = Applitools::Selenium::Target.frame(frame).timeout([:timeout]).fully check([:tag], target) end |
#check_region(element, how = nil, what = nil, options = {}) ⇒ Object
Takes a snapshot of the application under test and matches a region of a specific element with the expected region output.
297 298 299 300 301 302 |
# File 'lib/applitools/selenium/visual_grid/visual_grid_eyes.rb', line 297 def check_region(*args) = { timeout: USE_DEFAULT_MATCH_TIMEOUT, tag: nil }.merge! Applitools::Utils.(args) target = Applitools::Selenium::Target.new.region(*args).timeout([:match_timeout]) target.fully if [:stitch_content] check([:tag], target) end |
#check_region_in_frame(options = {}) ⇒ Applitools::MatchResult
Validates the contents of a region in an iframe and matches it with the expected output.
332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 |
# File 'lib/applitools/selenium/visual_grid/visual_grid_eyes.rb', line 332 def check_region_in_frame( = {}) = { timeout: USE_DEFAULT_MATCH_TIMEOUT, tag: nil, stitch_content: false }.merge!() Applitools::ArgumentGuard.not_nil [:by], 'options[:by]' Applitools::ArgumentGuard.is_a? [:by], 'options[:by]', Array how_what = .delete(:by) frame = [:frame] || [:frame_element] || [:name_or_id] target = Applitools::Selenium::Target.new.timeout([:timeout]) target.frame(frame) if frame target.fully if [:stitch_content] target.region(*how_what) check([:tag], target) end |
#check_window(tag = nil, match_timeout = USE_DEFAULT_MATCH_TIMEOUT) ⇒ Object
Takes a snapshot of the application under test and matches it with the expected output.
270 271 272 273 274 275 276 |
# File 'lib/applitools/selenium/visual_grid/visual_grid_eyes.rb', line 270 def check_window(tag = nil, match_timeout = USE_DEFAULT_MATCH_TIMEOUT) target = Applitools::Selenium::Target.window.tap do |t| t.timeout(match_timeout) t.fully if force_full_page_screenshot end check(tag, target) end |
#close(throw_exception = true) ⇒ Object
197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 |
# File 'lib/applitools/selenium/visual_grid/visual_grid_eyes.rb', line 197 def close(throw_exception = true) return false if test_list.empty? test_list.each(&:close) while (!((states = test_list.map(&:state_name).uniq).count == 1 && states.first == :completed)) do sleep 0.5 end self.opened = false test_list.select { |t| t.pending_exceptions && !t.pending_exceptions.empty? }.each do |t| t.pending_exceptions.each do |e| raise e end end if throw_exception test_list.map(&:test_result).compact.each do |r| raise Applitools::NewTestError.new (r), r if r.new? raise Applitools::DiffsFoundError.new (r), r if r.unresolved? && !r.new? raise Applitools::TestFailedError.new (r), r if r.failed? end end failed_results = test_list.map(&:test_result).compact.select { |r| !r.as_expected? } failed_results.empty? ? test_list.map(&:test_result).compact.first : failed_results end |
#collect_selenium_regions(target) ⇒ Object
147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 |
# File 'lib/applitools/selenium/visual_grid/visual_grid_eyes.rb', line 147 def collect_selenium_regions(target) selenium_regions = {} target_element = target.region_to_check setup_size_mode(target_element) target.ignored_regions.each do |r| selenium_regions[element_or_region(r)] = :ignore end target.floating_regions.each do |r| selenium_regions[element_or_region(r)] = :floating end target.layout_regions.each do |r| selenium_regions[element_or_region(r)] = :layout end target.strict_regions.each do |r| selenium_regions[element_or_region(r)] = :strict end target.content_regions.each do |r| selenium_regions[element_or_region(r)] = :content end selenium_regions[region_to_check] = :target if size_mod == 'selector' selenium_regions end |
#configure {|config| ... } ⇒ Object
34 35 36 37 |
# File 'lib/applitools/selenium/visual_grid/visual_grid_eyes.rb', line 34 def configure return unless block_given? yield(config) end |
#element_or_region(target_element) ⇒ Object
188 189 190 191 192 193 194 195 |
# File 'lib/applitools/selenium/visual_grid/visual_grid_eyes.rb', line 188 def element_or_region(target_element) if target_element.respond_to?(:call) region, _padding_proc = target_element.call(driver, true) region else target_element end end |
#ensure_config ⇒ Object
30 31 32 |
# File 'lib/applitools/selenium/visual_grid/visual_grid_eyes.rb', line 30 def ensure_config self.config = Applitools::Selenium::Configuration.new end |
#eyes_connector ⇒ Object
74 75 76 77 78 79 80 |
# File 'lib/applitools/selenium/visual_grid/visual_grid_eyes.rb', line 74 def eyes_connector logger.info("creating VisualGridEyes server connector") ::Applitools::Selenium::EyesConnector.new(server_url).tap do |connector| connector.batch = batch connector.config = config.deep_clone end end |
#get_all_test_results ⇒ Object
231 232 233 |
# File 'lib/applitools/selenium/visual_grid/visual_grid_eyes.rb', line 231 def get_all_test_results test_list.map(&:test_result) end |
#get_regions_x_paths(target) ⇒ Object
123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 |
# File 'lib/applitools/selenium/visual_grid/visual_grid_eyes.rb', line 123 def get_regions_x_paths(target) # regions_hash = collect_selenium_regions(target).each do |k,v| # v.map! do |el| # if [::Selenium::WebDriver::Element, Applitools::Selenium::Element].include?(el.class) # xpath = driver.execute_script(Applitools::Selenium::Scripts::GET_ELEMENT_XPATH_JS, el) # web_element_region = Applitools::Selenium::WebElementRegion.new(xpath, k) # self.region_to_check = web_element_region if k ==:target && size_mod == 'selector' # web_element_region # end # end.compact! # end result = [] collect_selenium_regions(target).each do |el, v| if [::Selenium::WebDriver::Element, Applitools::Selenium::Element].include?(el.class) xpath = driver.execute_script(Applitools::Selenium::Scripts::GET_ELEMENT_XPATH_JS, el) web_element_region = Applitools::Selenium::WebElementRegion.new(xpath, v) self.region_to_check = web_element_region if v == :target && size_mod == 'selector' result << web_element_region target.regions[el] = result.size - 1 end end result end |
#get_viewport_size(web_driver = driver) ⇒ Object
69 70 71 72 |
# File 'lib/applitools/selenium/visual_grid/visual_grid_eyes.rb', line 69 def (web_driver = driver) Applitools::ArgumentGuard.not_nil 'web_driver', web_driver Applitools::Utils::EyesSeleniumUtils.(driver) end |
#open(*args) ⇒ Object
39 40 41 42 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 |
# File 'lib/applitools/selenium/visual_grid/visual_grid_eyes.rb', line 39 def open(*args) self.test_list = Applitools::Selenium::TestList.new = Applitools::Utils.(args) Applitools::ArgumentGuard.hash(, 'options', [:driver]) config.app_name = [:app_name] if config.app_name.nil? || config.app_name && config.app_name.empty? config.test_name = [:test_name] if config.test_name.nil? || config.test_name && config.test_name.empty? self.driver = .delete(:driver) self.current_url = driver.current_url if () else self. = end visual_grid_manager.open(self) logger.info("getting all browsers info...") browsers_info_list = config.browsers_info logger.info("creating test descriptors for each browser info...") browsers_info_list.each() do |bi| test_list.push Applitools::Selenium::RunningTest.new(eyes_connector, bi, driver) end self.opened = true driver end |
#open? ⇒ Boolean
227 228 229 |
# File 'lib/applitools/selenium/visual_grid/visual_grid_eyes.rb', line 227 def open? opened end |
#set_viewport_size(value) ⇒ Object
235 236 237 238 239 240 241 242 243 |
# File 'lib/applitools/selenium/visual_grid/visual_grid_eyes.rb', line 235 def (value) begin Applitools::Utils::EyesSeleniumUtils. driver, value rescue => e logger.error e.class.to_s logger.error e. raise Applitools::TestFailedError.new "#{e.class} - #{e.message}" end end |
#setup_size_mode(target_element) ⇒ Object
171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 |
# File 'lib/applitools/selenium/visual_grid/visual_grid_eyes.rb', line 171 def setup_size_mode(target_element) self.size_mod = 'full-page' element_or_region = element_or_region(target_element) case element_or_region when ::Selenium::WebDriver::Element, Applitools::Selenium::Element self.size_mod = 'selector' when Applitools::Region self.size_mod = 'region' unless element_or_region == Applitools::Region::EMPTY else self.size_mod = 'full-page' end self.region_to_check = element_or_region end |
#test(options = {}) {|driver| ... } ⇒ Object
Use this method to perform seamless testing with selenium through eyes driver. It yields a block and passes to it an Applitools::Selenium::Driver instance, which wraps standard driver. Using Selenium methods inside the ‘test’ block will send the messages to Selenium after creating the Eyes triggers for them. Options are similar to #open
359 360 361 362 363 364 365 |
# File 'lib/applitools/selenium/visual_grid/visual_grid_eyes.rb', line 359 def test( = {}, &_block) open() yield(driver) close ensure abort_if_not_closed end |