Path: blob/trunk/rb/spec/integration/selenium/webdriver/spec_support/helpers.rb
4091 views
# frozen_string_literal: true12# Licensed to the Software Freedom Conservancy (SFC) under one3# or more contributor license agreements. See the NOTICE file4# distributed with this work for additional information5# regarding copyright ownership. The SFC licenses this file6# to you under the Apache License, Version 2.0 (the7# "License"); you may not use this file except in compliance8# with the License. You may obtain a copy of the License at9#10# http://www.apache.org/licenses/LICENSE-2.011#12# Unless required by applicable law or agreed to in writing,13# software distributed under the License is distributed on an14# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY15# KIND, either express or implied. See the License for the16# specific language governing permissions and limitations17# under the License.1819module Selenium20module WebDriver21module SpecSupport22module Helpers23def driver24GlobalTestEnv.driver_instance25end2627def reset_driver!(...)28GlobalTestEnv.reset_driver!(...)29end3031def quit_driver32GlobalTestEnv.quit_driver33end3435def create_driver!(...)36GlobalTestEnv.create_driver!(...)37end3839def url_for(filename)40GlobalTestEnv.url_for filename41end4243def fix_windows_path(path)44return path unless WebDriver::Platform.windows?4546if GlobalTestEnv.browser == :ie47path = path[%r{file://(.*)}, 1]48path = WebDriver::Platform.windows_path(path)4950"file://#{path}"51else52path.sub(%r[file:/{0,2}], 'file:///')53end54end5556def long_wait57@long_wait ||= Wait.new(timeout: 30)5859return @long_wait unless block_given?6061result = nil62@long_wait.until { result = yield }63result64end6566def short_wait67@short_wait ||= Wait.new(timeout: 3)6869return @short_wait unless block_given?7071result = nil72@short_wait.until { result = yield }73result74end7576def wait_for_alert77wait = Wait.new(timeout: 5, ignore: Error::NoSuchAlertError)78wait.until { driver.switch_to.alert }79end8081def wait_for_no_alert82wait = Wait.new(timeout: 5, ignore: Error::UnexpectedAlertOpenError)83wait.until { driver.title }84end8586def wait_for_element(locator, timeout = 25)87wait = Wait.new(timeout: timeout, ignore: Error::NoSuchElementError, message_provider: lambda {88url = "page url: #{driver.current_url};\n"89source = "page source: #{driver.find_element(css: 'body').attribute('innerHTML')}\n"90"could not find element #{locator} in #{timeout} seconds;\n#{url}#{source}"91})92wait.until { driver.find_element(locator) }93end9495def wait_for_url(new_url, timeout = 15)96wait = Wait.new(timeout: timeout, message_provider: lambda {97"could not wait for URL #{new_url} in #{timeout} seconds;\nactual page url: #{driver.current_url};\n"98})99wait.until do100driver.current_url.include?(new_url)101end102end103104def wait_for_devtools_target(target_type:)105wait = Wait.new(timeout: 3, ignore: Error::NoSuchTargetError)106wait.until { driver.devtools(target_type: target_type).target }107end108109def wait_for_title(title:)110wait = Wait.new(timeout: 15)111wait.until { driver.title == title }112end113114def open_file(file_name)115driver.navigate.to 'about:blank'116driver.navigate.to url_for('blank.html')117driver.navigate.to url_for(file_name)118wait_for_url(file_name)119end120121def wait(timeout = 10)122Wait.new(timeout: timeout)123end124125def png_size(path)126png = File.read(path, mode: 'rb')[0x10..0x18]127width = png.unpack1('NN')128height = png.unpack('NN').last129130if Platform.mac? # Retina131width /= 2132height /= 2133end134135[width, height]136end137138def create_tempfile139Tempfile.new.tap do |file|140file.write('This is a dummy test file')141file.close142end143end144end # Helpers145end # SpecSupport146end # WebDriver147end # Selenium148149150