File: switch_to_frame_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 (116 lines) | stat: -rw-r--r-- 4,634 bytes parent folder | download | duplicates (2)
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
# frozen_string_literal: true

Capybara::SpecHelper.spec '#switch_to_frame', requires: [:frames] do
  before do
    @session.visit('/within_frames')
  end

  after do
    # Ensure we clean up after the frame changes
    @session.switch_to_frame(:top)
  end

  it 'should find the div in frameOne' do
    frame = @session.find(:frame, 'frameOne')
    @session.switch_to_frame(frame)
    expect(@session.find("//*[@id='divInFrameOne']").text).to eql 'This is the text of divInFrameOne'
  end

  it 'should find the div in FrameTwo' do
    frame = @session.find(:frame, 'frameTwo')
    @session.switch_to_frame(frame)
    expect(@session.find("//*[@id='divInFrameTwo']").text).to eql 'This is the text of divInFrameTwo'
  end

  it 'should return to the parent frame when told to' do
    frame = @session.find(:frame, 'frameOne')
    @session.switch_to_frame(frame)
    @session.switch_to_frame(:parent)
    expect(@session.find("//*[@id='divInMainWindow']").text).to eql 'This is the text for divInMainWindow'
  end

  it 'should be able to switch to nested frames' do
    frame = @session.find(:frame, 'parentFrame')
    @session.switch_to_frame frame
    frame = @session.find(:frame, 'childFrame')
    @session.switch_to_frame frame
    frame = @session.find(:frame, 'grandchildFrame1')
    @session.switch_to_frame frame
    expect(@session).to have_selector(:css, '#divInFrameOne', text: 'This is the text of divInFrameOne')
  end

  it 'should reset scope when changing frames' do
    frame = @session.find(:frame, 'parentFrame')
    @session.within(:css, '#divInMainWindow') do
      @session.switch_to_frame(frame)
      expect(@session.has_selector?(:css, 'iframe#childFrame')).to be true
      @session.switch_to_frame(:parent)
    end
  end

  it 'works if the frame is closed', requires: %i[frames js] do
    frame = @session.find(:frame, 'parentFrame')
    @session.switch_to_frame frame
    frame = @session.find(:frame, 'childFrame')
    @session.switch_to_frame frame
    @session.click_link 'Close Window Now'
    @session.switch_to_frame :parent # Go back to parentFrame
    expect(@session).to have_selector(:css, 'body#parentBody')
    expect(@session).not_to have_selector(:css, '#childFrame')
    @session.switch_to_frame :parent # Go back to top
  end

  it 'works if the frame is closed with a slight delay', requires: %i[frames js] do
    frame = @session.find(:frame, 'parentFrame')
    @session.switch_to_frame frame
    frame = @session.find(:frame, 'childFrame')
    @session.switch_to_frame frame
    @session.click_link 'Close Window Soon'
    sleep 1
    @session.switch_to_frame :parent # Go back to parentFrame
    expect(@session).to have_selector(:css, 'body#parentBody')
    expect(@session).not_to have_selector(:css, '#childFrame')
    @session.switch_to_frame :parent # Go back to top
  end

  it 'can return to the top frame', requires: [:frames] do
    frame = @session.find(:frame, 'parentFrame')
    @session.switch_to_frame frame
    frame = @session.find(:frame, 'childFrame')
    @session.switch_to_frame frame
    @session.switch_to_frame :top
    expect(@session.find("//*[@id='divInMainWindow']").text).to eql 'This is the text for divInMainWindow'
  end

  it "should raise error if switching to parent unmatched inside `within` as it's nonsense" do
    expect do
      frame = @session.find(:frame, 'parentFrame')
      @session.switch_to_frame(frame)
      @session.within(:css, '#parentBody') do
        @session.switch_to_frame(:parent)
      end
    end.to raise_error(Capybara::ScopeError, "`switch_to_frame(:parent)` cannot be called from inside a descendant frame's `within` block.")
  end

  it "should raise error if switching to top inside a `within` in a frame as it's nonsense" do
    frame = @session.find(:frame, 'parentFrame')
    @session.switch_to_frame(frame)
    @session.within(:css, '#parentBody') do
      expect do
        @session.switch_to_frame(:top)
      end.to raise_error(Capybara::ScopeError, "`switch_to_frame(:top)` cannot be called from inside a descendant frame's `within` block.")
    end
  end

  it "should raise error if switching to top inside a nested `within` in a frame as it's nonsense" do
    frame = @session.find(:frame, 'parentFrame')
    @session.switch_to_frame(frame)
    @session.within(:css, '#parentBody') do
      @session.switch_to_frame(@session.find(:frame, 'childFrame'))
      expect do
        @session.switch_to_frame(:top)
      end.to raise_error(Capybara::ScopeError, "`switch_to_frame(:top)` cannot be called from inside a descendant frame's `within` block.")
      @session.switch_to_frame(:parent)
    end
  end
end