Module: Browsery::Utils::PageObjectHelper
- Included in:
- PageObjects::Base, PageObjects::Overlay::Base, PageObjects::Section, PageObjects::Widgets::Base, TestCase
- Defined in:
- lib/browsery/utils/page_object_helper.rb
Overview
Page object-related helper methods.
Instance Method Summary collapse
- #connector_is_saucelabs? ⇒ Boolean
- #current_page(calling_page) ⇒ Object
-
#is_element_present?(how, what, driver = nil) ⇒ Boolean
Check if a web element exists on page or not, without wait.
-
#is_element_present_and_displayed?(how, what, driver = nil) ⇒ Boolean
Check if a web element exists and displayed on page or not, without wait.
-
#json_save_to_ever_failed ⇒ Object
Create new/override same file ever_failed_tests.json with fail count.
-
#page(name, override_driver = nil) ⇒ PageObject::Base
Helper method to instantiate a new page object.
-
#print_sauce_link ⇒ Object
Print out a link of a saucelabs’s job when a test is not passed Rescue to skip this step for tests like cube tracking.
-
#put_value(web_element, value) ⇒ Object
Generic page object helper method to clear and send keys to a web element found by driver.
-
#read_yml(file_name, keys) ⇒ Object
Helper method for retrieving value from yml file todo should be moved to FileHelper.rb once we created this file in utils keys, eg.
-
#retry_with_count(count, &block) ⇒ Object
Retry a block of code for a number of times.
-
#take_screenshot ⇒ Object
Take screenshot and save as png with test name as file name.
-
#teardown ⇒ void
Local teardown for page objects.
-
#update_sauce_session_name ⇒ Object
Update SauceLabs session(job) name and build number/name.
-
#update_sauce_session_status ⇒ Object
Update session(job) status if test is not skipped.
- #visual_regression(test_step_id) ⇒ Object
-
#wait_for_attribute_to_have_value(how, what, attribute, value, friendly_name = "attribute") ⇒ Object
Useful when you want to wait for the status of an element attribute to change Example: the class attribute of <body> changes to include ‘logged-in’ when a user signs in to rent.com Example usage: wait_for_attribute_status_change(:css, ‘body’, ‘class’, ‘logged-in’, ‘sign in’).
- #wait_for_element_to_be_present(how, what, friendly_name = "element") ⇒ Object
- #wait_for_element_to_display(how, what, friendly_name = "element") ⇒ Object
- #with_url_change_wait(&block) ⇒ Object
Instance Method Details
#connector_is_saucelabs? ⇒ Boolean
137 138 139 |
# File 'lib/browsery/utils/page_object_helper.rb', line 137 def connector_is_saucelabs? Browsery.settings.connector.include? 'saucelabs' end |
#current_page(calling_page) ⇒ Object
219 220 221 |
# File 'lib/browsery/utils/page_object_helper.rb', line 219 def current_page(calling_page) calling_page.class.to_s.split('::').last.downcase end |
#is_element_present?(how, what, driver = nil) ⇒ Boolean
Check if a web element exists on page or not, without wait
192 193 194 |
# File 'lib/browsery/utils/page_object_helper.rb', line 192 def is_element_present?(how, what, driver = nil) element_appeared?(how, what, driver) end |
#is_element_present_and_displayed?(how, what, driver = nil) ⇒ Boolean
Check if a web element exists and displayed on page or not, without wait
197 198 199 |
# File 'lib/browsery/utils/page_object_helper.rb', line 197 def is_element_present_and_displayed?(how, what, driver = nil) element_appeared?(how, what, driver, check_display = true) end |
#json_save_to_ever_failed ⇒ Object
Create new/override same file ever_failed_tests.json with fail count
87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 |
# File 'lib/browsery/utils/page_object_helper.rb', line 87 def json_save_to_ever_failed ever_failed_tests = 'logs/tap_results/ever_failed_tests.json' data_hash = {} if File.file?(ever_failed_tests) && !File.zero?(ever_failed_tests) data_hash = JSON.parse(File.read(ever_failed_tests)) end if data_hash[name] data_hash[name]["fail_count"] += 1 else data_hash[name] = { "fail_count" => 1 } end begin data_hash[name]["last_fail_on_sauce"] = "saucelabs.com/tests/#{@driver.session_id}" rescue self.logger.debug "Failed setting last_fail_on_sauce, driver may not be available" end File.open(ever_failed_tests, 'w+') do |file| file.write JSON.pretty_generate(data_hash) end end |
#page(name, override_driver = nil) ⇒ PageObject::Base
Helper method to instantiate a new page object. This method should only be used when first loading; subsequent page objects are automatically instantiated by calling #cast on the page object.
Pass optional parameter Driver, which can be initialized in test and will override the global driver here.
15 16 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 47 48 49 50 51 52 53 54 55 56 57 58 59 |
# File 'lib/browsery/utils/page_object_helper.rb', line 15 def page(name, override_driver=nil) # Get the fully-qualified class name klass_name = "browsery/page_objects/#{name}".camelize klass = begin klass_name.constantize rescue => exc msg = "" msg << "Cannot find page object '#{name}', " msg << "because could not load class '#{klass_name}' " msg << "with underlying error:\n #{exc.class}: #{exc.}\n" msg << exc.backtrace.map { |str| " #{str}" }.join("\n") raise NameError, msg end # Get a default connector @driver = Browsery::Connector.get_default if override_driver.nil? @driver = override_driver if !override_driver.nil? instance = klass.new(@driver) # Set SauceLabs session(job) name to test's name if running on Saucelabs begin update_sauce_session_name if connector_is_saucelabs? && !@driver.nil? rescue self.logger.debug "Failed setting saucelabs session name for #{name()}" end # Before visiting the page, do any pre-processing necessary, if any, # but only visit the page if the pre-processing succeeds if block_given? retval = yield instance instance.go! if retval else instance.go! if override_driver.nil? end # similar like casting a page, necessary to validate some element on a page begin instance.validate! rescue Minitest::Assertion => exc raise Browsery::PageObjects::InvalidePageState, "#{klass}: #{exc.}" end # Return the instance as-is instance end |
#print_sauce_link ⇒ Object
Print out a link of a saucelabs’s job when a test is not passed Rescue to skip this step for tests like cube tracking
112 113 114 115 116 117 118 |
# File 'lib/browsery/utils/page_object_helper.rb', line 112 def print_sauce_link begin puts "Find test on saucelabs: https://saucelabs.com/tests/#{@driver.session_id}" rescue puts 'can not retrieve driver session id, no link to saucelabs' end end |
#put_value(web_element, value) ⇒ Object
Generic page object helper method to clear and send keys to a web element found by driver
143 144 145 146 |
# File 'lib/browsery/utils/page_object_helper.rb', line 143 def put_value(web_element, value) web_element.clear web_element.send_keys(value) end |
#read_yml(file_name, keys) ⇒ Object
Helper method for retrieving value from yml file todo should be moved to FileHelper.rb once we created this file in utils keys, eg. “timeouts:implicit_wait”
152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 |
# File 'lib/browsery/utils/page_object_helper.rb', line 152 def read_yml(file_name, keys) data = Hash.new begin data = YAML.load_file "#{file_name}" rescue raise Exception, "File #{file_name} doesn't exist" unless File.exist?(file_name) rescue raise YAMLErrors, "Failed to load #{file_name}" end keys_array = keys.split(/:/) value = data keys_array.each do |key| value = value[key] end value end |
#retry_with_count(count, &block) ⇒ Object
Retry a block of code for a number of times
170 171 172 173 174 175 176 177 178 179 180 181 182 |
# File 'lib/browsery/utils/page_object_helper.rb', line 170 def retry_with_count(count, &block) try = 0 count.times do try += 1 begin block.call return true rescue Exception => e Browsery.logger.warn "Exception: #{e}\nfrom\n#{block.source_location.join(':')}" Browsery.logger.warn "Retrying" if try < count end end end |
#take_screenshot ⇒ Object
Take screenshot and save as png with test name as file name
82 83 84 |
# File 'lib/browsery/utils/page_object_helper.rb', line 82 def take_screenshot @driver.save_screenshot("logs/#{name}.png") end |
#teardown ⇒ void
This method returns an undefined value.
Local teardown for page objects. Any page objects that are loaded will be finalized upon teardown.
65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 |
# File 'lib/browsery/utils/page_object_helper.rb', line 65 def teardown if !passed? && !skipped? && !@driver.nil? json_save_to_ever_failed if Browsery.settings.rerun_failure print_sauce_link if connector_is_saucelabs? take_screenshot end begin update_sauce_session_status if connector_is_saucelabs? && !@driver.nil? && !skipped? rescue self.logger.debug "Failed setting saucelabs session status for #{name()}" end Browsery::Connector.finalize! super end |
#update_sauce_session_name ⇒ Object
Update SauceLabs session(job) name and build number/name
121 122 123 124 125 126 127 128 |
# File 'lib/browsery/utils/page_object_helper.rb', line 121 def update_sauce_session_name http_auth = Browsery.settings.sauce_session_http_auth(@driver) body = { 'name' => name() } unless (build_number = ENV['JENKINS_BUILD_NUMBER']).nil? body['build'] = build_number end RestClient.put(http_auth, body.to_json, {:content_type => "application/json"}) end |
#update_sauce_session_status ⇒ Object
Update session(job) status if test is not skipped
131 132 133 134 135 |
# File 'lib/browsery/utils/page_object_helper.rb', line 131 def update_sauce_session_status http_auth = Browsery.settings.sauce_session_http_auth(@driver) body = { "passed" => passed? } RestClient.put(http_auth, body.to_json, {:content_type => "application/json"}) end |
#visual_regression(test_step_id) ⇒ Object
223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 |
# File 'lib/browsery/utils/page_object_helper.rb', line 223 def visual_regression(test_step_id) if Browsery.settings.visual_regression if File.exist?("visual_regression/#{test_step_id}-base.png") base_image_path = "visual_regression/#{test_step_id}-base.png" new_image_path = @driver.save_screenshot("visual_regression/#{test_step_id}-new.png") diff_image = Browsery::DiffImage.new(base_image_path, new_image_path) diff_percentage = diff_image.calculate_changes if diff_percentage > Browsery.settings.visual_regression diff_image_path = "visual_regression/#{test_step_id}-diff.png" diff_image.save_diff(diff_image_path) return false end else @driver.save_screenshot("visual_regression/#{test_step_id}-base.png") end end true end |
#wait_for_attribute_to_have_value(how, what, attribute, value, friendly_name = "attribute") ⇒ Object
Useful when you want to wait for the status of an element attribute to change Example: the class attribute of <body> changes to include ‘logged-in’ when a user signs in to rent.com Example usage: wait_for_attribute_status_change(:css, ‘body’, ‘class’, ‘logged-in’, ‘sign in’)
214 215 216 217 |
# File 'lib/browsery/utils/page_object_helper.rb', line 214 def wait_for_attribute_to_have_value(how, what, attribute, value, friendly_name = "attribute") wait(timeout: 15, message: "Timeout waiting for #{friendly_name} status to update") .until { driver.find_element(how, what).attribute(attribute).include?(value) rescue retry } end |
#wait_for_element_to_be_present(how, what, friendly_name = "element") ⇒ Object
206 207 208 209 |
# File 'lib/browsery/utils/page_object_helper.rb', line 206 def wait_for_element_to_be_present(how, what, friendly_name = "element") wait(timeout: 15, message: "Timeout waiting for #{friendly_name} to be present") .until {is_element_present?(how, what)} end |
#wait_for_element_to_display(how, what, friendly_name = "element") ⇒ Object
201 202 203 204 |
# File 'lib/browsery/utils/page_object_helper.rb', line 201 def wait_for_element_to_display(how, what, friendly_name = "element") wait(timeout: 15, message: "Timeout waiting for #{friendly_name} to display") .until {is_element_present_and_displayed?(how, what)} end |
#with_url_change_wait(&block) ⇒ Object
184 185 186 187 188 189 |
# File 'lib/browsery/utils/page_object_helper.rb', line 184 def with_url_change_wait(&block) starting_url = @driver.current_url block.call wait(timeout: 15, message: 'Timeout waiting for URL to change') .until { @driver.current_url != starting_url } end |