2016-03-07 19:52:19 -05:00
|
|
|
# frozen_string_literal: true
|
2018-02-28 19:11:41 -05:00
|
|
|
|
2010-11-12 15:46:46 -05:00
|
|
|
require 'spec_helper'
|
|
|
|
|
2014-04-03 13:25:03 -04:00
|
|
|
RSpec.describe Capybara do
|
2020-07-04 17:05:29 -04:00
|
|
|
include Capybara::RSpecMatchers
|
2010-11-12 15:46:46 -05:00
|
|
|
describe '.string' do
|
|
|
|
let :string do
|
2019-07-15 17:49:21 -04:00
|
|
|
described_class.string <<-STRING
|
2013-02-16 03:59:07 -05:00
|
|
|
<html>
|
|
|
|
<head>
|
|
|
|
<title>simple_node</title>
|
|
|
|
</head>
|
|
|
|
<body>
|
2015-05-28 13:51:23 -04:00
|
|
|
<svg><title>not document title</title></svg>
|
2013-02-16 03:59:07 -05:00
|
|
|
<div id="page">
|
|
|
|
<div id="content">
|
|
|
|
<h1 data="fantastic">Totally awesome</h1>
|
|
|
|
<p>Yes it is</p>
|
|
|
|
</div>
|
|
|
|
|
|
|
|
<form>
|
|
|
|
<input type="text" name="bleh" disabled="disabled"/>
|
|
|
|
<input type="text" name="meh"/>
|
|
|
|
</form>
|
|
|
|
|
|
|
|
<div id="footer">
|
|
|
|
<p>c2010</p>
|
|
|
|
<p>Jonas Nicklas</p>
|
|
|
|
<input type="text" name="foo" value="bar"/>
|
|
|
|
<select name="animal">
|
|
|
|
<option>Monkey</option>
|
|
|
|
<option selected="selected">Capybara</option>
|
|
|
|
</select>
|
|
|
|
</div>
|
|
|
|
|
|
|
|
<div id="hidden" style="display: none">
|
|
|
|
<p id="secret">Secret</p>
|
|
|
|
</div>
|
|
|
|
|
|
|
|
<section>
|
|
|
|
<div class="subsection"></div>
|
|
|
|
</section>
|
|
|
|
</div>
|
|
|
|
</body>
|
|
|
|
</html>
|
2010-11-12 15:46:46 -05:00
|
|
|
STRING
|
|
|
|
end
|
|
|
|
|
2018-07-10 17:18:39 -04:00
|
|
|
it 'allows using matchers' do
|
2013-11-14 12:43:36 -05:00
|
|
|
expect(string).to have_css('#page')
|
|
|
|
expect(string).not_to have_css('#does-not-exist')
|
2010-11-12 15:46:46 -05:00
|
|
|
end
|
|
|
|
|
2018-07-10 17:18:39 -04:00
|
|
|
it 'allows using custom matchers' do
|
2019-07-15 17:49:21 -04:00
|
|
|
described_class.add_selector :lifeform do
|
2016-10-26 15:24:51 -04:00
|
|
|
xpath { |name| ".//option[contains(.,'#{name}')]" }
|
2010-11-12 15:46:46 -05:00
|
|
|
end
|
2018-07-10 17:18:39 -04:00
|
|
|
expect(string).to have_selector(:id, 'page')
|
2013-11-14 12:43:36 -05:00
|
|
|
expect(string).not_to have_selector(:id, 'does-not-exist')
|
2018-07-10 17:18:39 -04:00
|
|
|
expect(string).to have_selector(:lifeform, 'Monkey')
|
|
|
|
expect(string).not_to have_selector(:lifeform, 'Gorilla')
|
2010-11-12 15:46:46 -05:00
|
|
|
end
|
|
|
|
|
2011-09-29 16:04:51 -04:00
|
|
|
it 'allows custom matcher using css' do
|
2019-07-15 17:49:21 -04:00
|
|
|
described_class.add_selector :section do
|
2011-09-29 16:04:51 -04:00
|
|
|
css { |css_class| "section .#{css_class}" }
|
|
|
|
end
|
2013-11-14 12:43:36 -05:00
|
|
|
expect(string).to have_selector(:section, 'subsection')
|
|
|
|
expect(string).not_to have_selector(:section, 'section_8')
|
2011-09-29 16:04:51 -04:00
|
|
|
end
|
|
|
|
|
2018-07-10 17:18:39 -04:00
|
|
|
it 'allows using matchers with text option' do
|
2016-10-04 14:10:29 -04:00
|
|
|
expect(string).to have_css('h1', text: 'Totally awesome')
|
|
|
|
expect(string).not_to have_css('h1', text: 'Not so awesome')
|
2010-11-12 15:46:46 -05:00
|
|
|
end
|
|
|
|
|
2018-07-10 17:18:39 -04:00
|
|
|
it 'allows finding only visible nodes' do
|
2016-10-04 14:10:29 -04:00
|
|
|
expect(string.all(:css, '#secret', visible: true)).to be_empty
|
|
|
|
expect(string.all(:css, '#secret', visible: false).size).to eq(1)
|
2010-11-12 15:46:46 -05:00
|
|
|
end
|
|
|
|
|
2018-07-10 17:18:39 -04:00
|
|
|
it 'allows finding elements and extracting text from them' do
|
2013-11-14 12:43:36 -05:00
|
|
|
expect(string.find('//h1').text).to eq('Totally awesome')
|
2010-11-12 15:46:46 -05:00
|
|
|
end
|
|
|
|
|
2018-07-10 17:18:39 -04:00
|
|
|
it 'allows finding elements and extracting attributes from them' do
|
2013-11-14 12:43:36 -05:00
|
|
|
expect(string.find('//h1')[:data]).to eq('fantastic')
|
2010-11-12 15:46:46 -05:00
|
|
|
end
|
|
|
|
|
2018-07-10 17:18:39 -04:00
|
|
|
it 'allows finding elements and extracting the tag name from them' do
|
2013-11-14 12:43:36 -05:00
|
|
|
expect(string.find('//h1').tag_name).to eq('h1')
|
2010-11-12 15:46:46 -05:00
|
|
|
end
|
|
|
|
|
2018-07-10 17:18:39 -04:00
|
|
|
it 'allows finding elements and extracting the path' do
|
2013-11-14 12:43:36 -05:00
|
|
|
expect(string.find('//h1').path).to eq('/html/body/div/div[1]/h1')
|
2010-11-12 15:46:46 -05:00
|
|
|
end
|
|
|
|
|
2018-07-10 17:18:39 -04:00
|
|
|
it 'allows finding elements and extracting the value' do
|
2013-11-14 12:43:36 -05:00
|
|
|
expect(string.find('//div/input').value).to eq('bar')
|
|
|
|
expect(string.find('//select').value).to eq('Capybara')
|
2010-11-12 15:46:46 -05:00
|
|
|
end
|
|
|
|
|
2018-07-10 17:18:39 -04:00
|
|
|
it 'allows finding elements and checking if they are visible' do
|
2013-11-14 12:43:36 -05:00
|
|
|
expect(string.find('//h1')).to be_visible
|
2018-07-10 17:18:39 -04:00
|
|
|
expect(string.find(:css, '#secret', visible: false)).not_to be_visible
|
2013-01-29 03:54:14 -05:00
|
|
|
end
|
|
|
|
|
2018-07-10 17:18:39 -04:00
|
|
|
it 'allows finding elements and checking if they are disabled' do
|
2013-11-14 12:43:36 -05:00
|
|
|
expect(string.find('//form/input[@name="bleh"]')).to be_disabled
|
|
|
|
expect(string.find('//form/input[@name="meh"]')).not_to be_disabled
|
2010-11-12 15:46:46 -05:00
|
|
|
end
|
2013-02-16 03:59:07 -05:00
|
|
|
|
2021-03-14 16:59:19 -04:00
|
|
|
it 'allows finding siblings' do
|
|
|
|
h1 = string.find(:css, 'h1')
|
|
|
|
expect(h1).to have_sibling(:css, 'p', text: 'Yes it is')
|
|
|
|
expect(h1).not_to have_sibling(:css, 'p', text: 'Jonas Nicklas')
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'allows finding ancestor' do
|
|
|
|
h1 = string.find(:css, 'h1')
|
|
|
|
expect(h1).to have_ancestor(:css, '#content')
|
|
|
|
expect(h1).not_to have_ancestor(:css, '#footer')
|
|
|
|
end
|
|
|
|
|
2018-12-06 17:23:35 -05:00
|
|
|
it 'drops illegal fragments when using gumbo' do
|
2019-01-16 12:53:09 -05:00
|
|
|
skip 'libxml is less strict than Gumbo' unless Nokogiri.respond_to?(:HTML5)
|
2021-08-07 21:51:57 -04:00
|
|
|
described_class.use_html5_parsing = true
|
2019-07-15 17:49:21 -04:00
|
|
|
expect(described_class.string('<td>1</td>')).not_to have_css('td')
|
2018-12-06 17:23:35 -05:00
|
|
|
end
|
|
|
|
|
2021-08-07 21:51:57 -04:00
|
|
|
it 'can disable use of HTML5 parsing' do
|
|
|
|
skip "Test doesn't make sense unlesss HTML5 parsing is loaded (Nokogumbo or Nokogiri >= 1.12.0)" unless Nokogiri.respond_to?(:HTML5)
|
|
|
|
described_class.use_html5_parsing = false
|
2019-07-15 17:49:21 -04:00
|
|
|
expect(described_class.string('<td>1</td>')).to have_css('td')
|
2018-12-06 17:23:35 -05:00
|
|
|
end
|
|
|
|
|
2018-07-10 17:18:39 -04:00
|
|
|
describe '#title' do
|
|
|
|
it 'returns the page title' do
|
|
|
|
expect(string.title).to eq('simple_node')
|
2013-02-16 03:59:07 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2018-07-10 17:18:39 -04:00
|
|
|
describe '#has_title?' do
|
|
|
|
it 'returns whether the page has the given title' do
|
2020-07-03 18:26:54 -04:00
|
|
|
expect(string.has_title?('simple_node')).to be true
|
|
|
|
expect(string.has_title?('monkey')).to be false
|
2013-02-16 03:59:07 -05:00
|
|
|
end
|
|
|
|
|
2018-07-10 17:18:39 -04:00
|
|
|
it 'allows regexp matches' do
|
2020-07-03 18:26:54 -04:00
|
|
|
expect(string.has_title?(/s[a-z]+_node/)).to be true
|
|
|
|
expect(string.has_title?(/monkey/)).to be false
|
2013-02-16 03:59:07 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe '#has_no_title?' do
|
2018-07-10 17:18:39 -04:00
|
|
|
it 'returns whether the page does not have the given title' do
|
2020-07-03 18:26:54 -04:00
|
|
|
expect(string.has_no_title?('simple_node')).to be false
|
|
|
|
expect(string.has_no_title?('monkey')).to be true
|
2013-02-16 03:59:07 -05:00
|
|
|
end
|
|
|
|
|
2018-07-10 17:18:39 -04:00
|
|
|
it 'allows regexp matches' do
|
2020-07-03 18:26:54 -04:00
|
|
|
expect(string.has_no_title?(/s[a-z]+_node/)).to be false
|
|
|
|
expect(string.has_no_title?(/monkey/)).to be true
|
2013-02-16 03:59:07 -05:00
|
|
|
end
|
|
|
|
end
|
2010-11-12 15:46:46 -05:00
|
|
|
end
|
|
|
|
end
|