2018-04-24 18:47:53 -04:00
# frozen_string_literal: true
require 'spec_helper'
require 'selenium-webdriver'
require 'shared_selenium_session'
2019-05-02 21:57:14 -04:00
require 'shared_selenium_node'
2018-04-24 18:47:53 -04:00
require 'rspec/shared_spec_matchers'
def selenium_host
ENV . fetch ( 'SELENIUM_HOST' , '0.0.0.0' )
end
def selenium_port
ENV . fetch ( 'SELENIUM_PORT' , 4444 )
end
def ensure_selenium_running!
2018-06-28 13:18:50 -04:00
timer = Capybara :: Helpers . timer ( expire_in : 20 )
begin
TCPSocket . open ( selenium_host , selenium_port )
2018-07-10 17:18:39 -04:00
rescue StandardError
2018-06-28 13:18:50 -04:00
if timer . expired?
raise 'Selenium is not running. ' \
2021-07-24 22:53:46 -04:00
" You can run a selenium server easily with: \n " \
' $ docker-compose up -d selenium_chrome'
2018-06-28 13:18:50 -04:00
else
2018-07-10 17:18:39 -04:00
puts 'Waiting for Selenium docker instance...'
2018-06-28 13:18:50 -04:00
sleep 1
retry
end
end
2018-04-24 18:47:53 -04:00
end
2018-07-26 17:13:30 -04:00
def selenium_gte? ( version )
defined? ( Selenium :: WebDriver :: VERSION ) && ( Selenium :: WebDriver :: VERSION . to_f > = version )
end
2018-04-24 18:47:53 -04:00
Capybara . register_driver :selenium_chrome_remote do | app |
ensure_selenium_running!
url = " http:// #{ selenium_host } : #{ selenium_port } /wd/hub "
2018-11-01 12:37:36 -04:00
browser_options = :: Selenium :: WebDriver :: Chrome :: Options . new
2018-04-24 18:47:53 -04:00
Capybara :: Selenium :: Driver . new app ,
browser : :remote ,
2018-11-01 12:37:36 -04:00
desired_capabilities : :chrome ,
options : browser_options ,
2018-04-24 18:47:53 -04:00
url : url
end
CHROME_REMOTE_DRIVER = :selenium_chrome_remote
module TestSessions
Chrome = Capybara :: Session . new ( CHROME_REMOTE_DRIVER , TestApp )
end
2018-06-17 16:35:29 -04:00
skipped_tests = % i [ response_headers status_code trigger download ]
2018-04-24 18:47:53 -04:00
2018-07-05 14:50:43 -04:00
Capybara :: SpecHelper . run_specs TestSessions :: Chrome , CHROME_REMOTE_DRIVER . to_s , capybara_skip : skipped_tests do | example |
case example . metadata [ :full_description ]
2018-07-24 17:13:03 -04:00
when 'Capybara::Session selenium_chrome_remote #attach_file with multipart form should not break when using HTML5 multiple file input uploading multiple files' ,
'Capybara::Session selenium_chrome_remote #attach_file with multipart form should fire change once for each set of files uploaded' ,
'Capybara::Session selenium_chrome_remote #attach_file with multipart form should fire change once when uploading multiple files from empty'
2018-07-26 17:13:30 -04:00
pending " Selenium with Remote Chrome doesn't support multiple file upload " unless selenium_gte? ( 3 . 14 )
2018-06-25 20:27:36 -04:00
end
end
2018-07-10 17:18:39 -04:00
RSpec . describe 'Capybara::Session with remote Chrome' do
2018-04-24 18:47:53 -04:00
include Capybara :: SpecHelper
2019-05-02 21:57:14 -04:00
[ 'Capybara::Session' , 'Capybara::Node' , Capybara :: RSpecMatchers ] . each do | examples |
include_examples examples , TestSessions :: Chrome , CHROME_REMOTE_DRIVER
end
2018-04-24 18:47:53 -04:00
2018-06-12 16:06:25 -04:00
it 'is considered to be chrome' do
2018-08-24 14:32:55 -04:00
expect ( session . driver . browser . browser ) . to eq :chrome
2018-04-24 18:47:53 -04:00
end
2019-06-11 14:28:52 -04:00
describe 'log access' do
2021-10-24 01:52:51 -04:00
let ( :logs ) do
session . driver . browser . then do | chrome_driver |
chrome_driver . respond_to? ( :logs ) ? chrome_driver : chrome_driver . manage
end . logs
end
2019-06-13 18:32:14 -04:00
it 'does not error when getting log types' do
2019-06-11 14:28:52 -04:00
expect do
2021-10-24 01:52:51 -04:00
logs . available_types
2019-06-13 18:32:14 -04:00
end . not_to raise_error
2019-06-11 14:28:52 -04:00
end
it 'does not error when getting logs' do
expect do
2021-10-24 01:52:51 -04:00
logs . get ( :browser )
2019-06-11 14:28:52 -04:00
end . not_to raise_error
end
end
2021-01-20 14:37:49 -05:00
def chromedriver_version
Gem :: Version . new ( session . driver . browser . capabilities [ 'chrome' ] [ 'chromedriverVersion' ] . split [ 0 ] )
end
2018-04-24 18:47:53 -04:00
end