Skip to content

Commit

Permalink
Support [role="status"]
Browse files Browse the repository at this point in the history
Add a `:status` selector in the same style as the `:alert` selector.

Additionally, extend both the `:alert` and `:status` selectors to accept
text as a locator argument.
  • Loading branch information
seanpdoyle committed Sep 2, 2022
1 parent fcf7a9c commit 9db73c5
Show file tree
Hide file tree
Showing 10 changed files with 101 additions and 8 deletions.
18 changes: 16 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -208,7 +208,7 @@ Selects an element with the role of [`alert`](https://www.w3.org/WAI/ARIA/apg/pa

```ruby
expect(page).to have_selector :alert, text: "Successfully saved"
expect(page).to have_alert, text: "Successfully saved"
expect(page).to have_alert text: "Successfully saved"
```

Also see [↓ Expectation shortcuts](#expectation-shortcuts)
Expand Down Expand Up @@ -474,6 +474,19 @@ Also see
- [↓ Expectation shortcuts](#expectation-shortcuts)
- [`within_section`](#within_sectionname-find_options-block)

#### `status`

Selects an element with the role of [`status`](https://www.w3.org/TR/wai-aria-practices-1.1/#status).

```html
<div role="status">Important message</div>
```

```ruby
expect(page).to have_selector :status, text: "Successfully saved"
expect(page).to have_status text: "Successfully saved"
```

#### `tab_panel`

Finds a [tab panel](https://www.w3.org/WAI/ARIA/apg/patterns/tabpanel/).
Expand Down Expand Up @@ -679,14 +692,15 @@ The following expectation shortcuts are also added for both `have_selector_` and
- `have_navigation`
- `have_region`
- `have_section`
- `have_status`
- `have_tab_panel`
- `have_tab_button`

For example the following two are equivalent:

```ruby
expect(page).to have_selector :combo_box, "Foo"
expect(page).to have_combo_box, "Foo"
expect(page).to have_combo_box "Foo"

```

Expand Down
2 changes: 1 addition & 1 deletion lib/capybara_accessible_selectors/rspec/matchers.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
module Capybara
module RSpecMatchers
%i[alert banner combo_box contentinfo main menu menuitem modal navigation region
tab_panel tab_button disclosure disclosure_button section item].each do |selector|
tab_panel tab_button disclosure disclosure_button section status item].each do |selector|
define_method "have_#{selector}" do |locator = nil, **options, &optional_filter_block|
Matchers::HaveSelector.new(selector, locator, **options, &optional_filter_block)
end
Expand Down
1 change: 1 addition & 0 deletions lib/capybara_accessible_selectors/selectors.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,5 @@
require "capybara_accessible_selectors/selectors/region"
require "capybara_accessible_selectors/selectors/rich_text"
require "capybara_accessible_selectors/selectors/section"
require "capybara_accessible_selectors/selectors/status"
require "capybara_accessible_selectors/selectors/tab"
4 changes: 2 additions & 2 deletions lib/capybara_accessible_selectors/selectors/alert.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# frozen_string_literal: true

Capybara.add_selector(:alert) do
xpath do |*|
XPath.descendant[XPath.attr(:role) == "alert"]
xpath do |locator, *|
XPath.descendant[XPath.attr(:role) == "alert"][XPath.string.n.is(locator.to_s)]
end
end
7 changes: 7 additions & 0 deletions lib/capybara_accessible_selectors/selectors/status.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# frozen_string_literal: true

Capybara.add_selector(:status) do
xpath do |locator, *|
XPath.descendant[XPath.attr(:role) == "status"][XPath.string.is(locator.to_s)]
end
end
9 changes: 9 additions & 0 deletions spec/fixtures/status.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<!doctype html>
<meta charset="UTF-8" />
<div role="status">
Status message
<div>

<div>
Just a message
</div>
12 changes: 10 additions & 2 deletions spec/matchers/have_alert_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,19 @@
visit "/alert.html"
end

it "matches using a custom matcher" do
it "matches using a custom matcher with locator" do
expect(page).to have_alert("Alert")
end

it "matches using a custom matcher with text: filter" do
expect(page).to have_alert(text: "Alert")
end

it "matches using a negated custom matcher" do
it "matches using a negated custom matcher with locator" do
expect(page).to have_no_alert("foo")
end

it "matches using a negated custom matcher with text: filter" do
expect(page).to have_no_alert(text: "foo")
end
end
17 changes: 17 additions & 0 deletions spec/matchers/have_status_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# frozen_string_literal: true

describe "have_status" do
before do
visit "/status.html"
end

it "matches using a custom matcher" do
expect(page).to have_status("Status")
expect(page).to have_status(text: "Status")
end

it "matches using a negated custom matcher" do
expect(page).to have_no_status("foo")
expect(page).to have_no_status(text: "foo")
end
end
12 changes: 11 additions & 1 deletion spec/selectors/alert_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,17 @@
visit "/alert.html"
end

it "finds alerts" do
it "finds alerts without arguments" do
alert = page.find(:css, "[role=alert]")
expect(page.find(:alert)).to eq alert
end

it "finds alerts with a locator" do
alert = page.find(:css, "[role=alert]")
expect(page.find(:alert, "Alert message")).to eq alert
end

it "finds alerts with a text: filter" do
alert = page.find(:css, "[role=alert]")
expect(page.find(:alert, text: "Alert message")).to eq alert
end
Expand Down
27 changes: 27 additions & 0 deletions spec/selectors/status_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
# frozen_string_literal: true

describe "status selector" do
before do
visit "/status.html"
end

it "finds statuses without arguments" do
status = page.find(:css, "[role=status]")
expect(page.find(:status)).to eq status
end

it "finds statuses with a locator" do
status = page.find(:css, "[role=status]")
expect(page.find(:status, "Status message")).to eq status
end

it "finds statuses with a text: filter" do
status = page.find(:css, "[role=status]")
expect(page.find(:status, text: "Status message")).to eq status
end

it "matches selector" do
status = page.find(:css, "[role=status]")
expect(status).to match_selector :status
end
end

0 comments on commit 9db73c5

Please sign in to comment.