Path: blob/trunk/rb/spec/integration/selenium/webdriver/spec_support/helpers.rb
1865 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)87wait = Wait.new(timeout: 25, ignore: Error::NoSuchElementError)88wait.until { driver.find_element(locator) }89end9091def wait_for_new_url(old_url)92wait = Wait.new(timeout: 5)93wait.until do94url = driver.current_url95!(url.empty? || url.include?(old_url))96end97end9899def wait(timeout = 10)100Wait.new(timeout: timeout)101end102103def png_size(path)104png = File.read(path, mode: 'rb')[0x10..0x18]105width = png.unpack1('NN')106height = png.unpack('NN').last107108if Platform.mac? # Retina109width /= 2110height /= 2111end112113[width, height]114end115116def create_tempfile117Tempfile.new.tap do |file|118file.write('This is a dummy test file')119file.close120end121end122end # Helpers123end # SpecSupport124end # WebDriver125end # Selenium126127128