Skip to content

Commit

Permalink
Allow page objects to skip the definition of a container
Browse files Browse the repository at this point in the history
Demanding page objects to define a container is a weird
requirement, that is likely to just generate development
overhead without any practical benefit.
  • Loading branch information
khamusa committed Jan 14, 2025
1 parent 4c5a067 commit 3e0ab31
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 7 deletions.
6 changes: 6 additions & 0 deletions lib/tabasco/page.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,12 @@ def self.url_value
@url_value
end

def container
return super if self.class.test_id

Capybara.current_session.find("body")
end

def self.visit(...)
new(...).tap do |instance|
Capybara.current_session.visit(instance.path)
Expand Down
39 changes: 32 additions & 7 deletions spec/tabasco/page_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@

Class.new(described_class) do
url page_url_value
container_test_id :root_container

ensure_loaded { has_content!("Welcome to the Testing Demo Page") }

Expand Down Expand Up @@ -96,13 +95,39 @@
end
# rubocop: enable RSpec/ExpectInLet

context "when the page fails to load properly" do
let(:page_url) { "oops_not_found.html" }
context "when a container test id is specified for a page" do
let(:root_container) { :"root-container" }

it "raises an expectation error when trying to use the page object" do
# TODO: wrap with Tabasco:: custom error class and improve error message
expect { subject }
.to raise_error(Capybara::ElementNotFound, "Unable to find css \"[data-testid='root-container']\"")
before do
root_container_test_id = root_container

page_klass.class_eval do
container_test_id root_container_test_id
end
end

it "loads the page correctly" do
expect { subject }.not_to raise_error
expect(subject).to have_content("Welcome to the Testing Demo Page")
end

context "when the page fails to render" do
let(:page_url) { "oops_not_found.html" }

it "raises an expectation error when trying to use the page object" do
# TODO: wrap with Tabasco:: custom error class and improve error message
expect { subject }
.to raise_error(Capybara::ElementNotFound, "Unable to find css \"[data-testid='root-container']\"")
end
end

context "when a narrower testid is provided" do
let(:root_container) { :welcome_header }

it "scopes the page object to the given test id" do
expect(subject).to have_content("Welcome to the Testing Demo Page")
expect(subject).to have_no_content("Home Section")
end
end
end

Expand Down

0 comments on commit 3e0ab31

Please sign in to comment.