123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268 |
- module BrowserTestHelper
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- def retry_on_stale(retries: 3)
- tries ||= 0
- yield
- rescue Selenium::WebDriver::Error::StaleElementReferenceError
- raise if tries == retries
- wait_time = tries
- tries += 1
- Rails.logger.info "Stale element found. Retry #{tries}/retries (sleeping: #{wait_time})"
- sleep wait_time
- end
-
-
- def cookies
- page.driver.browser.manage.all_cookies
- end
-
-
-
-
-
- def cookie(name)
- cookies.find { |cookie| cookie[:name].match?(name) }
- end
-
-
-
-
-
- def delete_cookie(name)
- cookie = cookies.find { |c| c[:name].match?(name) }
- return if !cookie
- page.driver.browser.manage.delete_cookie(cookie[:name])
- end
-
-
-
-
-
-
-
- def click(...)
- find(...).click
- end
-
-
-
-
-
-
- def find_icon(name)
- find("[href=\"#icon-#{name}\"]").find(:xpath, '..')
- end
-
-
-
-
-
-
-
-
-
-
- def wait(seconds = Capybara.default_max_wait_time, **kwargs)
- wait_args = Hash(kwargs).merge(timeout: seconds)
- wait_handle = Selenium::WebDriver::Wait.new(wait_args)
- Waiter.new(wait_handle)
- end
-
-
-
-
-
-
-
- def await_empty_ajax_queue
-
- return if %i[desktop_view mobile].include?(self.class.metadata[:app])
-
- begin
- return if page.driver.browser.switch_to.alert
- rescue Selenium::WebDriver::Error::NoSuchAlertError
- end
-
- return if page.evaluate_script('typeof(App) !== "function" || typeof($) !== "function"')
-
- sleep 0.1
- wait(5).until do
- page.evaluate_script('App.Ajax.queue().length === 0 && $.active === 0 && Object.keys(App.FormHandlerCoreWorkflow.getRequests()).length === 0').eql? true
- end
- rescue Selenium::WebDriver::Error::TimeoutError
- nil
- end
-
-
-
-
-
-
-
-
- def move_mouse_by(x_axis, y_axis)
- page.driver.browser.action.move_by(x_axis, y_axis).perform
- end
-
-
-
-
-
- def move_mouse_to(element)
- element.in_fixed_position
- page.driver.browser.action.move_to_location(element.native.location.x, element.native.location.y).perform
- end
-
-
-
-
-
-
- def click_and_hold(element)
- page.driver.browser.action.click_and_hold(element).perform
- end
-
-
-
-
-
-
-
- def display_macro_batches(ticket)
-
- element = page.find(:table_row, ticket.id).native
-
-
-
-
-
- page.driver.browser.action
- .move_to_location(element.location.x + 50, element.location.y + 10)
- .click_and_hold
- .move_by(0, -element.location.y + 3)
- .move_by(3, 0)
- .perform
- end
-
-
-
-
-
- def release_mouse
- page.driver.browser.action.release.perform
- await_empty_ajax_queue
- end
- class Waiter < SimpleDelegator
-
-
-
-
-
-
-
- def until_exists
- self.until do
- yield
- rescue Capybara::ElementNotFound
-
- end
- rescue Selenium::WebDriver::Error::TimeoutError => e
-
- e.set_backtrace(e.backtrace.drop(3))
- raise e
- end
-
-
-
-
-
-
-
- def until_disappears
- self.until do
- yield
- false
- rescue Capybara::ElementNotFound
- true
- end
- rescue Selenium::WebDriver::Error::TimeoutError => e
-
- e.set_backtrace(e.backtrace.drop(3))
- raise e
- end
-
-
-
-
-
- def until_constant
- previous = nil
- timeout = __getobj__.instance_variable_get(:@timeout)
- interval = __getobj__.instance_variable_get(:@interval)
- rounds = (timeout / interval).to_i
- rounds.times do
- sleep interval
- latest = yield
- next if latest.nil?
- break if latest == previous
- previous = latest
- end
- end
- end
- end
- RSpec.configure do |config|
- config.include BrowserTestHelper, type: :system
- end
|