Module: MiniAutobot::Utils::PageObjectHelper
- Defined in:
- lib/mini_autobot/utils/page_object_helper.rb
Overview
Page object-related helper methods.
Instance Method Summary collapse
- #connector_is_saucelabs? ⇒ Boolean
-
#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.
-
#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.
-
#save_to_ever_failed ⇒ Object
Save test name to ever_failed_tests file only for the first time it failed.
- #take_screenshot ⇒ Object
-
#teardown ⇒ void
Local teardown for page objects.
-
#update_sauce_session ⇒ Object
Update SauceLabs session(job) name Update session(job) status if test is not skipped.
Instance Method Details
#connector_is_saucelabs? ⇒ Boolean
126 127 128 129 |
# File 'lib/mini_autobot/utils/page_object_helper.rb', line 126 def connector_is_saucelabs? return true if MiniAutobot.settings.connector.include?('saucelabs') return false end |
#is_element_present?(how, what, driver = nil) ⇒ Boolean
Check if a web element exists on page or not, without wait
175 176 177 |
# File 'lib/mini_autobot/utils/page_object_helper.rb', line 175 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
180 181 182 |
# File 'lib/mini_autobot/utils/page_object_helper.rb', line 180 def is_element_present_and_displayed?(how, what, driver = nil) element_appeared?(how, what, driver, check_display = true) 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.
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 |
# File 'lib/mini_autobot/utils/page_object_helper.rb', line 16 def page(name, override_driver=nil) # Get the fully-qualified class name klass_name = "mini_autobot/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.message}\n" msg << exc.backtrace.map { |str| " #{str}" }.join("\n") raise NameError, msg end # Get a default connector @driver = MiniAutobot::Connector.get_default if override_driver.nil? @driver = override_driver if !override_driver.nil? instance = klass.new(@driver) # 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 MiniAutobot::PageObjects::InvalidePageState, "#{klass}: #{exc.message}" 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
93 94 95 96 97 98 99 |
# File 'lib/mini_autobot/utils/page_object_helper.rb', line 93 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
133 134 135 136 |
# File 'lib/mini_autobot/utils/page_object_helper.rb', line 133 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”
142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 |
# File 'lib/mini_autobot/utils/page_object_helper.rb', line 142 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 return value end |
#retry_with_count(count, &block) ⇒ Object
Retry a block of code for a number of times
160 161 162 163 164 165 166 167 168 169 170 171 172 |
# File 'lib/mini_autobot/utils/page_object_helper.rb', line 160 def retry_with_count(count, &block) try = 0 count.times do try += 1 begin block.call return true rescue Exception => e MiniAutobot.logger.warn "Exception: #{e}\nfrom\n#{block.source_location.join(':')}" MiniAutobot.logger.warn "Retrying" if try < count end end end |
#save_to_ever_failed ⇒ Object
Save test name to ever_failed_tests file only for the first time it failed
81 82 83 84 85 86 87 88 89 |
# File 'lib/mini_autobot/utils/page_object_helper.rb', line 81 def save_to_ever_failed ever_failed_tests = 'logs/tap_results/ever_failed_tests' File.open(ever_failed_tests, 'a') do |f| existing_failed_tests = File.readlines(ever_failed_tests).map do |line| line.delete "\n" end f.puts name unless existing_failed_tests.include? name end end |
#take_screenshot ⇒ Object
76 77 78 |
# File 'lib/mini_autobot/utils/page_object_helper.rb', line 76 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.
59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 |
# File 'lib/mini_autobot/utils/page_object_helper.rb', line 59 def teardown if !passed? && !skipped? && !@driver.nil? save_to_ever_failed if MiniAutobot.settings.rerun_failure take_screenshot print_sauce_link if connector_is_saucelabs? end begin update_sauce_session if connector_is_saucelabs? && !@driver.nil? self.logger.debug "Finished setting saucelabs session name for #{name()}" rescue self.logger.debug "Failed setting saucelabs session name for #{name()}" end MiniAutobot::Connector.finalize! super end |
#update_sauce_session ⇒ Object
Update SauceLabs session(job) name Update session(job) status if test is not skipped
103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 |
# File 'lib/mini_autobot/utils/page_object_helper.rb', line 103 def update_sauce_session connector = MiniAutobot.settings.connector # eg. saucelabs:phu:win7_ie11 overrides = connector.to_s.split(/:/) = overrides[2]+"_by_"+overrides[1] file_name = overrides.shift path = MiniAutobot.root.join('config/mini_autobot', 'connectors') filepath = path.join("#{file_name}.yml") raise ArgumentError, "Cannot load profile #{file_name.inspect} because #{filepath.inspect} does not exist" unless filepath.exist? cfg = YAML.load(File.read(filepath)) cfg = Connector.resolve(cfg, overrides) cfg.freeze username = cfg["hub"]["user"] access_key = cfg["hub"]["pass"] require 'json' session_id = @driver.session_id http_auth = "https://#{username}:#{access_key}@saucelabs.com/rest/v1/#{username}/jobs/#{session_id}" body = { "name" => name(), "tags" => [] } body["passed"] = passed? unless skipped? RestClient.put(http_auth, body.to_json, {:content_type => "application/json"}) end |