2016-03-08 00:52:19 +00:00
|
|
|
# frozen_string_literal: true
|
2018-03-01 00:11:41 +00:00
|
|
|
|
2010-09-17 23:56:32 +00:00
|
|
|
require 'spec_helper'
|
2013-11-14 21:25:00 +00:00
|
|
|
require "selenium-webdriver"
|
|
|
|
|
2016-09-01 22:46:56 +00:00
|
|
|
RSpec.shared_examples "Capybara::Session" do |session, mode|
|
2018-03-01 00:11:41 +00:00
|
|
|
let(:session) { session }
|
2016-07-26 18:15:35 +00:00
|
|
|
|
2009-11-14 09:44:46 +00:00
|
|
|
context 'with selenium driver' do
|
|
|
|
before do
|
2016-09-01 22:46:56 +00:00
|
|
|
@session = session
|
2009-11-14 09:44:46 +00:00
|
|
|
end
|
2009-12-11 21:40:23 +00:00
|
|
|
|
2009-11-14 09:44:46 +00:00
|
|
|
describe '#driver' do
|
2009-12-11 21:40:23 +00:00
|
|
|
it "should be a selenium driver" do
|
2013-11-14 17:43:36 +00:00
|
|
|
expect(@session.driver).to be_an_instance_of(Capybara::Selenium::Driver)
|
2009-11-14 09:44:46 +00:00
|
|
|
end
|
|
|
|
end
|
2009-12-11 21:40:23 +00:00
|
|
|
|
2009-11-14 09:44:46 +00:00
|
|
|
describe '#mode' do
|
|
|
|
it "should remember the mode" do
|
2016-09-01 22:46:56 +00:00
|
|
|
expect(@session.mode).to eq(mode)
|
2009-11-14 09:44:46 +00:00
|
|
|
end
|
|
|
|
end
|
2009-12-11 21:40:23 +00:00
|
|
|
|
2014-01-17 07:57:06 +00:00
|
|
|
describe "#reset!" do
|
|
|
|
it "freshly reset session should not be touched" do
|
|
|
|
@session.instance_variable_set(:@touched, true)
|
|
|
|
@session.reset!
|
2013-11-14 17:43:36 +00:00
|
|
|
expect(@session.instance_variable_get(:@touched)).to eq false
|
2014-01-17 07:57:06 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2012-07-13 11:15:44 +00:00
|
|
|
describe "exit codes" do
|
|
|
|
before do
|
|
|
|
@current_dir = Dir.getwd
|
2012-07-13 11:28:33 +00:00
|
|
|
Dir.chdir(File.join(File.dirname(__FILE__), '..'))
|
2017-05-04 19:35:46 +00:00
|
|
|
@env = { 'SELENIUM_BROWSER' => @session.driver.options[:browser].to_s }
|
2012-07-13 11:15:44 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
after do
|
|
|
|
Dir.chdir(@current_dir)
|
|
|
|
end
|
|
|
|
|
|
|
|
it "should have return code 1 when running selenium_driver_rspec_failure.rb" do
|
2017-08-02 19:10:56 +00:00
|
|
|
skip if ENV['HEADLESS']
|
2017-05-04 19:35:46 +00:00
|
|
|
system(@env, 'rspec spec/fixtures/selenium_driver_rspec_failure.rb', out: File::NULL, err: File::NULL)
|
2018-03-01 00:11:41 +00:00
|
|
|
expect($CHILD_STATUS.exitstatus).to eq(1)
|
2012-07-13 11:15:44 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
it "should have return code 0 when running selenium_driver_rspec_success.rb" do
|
2017-08-02 19:10:56 +00:00
|
|
|
skip if ENV['HEADLESS']
|
2017-05-04 19:35:46 +00:00
|
|
|
system(@env, 'rspec spec/fixtures/selenium_driver_rspec_success.rb', out: File::NULL, err: File::NULL)
|
2018-03-01 00:11:41 +00:00
|
|
|
expect($CHILD_STATUS.exitstatus).to eq(0)
|
2012-07-13 11:15:44 +00:00
|
|
|
end
|
|
|
|
end
|
2015-04-13 03:32:12 +00:00
|
|
|
|
2014-07-03 02:31:13 +00:00
|
|
|
describe "#accept_alert" do
|
2014-07-03 02:10:30 +00:00
|
|
|
it "supports a blockless mode" do
|
|
|
|
@session.visit('/with_js')
|
|
|
|
@session.click_link('Open alert')
|
|
|
|
@session.accept_alert
|
2018-03-01 00:11:41 +00:00
|
|
|
expect { @session.driver.browser.switch_to.alert }.to raise_error(@session.driver.send(:modal_error))
|
2014-07-03 02:10:30 +00:00
|
|
|
end
|
2017-08-14 23:25:57 +00:00
|
|
|
|
2018-02-12 18:07:06 +00:00
|
|
|
it "can be called before visiting" do
|
2018-03-17 11:55:00 +00:00
|
|
|
skip "Edge driver doesn't get any interactions when alert is set" if edge?(@session)
|
2018-02-12 18:07:06 +00:00
|
|
|
@session.accept_alert "Initial alert" do
|
|
|
|
@session.visit('/initial_alert')
|
|
|
|
end
|
|
|
|
expect(@session).to have_text('Initial alert page')
|
2017-08-14 23:25:57 +00:00
|
|
|
end
|
2014-07-03 02:10:30 +00:00
|
|
|
end
|
2015-04-13 03:32:12 +00:00
|
|
|
|
2017-09-07 16:58:14 +00:00
|
|
|
context '#fill_in_with empty string and no options' do
|
|
|
|
it 'should trigger change when clearing a field' do
|
|
|
|
@session.visit('/with_js')
|
|
|
|
@session.fill_in('with_change_event', with: '')
|
|
|
|
# click outside the field to trigger the change event
|
|
|
|
@session.find(:css, 'body').click
|
|
|
|
expect(@session).to have_selector(:css, '.change_event_triggered', match: :one)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2016-10-04 18:10:29 +00:00
|
|
|
context "#fill_in with { :clear => :backspace } fill_option", requires: [:js] do
|
2015-04-13 03:32:12 +00:00
|
|
|
it 'should fill in a field, replacing an existing value' do
|
|
|
|
@session.visit('/form')
|
2018-03-01 00:11:41 +00:00
|
|
|
@session.fill_in('form_first_name',
|
|
|
|
with: 'Harry',
|
|
|
|
fill_options: { clear: :backspace })
|
2015-04-13 03:32:12 +00:00
|
|
|
expect(@session.find(:fillable_field, 'form_first_name').value).to eq('Harry')
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'should only trigger onchange once' do
|
|
|
|
@session.visit('/with_js')
|
2018-03-01 00:11:41 +00:00
|
|
|
@session.fill_in('with_change_event',
|
|
|
|
with: 'some value',
|
|
|
|
fill_options: { clear: :backspace })
|
2015-04-13 03:32:12 +00:00
|
|
|
# click outside the field to trigger the change event
|
|
|
|
@session.find(:css, 'body').click
|
2016-10-04 18:10:29 +00:00
|
|
|
expect(@session.find(:css, '.change_event_triggered', match: :one)).to have_text 'some value'
|
2015-04-13 03:32:12 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'should trigger change when clearing field' do
|
|
|
|
@session.visit('/with_js')
|
2018-03-01 00:11:41 +00:00
|
|
|
@session.fill_in('with_change_event',
|
|
|
|
with: '',
|
|
|
|
fill_options: { clear: :backspace })
|
2015-04-13 03:32:12 +00:00
|
|
|
# click outside the field to trigger the change event
|
|
|
|
@session.find(:css, 'body').click
|
2016-10-04 18:10:29 +00:00
|
|
|
expect(@session).to have_selector(:css, '.change_event_triggered', match: :one)
|
2015-04-13 03:32:12 +00:00
|
|
|
end
|
2017-09-07 16:58:14 +00:00
|
|
|
|
|
|
|
it 'should trigger input event field_value.length times' do
|
|
|
|
@session.visit('/with_js')
|
2018-03-01 00:11:41 +00:00
|
|
|
@session.fill_in('with_change_event',
|
|
|
|
with: '',
|
|
|
|
fill_options: { clear: :backspace })
|
2017-09-07 16:58:14 +00:00
|
|
|
# click outside the field to trigger the change event
|
|
|
|
@session.find(:css, 'body').click
|
|
|
|
expect(@session).to have_xpath('//p[@class="input_event_triggered"]', count: 13)
|
|
|
|
end
|
2015-04-13 03:32:12 +00:00
|
|
|
end
|
2015-08-09 16:55:56 +00:00
|
|
|
|
2015-04-13 04:25:13 +00:00
|
|
|
context "#fill_in with { clear: :none } fill_options" do
|
|
|
|
it 'should append to content in a field' do
|
|
|
|
@session.visit('/form')
|
2018-03-01 00:11:41 +00:00
|
|
|
@session.fill_in('form_first_name',
|
|
|
|
with: 'Harry',
|
|
|
|
fill_options: { clear: :none })
|
2015-04-13 04:25:13 +00:00
|
|
|
expect(@session.find(:fillable_field, 'form_first_name').value).to eq('JohnHarry')
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context "#fill_in with { clear: Array } fill_options" do
|
2016-04-05 16:33:19 +00:00
|
|
|
it 'should pass the array through to the element' do
|
2017-05-08 22:12:33 +00:00
|
|
|
pending "selenium-webdriver/geckodriver doesn't support complex sets of characters" if marionette?(@session)
|
2018-03-01 00:11:41 +00:00
|
|
|
# this is mainly for use with [[:control, 'a'], :backspace] - however since that is platform dependant I'm testing with something less useful
|
2015-04-13 04:25:13 +00:00
|
|
|
@session.visit('/form')
|
2018-03-01 00:11:41 +00:00
|
|
|
@session.fill_in('form_first_name',
|
|
|
|
with: 'Harry',
|
|
|
|
fill_options: { clear: [[:shift, 'abc'], :backspace] })
|
2015-04-13 04:25:13 +00:00
|
|
|
expect(@session.find(:fillable_field, 'form_first_name').value).to eq('JohnABHarry')
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2015-08-09 16:55:56 +00:00
|
|
|
describe "#path" do
|
|
|
|
it "returns xpath" do
|
2015-08-20 21:35:15 +00:00
|
|
|
# this is here because it is testing for an XPath that is specific to the algorithm used in the selenium driver
|
|
|
|
@session.visit('/path')
|
2015-08-10 13:53:38 +00:00
|
|
|
element = @session.find(:link, 'Second Link')
|
|
|
|
expect(element.path).to eq('/html/body/div[2]/a[1]')
|
2015-08-09 16:55:56 +00:00
|
|
|
end
|
|
|
|
end
|
2017-01-27 19:11:13 +00:00
|
|
|
|
2017-05-31 21:07:34 +00:00
|
|
|
describe "all with disappearing elements" do
|
|
|
|
it "ignores stale elements in results" do
|
|
|
|
@session.visit('/path')
|
2018-03-01 00:11:41 +00:00
|
|
|
elements = @session.all(:link) { |_node| raise Selenium::WebDriver::Error::StaleElementReferenceError }
|
2017-05-31 21:07:34 +00:00
|
|
|
expect(elements.size).to eq 0
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2017-01-27 19:11:13 +00:00
|
|
|
describe "#evaluate_script" do
|
2017-01-28 22:34:44 +00:00
|
|
|
it "can return an element" do
|
2017-01-27 19:11:13 +00:00
|
|
|
@session.visit('/form')
|
|
|
|
element = @session.evaluate_script("document.getElementById('form_title')")
|
|
|
|
expect(element).to eq @session.find(:id, 'form_title')
|
|
|
|
end
|
2017-01-28 22:34:44 +00:00
|
|
|
|
|
|
|
it "can return arrays of nested elements" do
|
|
|
|
@session.visit('/form')
|
|
|
|
elements = @session.evaluate_script('document.querySelectorAll("#form_city option")')
|
|
|
|
elements.each do |el|
|
|
|
|
expect(el).to be_instance_of Capybara::Node::Element
|
|
|
|
end
|
|
|
|
expect(elements).to eq @session.find(:css, '#form_city').all(:css, 'option').to_a
|
|
|
|
end
|
|
|
|
|
|
|
|
it "can return hashes with elements" do
|
|
|
|
@session.visit('/form')
|
|
|
|
result = @session.evaluate_script("{ a: document.getElementById('form_title'), b: {c: document.querySelectorAll('#form_city option')}}")
|
2018-03-01 00:11:41 +00:00
|
|
|
expect(result).to eq(
|
2017-01-28 22:34:44 +00:00
|
|
|
'a' => @session.find(:id, 'form_title'),
|
|
|
|
'b' => {
|
|
|
|
'c' => @session.find(:css, '#form_city').all(:css, 'option').to_a
|
|
|
|
}
|
2018-03-01 00:11:41 +00:00
|
|
|
)
|
2017-01-28 22:34:44 +00:00
|
|
|
end
|
2017-10-26 00:09:32 +00:00
|
|
|
|
|
|
|
describe "#evaluate_async_script" do
|
|
|
|
it "will timeout if the script takes too long" do
|
|
|
|
@session.visit('/with_js')
|
|
|
|
expect do
|
|
|
|
@session.using_wait_time(1) do
|
|
|
|
@session.evaluate_async_script("var cb = arguments[0]; setTimeout(function(){ cb(null) }, 3000)")
|
|
|
|
end
|
|
|
|
end.to raise_error Selenium::WebDriver::Error::ScriptTimeoutError
|
|
|
|
end
|
|
|
|
end
|
2017-01-27 19:11:13 +00:00
|
|
|
end
|
2017-03-21 22:34:18 +00:00
|
|
|
|
|
|
|
describe "Element#inspect" do
|
|
|
|
it "outputs obsolete elements" do
|
|
|
|
@session.visit('/form')
|
|
|
|
el = @session.find(:button, 'Click me!').click
|
2017-08-09 03:36:45 +00:00
|
|
|
expect(@session).to have_no_button('Click me!')
|
|
|
|
expect(el).not_to receive(:synchronize)
|
2017-03-21 22:34:18 +00:00
|
|
|
expect(el.inspect).to eq "Obsolete #<Capybara::Node::Element>"
|
|
|
|
end
|
|
|
|
end
|
2017-10-02 18:55:13 +00:00
|
|
|
|
|
|
|
describe "Element#click" do
|
|
|
|
it "should handle fixed headers/footers" do
|
|
|
|
@session.visit('/with_fixed_header_footer')
|
|
|
|
# @session.click_link('Go to root')
|
|
|
|
@session.find(:link, 'Go to root').click
|
|
|
|
expect(@session).to have_current_path('/')
|
|
|
|
end
|
|
|
|
end
|
2018-03-15 19:48:19 +00:00
|
|
|
|
|
|
|
context "Windows" do
|
|
|
|
it "can't close the primary window" do
|
|
|
|
expect do
|
|
|
|
@session.current_window.close
|
|
|
|
end.to raise_error(ArgumentError, 'Not allowed to close the primary window')
|
|
|
|
end
|
|
|
|
end
|
2009-11-14 09:44:46 +00:00
|
|
|
end
|
|
|
|
end
|