File: has_button_spec.rb

package info (click to toggle)
ruby-capybara 3.36.0%2Bds-2
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 2,380 kB
  • sloc: ruby: 23,399; javascript: 748; makefile: 11
file content (144 lines) | stat: -rw-r--r-- 5,118 bytes parent folder | download
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
# frozen_string_literal: true

Capybara::SpecHelper.spec '#has_button?' do
  before do
    @session.visit('/form')
  end

  it 'should be true if the given button is on the page' do
    expect(@session).to have_button('med')
    expect(@session).to have_button('crap321')
    expect(@session).to have_button(:crap321)
    expect(@session).to have_button('button with label element')
    expect(@session).to have_button('button within label element')
  end

  it 'should be true for disabled buttons if disabled: true' do
    expect(@session).to have_button('Disabled button', disabled: true)
  end

  it 'should be false if the given button is not on the page' do
    expect(@session).not_to have_button('monkey')
  end

  it 'should be false for disabled buttons by default' do
    expect(@session).not_to have_button('Disabled button')
  end

  it 'should be false for disabled buttons if disabled: false' do
    expect(@session).not_to have_button('Disabled button', disabled: false)
  end

  it 'should be true for disabled buttons if disabled: :all' do
    expect(@session).to have_button('Disabled button', disabled: :all)
  end

  it 'should be true for enabled buttons if disabled: :all' do
    expect(@session).to have_button('med', disabled: :all)
  end

  it 'can verify button type' do
    expect(@session).to have_button('awe123', type: 'submit')
    expect(@session).not_to have_button('awe123', type: 'reset')
  end

  it 'should be true for role=button when enable_aria_role: true' do
    expect(@session).to have_button('ARIA button', enable_aria_role: true)
  end

  it 'should be false for a role=button within a label when enable_aria_role: true' do
    expect(@session).not_to have_button('role=button within label', enable_aria_role: true)
  end

  it 'should be false for role=button when enable_aria_role: false' do
    expect(@session).not_to have_button('ARIA button', enable_aria_role: false)
  end

  it 'should be false for a role=button within a label when enable_aria_role: false' do
    expect(@session).not_to have_button('role=button within label', enable_aria_role: false)
  end

  it 'should not affect other selectors when enable_aria_role: true' do
    expect(@session).to have_button('Click me!', enable_aria_role: true)
  end

  it 'should not affect other selectors when enable_aria_role: false' do
    expect(@session).to have_button('Click me!', enable_aria_role: false)
  end

  context 'with focused:', requires: [:active_element] do
    it 'should be true if a field has focus when focused: true' do
      @session.send_keys(:tab)

      expect(@session).to have_button('A Button', focused: true)
    end

    it 'should be true if a field does not have focus when focused: false' do
      expect(@session).to have_button('A Button', focused: false)
    end
  end
end

Capybara::SpecHelper.spec '#has_no_button?' do
  before do
    @session.visit('/form')
  end

  it 'should be true if the given button is on the page' do
    expect(@session).not_to have_no_button('med')
    expect(@session).not_to have_no_button('crap321')
  end

  it 'should be true for disabled buttons if disabled: true' do
    expect(@session).not_to have_no_button('Disabled button', disabled: true)
  end

  it 'should be false if the given button is not on the page' do
    expect(@session).to have_no_button('monkey')
  end

  it 'should be false for disabled buttons by default' do
    expect(@session).to have_no_button('Disabled button')
  end

  it 'should be false for disabled buttons if disabled: false' do
    expect(@session).to have_no_button('Disabled button', disabled: false)
  end

  it 'should be true for role=button when enable_aria_role: false' do
    expect(@session).to have_no_button('ARIA button', enable_aria_role: false)
  end

  it 'should be true for role=button within a label when enable_aria_role: false' do
    expect(@session).to have_no_button('role=button within label', enable_aria_role: false)
  end

  it 'should be false for role=button when enable_aria_role: true' do
    expect(@session).not_to have_no_button('ARIA button', enable_aria_role: true)
  end

  it 'should be true for a role=button within a label when enable_aria_role: true' do
    # label element does not associate with aria button
    expect(@session).to have_no_button('role=button within label', enable_aria_role: true)
  end

  it 'should not affect other selectors when enable_aria_role: true' do
    expect(@session).to have_no_button('Junk button that does not exist', enable_aria_role: true)
  end

  it 'should not affect other selectors when enable_aria_role: false' do
    expect(@session).to have_no_button('Junk button that does not exist', enable_aria_role: false)
  end

  context 'with focused:', requires: [:active_element] do
    it 'should be true if a button does not have focus when focused: true' do
      expect(@session).to have_no_button('A Button', focused: true)
    end

    it 'should be false if a button has focus when focused: false' do
      @session.send_keys(:tab)

      expect(@session).to have_no_button('A Button', focused: false)
    end
  end
end