diff --git a/README.md b/README.md index b7e267f..d0eb400 100644 --- a/README.md +++ b/README.md @@ -139,6 +139,9 @@ todos_index.has_no_todo_matching?(text: "Buy milk") # => has_no_css?("ul li span todos_index.todos.has_count_of?(number) # => has_css?("ul li span[data-role=todo-name]", count: number) todos_index.has_todos_count?(number) # => has_css?("ul li span[data-role=todo-name]", count: number) + +todos_index.todos.has_any_elements? # => has_css?("ul li span[data-role=todo-name]") +todos_index.todos.has_no_elements? # => has_no_css?("ul li span[data-role=todo-name]") ``` The methods defined by PageEz can be passed additional options from Capybara. Refer to documentation for the following methods: diff --git a/lib/page_ez/has_many_result.rb b/lib/page_ez/has_many_result.rb index 988cb83..f25f249 100644 --- a/lib/page_ez/has_many_result.rb +++ b/lib/page_ez/has_many_result.rb @@ -20,5 +20,16 @@ def has_count_of?(count) **@options.merge(count: count) ) end + + def has_any_elements? + @container.has_css?( + @selector, + **@options + ) + end + + def has_no_elements? + @container.has_no_css?(@selector, **@options) + end end end diff --git a/lib/page_ez/method_generators/define_has_many_result_methods.rb b/lib/page_ez/method_generators/define_has_many_result_methods.rb index 22409c4..d1cdbf9 100644 --- a/lib/page_ez/method_generators/define_has_many_result_methods.rb +++ b/lib/page_ez/method_generators/define_has_many_result_methods.rb @@ -34,6 +34,14 @@ def run(target) target.logged_define_method("has_#{name}_count?") do |count, *args| send(name, *args).has_count_of?(count) end + + target.logged_define_method("has_#{name}?") do |*args| + send(name, *args).has_any_elements? + end + + target.logged_define_method("has_no_#{name}?") do |*args| + send(name, *args).has_no_elements? + end end end end diff --git a/spec/features/capybara_javascript_spec.rb b/spec/features/capybara_javascript_spec.rb index 35e2664..f38af51 100644 --- a/spec/features/capybara_javascript_spec.rb +++ b/spec/features/capybara_javascript_spec.rb @@ -150,4 +150,54 @@ expect(test_page.list.items).to have_count_of(4) end end + + it "allows for checking for any results" do + page = build_page(<<-HTML) + + +
+ +
+ + + HTML + + test_page = Class.new(PageEz::Page) do + has_one :list, "ul" do + has_many :items, "li" + end + end.new(page) + + page.visit "/" + + with_max_wait_time(seconds: 1) do + expect(test_page.list).not_to have_items + expect(test_page.list).to have_no_items + end + + with_max_wait_time(seconds: 2) do + expect(test_page.list).to have_items + end + end end diff --git a/spec/features/has_many_spec.rb b/spec/features/has_many_spec.rb index 19fea7b..a97d385 100644 --- a/spec/features/has_many_spec.rb +++ b/spec/features/has_many_spec.rb @@ -87,4 +87,47 @@ def name_text expect(test_page.sections("Section").map(&:text)).to eq(["Section 1", "Section 2"]) expect(test_page.sections("Bogus")).to be_empty end + + it "allows for checking for any or no elements" do + page = build_page(<<-HTML) +
+ + + +
+ HTML + + test_page = Class.new(PageEz::Page) do + has_one :first_list, "ul:nth-of-type(1)" do + has_many :items, "li" + end + + has_one :second_list, "ul:nth-of-type(2)" do + has_many :items, "li" + end + + has_one :third_list, "ul:nth-of-type(3)" do + has_many :items, "li" + end + end.new(page) + + page.visit "/" + + expect(test_page.first_list.items).to have_any_elements + expect(test_page.first_list.items).not_to have_no_elements + + expect(test_page.second_list.items).not_to have_any_elements + expect(test_page.second_list.items).to have_no_elements + + expect(test_page.first_list).to have_items + expect(test_page.first_list).not_to have_no_items + + expect(test_page.second_list).not_to have_items + expect(test_page.second_list).to have_no_items + end end