2016-09-01 22:46:56 +00:00
# frozen_string_literal: true
2018-03-01 00:11:41 +00:00
2016-09-01 22:46:56 +00:00
require 'spec_helper'
2018-07-10 21:18:39 +00:00
require 'selenium-webdriver'
2016-09-01 22:46:56 +00:00
require 'shared_selenium_session'
2019-05-03 01:57:14 +00:00
require 'shared_selenium_node'
2016-10-14 02:50:03 +00:00
require 'rspec/shared_spec_matchers'
2018-03-01 00:11:41 +00:00
browser_options = :: Selenium :: WebDriver :: Firefox :: Options . new
2018-10-19 17:09:52 +00:00
browser_options . headless! if ENV [ 'HEADLESS' ]
2017-08-11 18:37:43 +00:00
# browser_options.add_option("log", {"level": "trace"})
2017-08-02 19:10:56 +00:00
2018-06-17 17:29:46 +00:00
browser_options . profile = Selenium :: WebDriver :: Firefox :: Profile . new . tap do | profile |
profile [ 'browser.download.dir' ] = Capybara . save_path
profile [ 'browser.download.folderList' ] = 2
profile [ 'browser.helperApps.neverAsk.saveToDisk' ] = 'text/csv'
end
2018-11-18 21:27:00 +00:00
Capybara . register_driver :selenium_firefox do | app |
2017-08-07 20:21:11 +00:00
# ::Selenium::WebDriver.logger.level = "debug"
2016-10-14 02:50:03 +00:00
Capybara :: Selenium :: Driver . new (
app ,
2017-08-11 18:37:43 +00:00
browser : :firefox ,
2018-06-17 17:29:46 +00:00
options : browser_options ,
2018-12-20 20:37:14 +00:00
timeout : 31
2017-08-07 20:21:11 +00:00
# Get a trace level log from geckodriver
# :driver_opts => { args: ['-vv'] }
2016-10-14 02:50:03 +00:00
)
2016-09-01 22:46:56 +00:00
end
2018-11-20 17:44:52 +00:00
Capybara . register_driver :selenium_firefox_not_clear_storage do | app |
2016-10-28 20:08:20 +00:00
Capybara :: Selenium :: Driver . new (
app ,
browser : :firefox ,
2018-11-20 17:44:52 +00:00
clear_local_storage : false ,
clear_session_storage : false ,
2017-08-11 18:37:43 +00:00
options : browser_options
2016-10-28 20:08:20 +00:00
)
end
2016-09-01 22:46:56 +00:00
module TestSessions
2018-11-18 21:27:00 +00:00
SeleniumFirefox = Capybara :: Session . new ( :selenium_firefox , TestApp )
2016-09-01 22:46:56 +00:00
end
2018-03-01 00:11:41 +00:00
skipped_tests = % i [ response_headers status_code trigger ]
2016-09-01 22:46:56 +00:00
2019-04-19 19:22:17 +00:00
Capybara :: SpecHelper . log_selenium_driver_version ( Selenium :: WebDriver :: Firefox ) if ENV [ 'CI' ]
2018-03-13 21:22:50 +00:00
2018-11-18 21:27:00 +00:00
Capybara :: SpecHelper . run_specs TestSessions :: SeleniumFirefox , 'selenium' , capybara_skip : skipped_tests do | example |
2018-07-05 18:50:43 +00:00
case example . metadata [ :full_description ]
when 'Capybara::Session selenium node #click should allow multiple modifiers'
2019-11-19 18:35:04 +00:00
pending " Firefox on OSX doesn't generate an event for shift+control+click " if firefox_gte? ( 62 , @session ) && Selenium :: WebDriver :: Platform . mac?
2018-07-05 18:50:43 +00:00
when / ^Capybara::Session selenium node # double_click /
2018-11-18 21:27:00 +00:00
pending " selenium-webdriver/geckodriver doesn't generate double click event " if firefox_lt? ( 59 , @session )
2018-07-05 18:50:43 +00:00
when 'Capybara::Session selenium #accept_prompt should accept the prompt with a blank response when there is a default'
2018-11-18 21:27:00 +00:00
pending " Geckodriver doesn't set a blank response in FF < 63 - https://bugzilla.mozilla.org/show_bug.cgi?id=1486485 " if firefox_lt? ( 63 , @session )
2018-07-24 21:13:03 +00:00
when 'Capybara::Session selenium #attach_file with multipart form should fire change once when uploading multiple files from empty'
2018-11-18 21:27:00 +00:00
pending " FF < 62 doesn't support setting all files at once " if firefox_lt? ( 62 , @session )
2018-09-06 01:19:07 +00:00
when 'Capybara::Session selenium #accept_confirm should work with nested modals'
2019-06-17 22:11:35 +00:00
skip 'Broken in 63 <= FF < 69 - https://bugzilla.mozilla.org/show_bug.cgi?id=1487358' if firefox_gte? ( 63 , @session ) && firefox_lt? ( 69 , @session )
2019-09-07 16:12:26 +00:00
skip 'Hangs in 69 <= FF < 71 - Dont know what issue for this - previous issue was closed as fixed but it is not' if firefox_gte? ( 69 , @session ) && firefox_lt? ( 71 , @session )
2019-12-22 17:37:42 +00:00
skip 'Broken again intermittently in FF 71 - jus skip it'
2018-09-14 20:00:29 +00:00
when 'Capybara::Session selenium #click_link can download a file'
skip 'Need to figure out testing of file downloading on windows platform' if Gem . win_platform?
2018-10-02 21:26:31 +00:00
when 'Capybara::Session selenium #reset_session! removes ALL cookies'
pending " Geckodriver doesn't provide a way to remove cookies outside the current domain "
2019-05-15 21:25:50 +00:00
when / drag_to.*HTML5 /
pending " Firefox < 62 doesn't support a DataTransfer constuctor " if firefox_lt? ( 62 . 0 , @session )
2019-10-21 03:34:34 +00:00
when 'Capybara::Session selenium #accept_alert should handle the alert if the page changes' ,
'Capybara::Session selenium #accept_alert with an asynchronous alert should accept the alert'
skip 'No clue what Firefox is doing here - works fine on MacOS locally'
2018-07-05 18:50:43 +00:00
end
end
2016-09-01 22:46:56 +00:00
2018-07-10 21:18:39 +00:00
RSpec . describe 'Capybara::Session with firefox' do # rubocop:disable RSpec/MultipleDescribes
2017-05-08 22:12:33 +00:00
include Capybara :: SpecHelper
2019-05-03 01:57:14 +00:00
[ 'Capybara::Session' , 'Capybara::Node' , Capybara :: RSpecMatchers ] . each do | examples |
include_examples examples , TestSessions :: SeleniumFirefox , :selenium_firefox
end
2019-03-07 21:50:59 +00:00
describe 'filling in Firefox-specific date and time fields with keystrokes' do
let ( :datetime ) { Time . new ( 1983 , 6 , 19 , 6 , 30 ) }
let ( :session ) { TestSessions :: SeleniumFirefox }
before do
session . visit ( '/form' )
end
it 'should fill in a date input with a String' do
session . fill_in ( 'form_date' , with : datetime . to_date . iso8601 )
session . click_button ( 'awesome' )
expect ( Date . parse ( extract_results ( session ) [ 'date' ] ) ) . to eq datetime . to_date
end
it 'should fill in a time input with a String' do
session . fill_in ( 'form_time' , with : datetime . to_time . strftime ( '%T' ) )
session . click_button ( 'awesome' )
results = extract_results ( session ) [ 'time' ]
expect ( Time . parse ( results ) . strftime ( '%r' ) ) . to eq datetime . strftime ( '%r' )
end
it 'should fill in a datetime input with a String' do
# FF doesn't currently support datetime-local so this is really just a text input
session . fill_in ( 'form_datetime' , with : datetime . iso8601 )
session . click_button ( 'awesome' )
expect ( Time . parse ( extract_results ( session ) [ 'datetime' ] ) ) . to eq datetime
end
end
2016-09-01 22:46:56 +00:00
end
RSpec . describe Capybara :: Selenium :: Driver do
2019-07-15 21:49:21 +00:00
let ( :driver ) { described_class . new ( TestApp , browser : :firefox , options : browser_options ) }
2016-09-01 22:46:56 +00:00
describe '#quit' do
2018-07-10 21:18:39 +00:00
it 'should reset browser when quit' do
2019-02-25 23:50:24 +00:00
expect ( driver . browser ) . to be_truthy
driver . quit
2018-03-01 00:11:41 +00:00
# access instance variable directly so we don't create a new browser instance
2019-02-25 23:50:24 +00:00
expect ( driver . instance_variable_get ( :@browser ) ) . to be_nil
2016-09-01 22:46:56 +00:00
end
2016-11-23 19:53:57 +00:00
2018-07-10 21:18:39 +00:00
context 'with errors' do
2019-02-25 23:50:24 +00:00
let! ( :original_browser ) { driver . browser }
2018-06-19 20:57:42 +00:00
2016-11-23 19:53:57 +00:00
after do
# Ensure browser is actually quit so we don't leave hanging processe
2019-02-25 23:50:24 +00:00
RSpec :: Mocks . space . proxy_for ( original_browser ) . reset
original_browser . quit
2016-11-23 19:53:57 +00:00
end
2018-07-10 21:18:39 +00:00
it 'warns UnknownError returned during quit because the browser is probably already gone' do
2019-02-25 23:50:24 +00:00
allow ( driver ) . to receive ( :warn )
allow ( driver . browser ) . to (
2016-11-23 19:53:57 +00:00
receive ( :quit )
2018-07-10 21:18:39 +00:00
. and_raise ( Selenium :: WebDriver :: Error :: UnknownError , 'random message' )
2016-11-23 19:53:57 +00:00
)
2019-02-25 23:50:24 +00:00
expect { driver . quit } . not_to raise_error
expect ( driver . instance_variable_get ( :@browser ) ) . to be_nil
expect ( driver ) . to have_received ( :warn ) . with ( / random message / )
2016-11-23 19:53:57 +00:00
end
2018-07-10 21:18:39 +00:00
it 'ignores silenced UnknownError returned during quit because the browser is almost definitely already gone' do
2019-02-25 23:50:24 +00:00
allow ( driver ) . to receive ( :warn )
allow ( driver . browser ) . to (
2016-11-23 19:53:57 +00:00
receive ( :quit )
2018-07-10 21:18:39 +00:00
. and_raise ( Selenium :: WebDriver :: Error :: UnknownError , 'Error communicating with the remote browser' )
2016-11-23 19:53:57 +00:00
)
2019-02-25 23:50:24 +00:00
expect { driver . quit } . not_to raise_error
expect ( driver . instance_variable_get ( :@browser ) ) . to be_nil
expect ( driver ) . not_to have_received ( :warn )
2016-11-23 19:53:57 +00:00
end
end
2016-09-01 22:46:56 +00:00
end
2016-10-28 20:08:20 +00:00
2018-07-10 21:18:39 +00:00
context 'storage' do
describe '#reset!' do
2018-11-20 17:44:52 +00:00
it 'clears storage by default' do
2019-02-25 23:50:24 +00:00
session = TestSessions :: SeleniumFirefox
session . visit ( '/with_js' )
session . find ( :css , '#set-storage' ) . click
session . reset!
session . visit ( '/with_js' )
expect ( session . driver . browser . local_storage . keys ) . to be_empty
expect ( session . driver . browser . session_storage . keys ) . to be_empty
2016-10-28 20:08:20 +00:00
end
2018-11-20 17:44:52 +00:00
it 'does not clear storage when false' do
2019-02-25 23:50:24 +00:00
session = Capybara :: Session . new ( :selenium_firefox_not_clear_storage , TestApp )
session . visit ( '/with_js' )
session . find ( :css , '#set-storage' ) . click
session . reset!
session . visit ( '/with_js' )
expect ( session . driver . browser . local_storage . keys ) . not_to be_empty
expect ( session . driver . browser . session_storage . keys ) . not_to be_empty
2016-10-28 20:08:20 +00:00
end
end
end
2018-11-20 22:20:46 +00:00
context 'timeout' do
it 'sets the http client read timeout' do
expect ( TestSessions :: SeleniumFirefox . driver . browser . send ( :bridge ) . http . read_timeout ) . to eq 31
end
end
2016-09-01 22:46:56 +00:00
end
2018-06-06 20:21:33 +00:00
RSpec . describe Capybara :: Selenium :: Node do
2019-10-16 00:40:27 +00:00
describe '#click' do
2018-07-10 21:18:39 +00:00
it 'warns when attempting on a table row' do
2018-11-18 21:27:00 +00:00
session = TestSessions :: SeleniumFirefox
2018-06-06 20:21:33 +00:00
session . visit ( '/tables' )
tr = session . find ( :css , '#agent_table tr:first-child' )
allow ( tr . base ) . to receive ( :warn )
tr . click
2018-06-06 23:11:47 +00:00
expect ( tr . base ) . to have_received ( :warn ) . with ( / Clicking the first cell in the row instead / )
2018-06-06 20:21:33 +00:00
end
2018-07-03 20:31:31 +00:00
2018-07-10 21:18:39 +00:00
it 'should allow multiple modifiers' , requires : [ :js ] do
2018-11-18 21:27:00 +00:00
session = TestSessions :: SeleniumFirefox
2018-07-03 20:31:31 +00:00
session . visit ( 'with_js' )
2018-07-05 23:29:26 +00:00
# Firefox v62+ doesn't generate an event for control+shift+click
2018-07-03 20:31:31 +00:00
session . find ( :css , '#click-test' ) . click ( :alt , :ctrl , :meta )
2018-07-05 23:29:26 +00:00
# it also triggers a contextmenu event when control is held so don't check click type
2018-07-10 21:18:39 +00:00
expect ( session ) . to have_link ( 'Has been alt control meta' )
2018-07-03 20:31:31 +00:00
end
2018-06-06 20:21:33 +00:00
end
end