/usr/lib/ruby/vendor_ruby/capybara/spec/session/assert_text.rb is in ruby-capybara 2.10.2-1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 | # frozen_string_literal: true
Capybara::SpecHelper.spec '#assert_text' do
it "should be true if the given text is on the page" do
@session.visit('/with_html')
expect(@session.assert_text('est')).to eq(true)
expect(@session.assert_text('Lorem')).to eq(true)
expect(@session.assert_text('Redirect')).to eq(true)
expect(@session.assert_text(:'Redirect')).to eq(true)
expect(@session.assert_text('text with whitespace')).to eq(true)
expect(@session.assert_text("text with \n\n whitespace")).to eq(true)
end
it "should take scopes into account" do
@session.visit('/with_html')
@session.within("//a[@title='awesome title']") do
expect(@session.assert_text('labore')).to eq(true)
end
end
it "should raise if scoped to an element which does not have the text" do
@session.visit('/with_html')
@session.within("//a[@title='awesome title']") do
expect do
@session.assert_text('monkey')
end.to raise_error(Capybara::ExpectationNotMet, 'expected to find text "monkey" in "labore"')
end
end
it "should be true if :all given and text is invisible." do
@session.visit('/with_html')
expect(@session.assert_text(:all, 'Some of this text is hidden!')).to eq(true)
end
it "should be true if `Capybara.ignore_hidden_elements = true` and text is invisible." do
Capybara.ignore_hidden_elements = false
@session.visit('/with_html')
expect(@session.assert_text('Some of this text is hidden!')).to eq(true)
end
it "should raise error with a helpful message if the requested text is present but invisible" do
@session.visit('/with_html')
el = @session.find(:css, '#hidden-text')
expect do
el.assert_text(:visible, 'Some of this text is hidden!')
end.to raise_error(Capybara::ExpectationNotMet, /it was found 1 time including non-visible text/)
end
it "should raise error with a helpful message if the requested text is present but with incorrect case" do
@session.visit('/with_html')
expect do
@session.assert_text('Text With Whitespace')
end.to raise_error(Capybara::ExpectationNotMet, /it was found 1 time using a case insensitive search/)
end
it "should raise the correct error if requested text is missing but contains regex special characters" do
@session.visit('/with_html')
expect do
@session.assert_text('[]*.')
end.to raise_error(Capybara::ExpectationNotMet, /expected to find text "\[\]\*\."/)
end
it "should be true if the text in the page matches given regexp" do
@session.visit('/with_html')
expect(@session.assert_text(/Lorem/)).to eq(true)
end
it "should be raise error if the text in the page doesn't match given regexp" do
@session.visit('/with_html')
expect do
@session.assert_text(/xxxxyzzz/)
end.to raise_error(Capybara::ExpectationNotMet, /\Aexpected to find text matching \/xxxxyzzz\/ in "This is a test Header Class(.+)"\Z/)
end
it "should escape any characters that would have special meaning in a regexp" do
@session.visit('/with_html')
expect do
@session.assert_text('.orem')
end.to raise_error(Capybara::ExpectationNotMet)
end
it "should wait for text to appear", requires: [:js] do
@session.visit('/with_js')
@session.click_link('Click me')
expect(@session.assert_text('Has been clicked')).to eq(true)
end
context "with between" do
it "should be true if the text occurs within the range given" do
@session.visit('/with_count')
expect(@session.assert_text('count', between: 1..3)).to eq(true)
end
it "should be false if the text occurs more or fewer times than range" do
@session.visit('/with_html')
expect do
@session.find(:css, '.number').assert_text(/\d/, between: 0..1)
end.to raise_error(Capybara::ExpectationNotMet, "expected to find text matching /\\d/ between 0 and 1 times but found 2 times in \"42\"")
end
end
context "with wait", requires: [:js] do
it "should find element if it appears before given wait duration" do
Capybara.using_wait_time(0) do
@session.visit('/with_js')
@session.find(:css, '#reload-list').click
@session.find(:css, '#the-list').assert_text('Foo Bar', wait: 0.9)
end
end
it "should raise error if it appears after given wait duration" do
Capybara.using_wait_time(0) do
@session.visit('/with_js')
@session.find(:css, '#reload-list').click
el = @session.find(:css, '#the-list', visible: false)
expect do
el.assert_text(:all, 'Foo Bar', wait: 0.3)
end.to raise_error(Capybara::ExpectationNotMet)
end
end
end
context 'with multiple count filters' do
before(:each) do
@session.visit('/with_html')
end
it 'ignores other filters when :count is specified' do
o = {count: 5,
minimum: 6,
maximum: 0,
between: 0..4}
expect { @session.assert_text('Header', o) }.not_to raise_error
end
context 'with no :count expectation' do
it 'fails if :minimum is not met' do
o = {minimum: 6,
maximum: 5,
between: 2..7}
expect { @session.assert_text('Header', o) }.to raise_error(Capybara::ExpectationNotMet)
end
it 'fails if :maximum is not met' do
o = {minimum: 0,
maximum: 0,
between: 2..7}
expect { @session.assert_text('Header', o) }.to raise_error(Capybara::ExpectationNotMet)
end
it 'fails if :between is not met' do
o = {minimum: 0,
maximum: 5,
between: 0..4}
expect { @session.assert_text('Header', o) }.to raise_error(Capybara::ExpectationNotMet)
end
it 'succeeds if all combineable expectations are met' do
o = {minimum: 0,
maximum: 5,
between: 2..7}
expect { @session.assert_text('Header', o) }.not_to raise_error
end
end
end
end
Capybara::SpecHelper.spec '#assert_no_text' do
it "should raise error if the given text is on the page at least once" do
@session.visit('/with_html')
expect do
@session.assert_no_text('Lorem')
end.to raise_error(Capybara::ExpectationNotMet, /\Aexpected not to find text "Lorem" in "This is a test Header Class.+"\Z/)
end
it "should be true if scoped to an element which does not have the text" do
@session.visit('/with_html')
@session.within("//a[@title='awesome title']") do
expect(@session.assert_no_text('monkey')).to eq(true)
end
end
it "should be true if the given text is on the page but not visible" do
@session.visit('/with_html')
expect(@session.assert_no_text('Inside element with hidden ancestor')).to eq(true)
end
it "should raise error if :all given and text is invisible." do
@session.visit('/with_html')
el = @session.find(:css, '#hidden-text', visible: false)
expect do
el.assert_no_text(:all, 'Some of this text is hidden!')
end.to raise_error(Capybara::ExpectationNotMet, 'expected not to find text "Some of this text is hidden!" in "Some of this text is hidden!"')
end
it "should raise error if :all given and text is invisible." do
@session.visit('/with_html')
el = @session.find(:css, '#some-hidden-text', visible: false)
expect do
el.assert_no_text(:visible, 'hidden')
end.to raise_error(Capybara::ExpectationNotMet, 'expected not to find text "hidden" in "Some of this text is not hidden"')
end
it "should be true if the text in the page doesn't match given regexp" do
@session.visit('/with_html')
@session.assert_no_text(/xxxxyzzz/)
end
context "with count" do
it "should be true if the text occurs within the range given" do
@session.visit('/with_count')
expect(@session.assert_text('count', count: 2)).to eq(true)
end
it "should be false if the text occurs more or fewer times than range" do
@session.visit('/with_html')
expect do
@session.find(:css, '.number').assert_text(/\d/, count: 1)
end.to raise_error(Capybara::ExpectationNotMet, "expected to find text matching /\\d/ 1 time but found 2 times in \"42\"")
end
end
context "with wait", requires: [:js] do
it "should not find element if it appears after given wait duration" do
@session.visit('/with_js')
@session.click_link('Click me')
@session.find(:css, '#reload-list').click
@session.find(:css, '#the-list').assert_no_text('Foo Bar', wait: 0.3)
end
end
end
|