File: text_spec.rb

package info (click to toggle)
ruby-capybara 3.40.0%2Bds-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 2,368 kB
  • sloc: ruby: 23,988; javascript: 752; makefile: 11
file content (74 lines) | stat: -rw-r--r-- 2,650 bytes parent folder | download | duplicates (3)
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
# frozen_string_literal: true

Capybara::SpecHelper.spec '#text' do
  it 'should print the text of the page' do
    @session.visit('/with_simple_html')
    expect(@session.text).to eq('Bar')
  end

  it 'ignores invisible text by default' do
    @session.visit('/with_html')
    expect(@session.find(:id, 'hidden-text').text).to eq('Some of this text is')
  end

  it 'shows invisible text if `:all` given' do
    @session.visit('/with_html')
    expect(@session.find(:id, 'hidden-text').text(:all)).to eq('Some of this text is hidden!')
  end

  it 'ignores invisible text if `:visible` given' do
    Capybara.ignore_hidden_elements = false
    @session.visit('/with_html')
    expect(@session.find(:id, 'hidden-text').text(:visible)).to eq('Some of this text is')
  end

  it 'ignores invisible text if `Capybara.ignore_hidden_elements = true`' do
    @session.visit('/with_html')
    expect(@session.find(:id, 'hidden-text').text).to eq('Some of this text is')
    Capybara.ignore_hidden_elements = false
    expect(@session.find(:id, 'hidden-text').text).to eq('Some of this text is hidden!')
  end

  it 'ignores invisible text if `Capybara.visible_text_only = true`' do
    @session.visit('/with_html')
    Capybara.visible_text_only = true
    expect(@session.find(:id, 'hidden-text').text).to eq('Some of this text is')
    Capybara.ignore_hidden_elements = false
    expect(@session.find(:id, 'hidden-text').text).to eq('Some of this text is')
  end

  it 'ignores invisible text if ancestor is invisible' do
    @session.visit('/with_html')
    expect(@session.find(:id, 'hidden_via_ancestor', visible: false).text).to eq('')
  end

  context 'with css as default selector' do
    before { Capybara.default_selector = :css }

    after { Capybara.default_selector = :xpath }

    it 'should print the text of the page' do
      @session.visit('/with_simple_html')
      expect(@session.text).to eq('Bar')
    end
  end

  it 'should be correctly normalized when visible' do
    @session.visit('/with_html')
    el = @session.find(:css, '#normalized')
    expect(el.text).to eq "Some text\nMore text\nAnd more text\nEven more    text on multiple lines"
  end

  it 'should be a textContent with irrelevant whitespace collapsed when non-visible' do
    @session.visit('/with_html')
    el = @session.find(:css, '#non_visible_normalized', visible: false)
    expect(el.text(:all)).to eq 'Some textMore text And more text Even more    text on multiple lines'
  end

  it 'should strip correctly' do
    @session.visit('/with_html')
    el = @session.find(:css, '#ws')
    expect(el.text).to eq ' '
    expect(el.text(:all)).to eq ' '
  end
end