From 96eb3b610a47e6dd501f8b4098b0aadc7cfce153 Mon Sep 17 00:00:00 2001 From: Tim Connor Date: Wed, 26 Sep 2018 16:00:50 -0700 Subject: [PATCH] prevent AePageObjects::Collection.size from waiting --- lib/ae_page_objects/elements/collection.rb | 14 ++++++++++++- test/unit/collection_test.rb | 8 ++++---- test/unit/dsl/collection_test.rb | 24 +++++++++++----------- 3 files changed, 29 insertions(+), 17 deletions(-) diff --git a/lib/ae_page_objects/elements/collection.rb b/lib/ae_page_objects/elements/collection.rb index fd943153..98aa3c77 100644 --- a/lib/ae_page_objects/elements/collection.rb +++ b/lib/ae_page_objects/elements/collection.rb @@ -41,7 +41,19 @@ def each end def size - node.all(:xpath, item_xpath, options.merge(wait: false)).size + # + # In some cases when #size is called while the DOM is updating, Capybara + # will catch (and swallow) underlying exceptions such as + # `Selenium::WebDriver::Error::StaleElementReferenceError`. + # When this happens it will wait up to the max wait time, which can cause + # issues for `AePageObjects.wait_until` blocks. + # + # To prevent this issue the #all and #size calls are made with the Capybara + # wait time set to 0. + # + Capybara.using_wait_time(0) do + node.all(:xpath, item_xpath, options).size + end end def last diff --git a/test/unit/collection_test.rb b/test/unit/collection_test.rb index b99edc7f..593ad988 100644 --- a/test/unit/collection_test.rb +++ b/test/unit/collection_test.rb @@ -96,7 +96,7 @@ def test_locator_options__blank magazine.stubs(:item_xpath).returns('item_xpath') bullet1_stub = mock(:allow_reload!) - magazine_node.expects(:all).with(:xpath, 'item_xpath', wait: false).returns([bullet1_stub]) + magazine_node.expects(:all).with(:xpath, 'item_xpath', {}).returns([bullet1_stub]) magazine_node.expects(:first).with(:xpath, '(item_xpath)[1]', minimum: 0).returns(bullet1_stub) assert_equal bullet1_stub, magazine.at(0).node end @@ -115,7 +115,7 @@ def test_locator_options__present magazine.stubs(:item_xpath).returns("item_xpath") bullet1_stub = mock(:allow_reload!) - magazine_node.expects(:all).with(:xpath, 'item_xpath', { capybara: 'options', wait: false }).returns([bullet1_stub]) + magazine_node.expects(:all).with(:xpath, 'item_xpath', { capybara: 'options' }).returns([bullet1_stub]) magazine_node.expects(:first).with(:xpath, "(item_xpath)[1]", { capybara: 'options', minimum: 0 }).returns(bullet1_stub) assert_equal bullet1_stub, magazine.at(0).node end @@ -136,7 +136,7 @@ def test_empty magazine = clip.new(parent, :name => "18_holder") magazine.stubs(:item_xpath).returns("item_xpath") - magazine_node.stubs(:all).with(:xpath, "item_xpath", wait: false).returns([]) + magazine_node.stubs(:all).with(:xpath, "item_xpath", {}).returns([]) assert_equal 0, magazine.size @@ -169,7 +169,7 @@ def test_non_empty bullet1_stub = stub(:allow_reload!) bullet2_stub = stub(:allow_reload!) - magazine_node.stubs(:all).with(:xpath, "item_xpath", wait: false).returns([bullet1_stub, bullet2_stub]) + magazine_node.stubs(:all).with(:xpath, "item_xpath", {}).returns([bullet1_stub, bullet2_stub]) assert_equal 2, magazine.size diff --git a/test/unit/dsl/collection_test.rb b/test/unit/dsl/collection_test.rb index 6b740e4f..6474d19c 100644 --- a/test/unit/dsl/collection_test.rb +++ b/test/unit/dsl/collection_test.rb @@ -24,7 +24,7 @@ def test_collection__no_is__no_contains__block previous_owners = verify_element_on_parent_with_intermediary_class(jon, :previous_owners, AePageObjects::Collection, previous_owners_page_object) first_owner_page_object = stub(:allow_reload!) - previous_owners_page_object.expects(:all).with(:xpath, ".//*", wait: false).returns([first_owner_page_object]) + previous_owners_page_object.expects(:all).with(:xpath, ".//*", {}).returns([first_owner_page_object]) previous_owners_page_object.expects(:first).with(:xpath, "(.//*)[1]", minimum: 0).returns(first_owner_page_object) first_owner = verify_item_element_with_intermediary_class(previous_owners, 0, AePageObjects::Element, first_owner_page_object) @@ -61,7 +61,7 @@ def test_collection__is__no_contains__block first_owner_page_object = stub(:allow_reload!) - previous_owners_page_object.expects(:all).with(:xpath, ".//*", wait: false).returns([first_owner_page_object]) + previous_owners_page_object.expects(:all).with(:xpath, ".//*", {}).returns([first_owner_page_object]) previous_owners_page_object.expects(:first).with(:xpath, "(.//*)[1]", minimum: 0).returns(first_owner_page_object) first_owner = verify_item_element_with_intermediary_class(previous_owners, 0, previous_owners_class.item_class, first_owner_page_object) @@ -97,7 +97,7 @@ def test_collection__is__no_contains__block__no_item_class first_owner_page_object = stub(:allow_reload!) - previous_owners_page_object.expects(:all).with(:xpath, ".//*", wait: false).returns([first_owner_page_object]) + previous_owners_page_object.expects(:all).with(:xpath, ".//*", {}).returns([first_owner_page_object]) previous_owners_page_object.expects(:first).with(:xpath, "(.//*)[1]", minimum: 0).returns(first_owner_page_object) first_owner = verify_item_element_with_intermediary_class(previous_owners, 0, previous_owners_class.item_class, first_owner_page_object) @@ -135,7 +135,7 @@ def test_collection__is__no_contains__no_block previous_owners = verify_element_on_parent(jon, :previous_owners, previous_owners_class, previous_owners_page_object) first_owner_page_object = stub(:allow_reload!) - previous_owners_page_object.expects(:all).with(:xpath, ".//*", wait: false).returns([first_owner_page_object]) + previous_owners_page_object.expects(:all).with(:xpath, ".//*", {}).returns([first_owner_page_object]) previous_owners_page_object.expects(:first).with(:xpath, "(.//*)[1]", minimum: 0).returns(first_owner_page_object) first_owner = verify_item_element(previous_owners, 0, previous_owner_class, first_owner_page_object) @@ -173,7 +173,7 @@ def test_collection__is__contains__no_block__same_item_class previous_owners = verify_element_on_parent(jon, :previous_owners, previous_owners_class, previous_owners_page_object) first_owner_page_object = stub(:allow_reload!) - previous_owners_page_object.expects(:all).with(:xpath, ".//*", wait: false).returns([first_owner_page_object]) + previous_owners_page_object.expects(:all).with(:xpath, ".//*", {}).returns([first_owner_page_object]) previous_owners_page_object.expects(:first).with(:xpath, "(.//*)[1]", minimum: 0).returns(first_owner_page_object) first_owner = verify_item_element(previous_owners, 0, previous_owner_class, first_owner_page_object) @@ -209,7 +209,7 @@ def test_collection__is__contains__no_block__different_item_class previous_owners = verify_element_on_parent_with_intermediary_class(jon, :previous_owners, previous_owners_class, previous_owners_page_object) first_owner_page_object = stub(:allow_reload!) - previous_owners_page_object.expects(:all).with(:xpath, ".//*", wait: false).returns([first_owner_page_object]) + previous_owners_page_object.expects(:all).with(:xpath, ".//*", {}).returns([first_owner_page_object]) previous_owners_page_object.expects(:first).with(:xpath, "(.//*)[1]", minimum: 0).returns(first_owner_page_object) first_owner = verify_item_element(previous_owners, 0, previous_owner_class, first_owner_page_object) @@ -243,7 +243,7 @@ def test_collection__no_is__contains__no_block previous_owners = verify_element_on_parent_with_intermediary_class(jon, :previous_owners, AePageObjects::Collection, previous_owners_page_object) first_owner_page_object = stub(:allow_reload!) - previous_owners_page_object.expects(:all).with(:xpath, ".//*", wait: false).returns([first_owner_page_object]) + previous_owners_page_object.expects(:all).with(:xpath, ".//*", {}).returns([first_owner_page_object]) previous_owners_page_object.expects(:first).with(:xpath, "(.//*)[1]", minimum: 0).returns(first_owner_page_object) first_owner = verify_item_element(previous_owners, 0, previous_owner_class, first_owner_page_object) @@ -279,7 +279,7 @@ def test_collection__no_is__contains__block previous_owners = verify_element_on_parent_with_intermediary_class(jon, :previous_owners, AePageObjects::Collection, previous_owners_page_object) first_owner_page_object = stub(:allow_reload!) - previous_owners_page_object.expects(:all).with(:xpath, ".//*", wait: false).returns([first_owner_page_object]) + previous_owners_page_object.expects(:all).with(:xpath, ".//*", {}).returns([first_owner_page_object]) previous_owners_page_object.expects(:first).with(:xpath, "(.//*)[1]", minimum: 0).returns(first_owner_page_object) first_owner = verify_item_element_with_intermediary_class(previous_owners, 0, previous_owner_class, first_owner_page_object) @@ -322,7 +322,7 @@ def test_collection__is__contains__block previous_owners = verify_element_on_parent_with_intermediary_class(jon, :previous_owners, previous_owners_class, previous_owners_page_object) first_owner_page_object = stub(:allow_reload!) - previous_owners_page_object.expects(:all).with(:xpath, ".//*", wait: false).returns([first_owner_page_object]) + previous_owners_page_object.expects(:all).with(:xpath, ".//*", {}).returns([first_owner_page_object]) previous_owners_page_object.expects(:first).with(:xpath, "(.//*)[1]", minimum: 0).returns(first_owner_page_object) first_owner = verify_item_element_with_intermediary_class(previous_owners, 0, previous_owner_class, first_owner_page_object) @@ -350,7 +350,7 @@ def test_collection__no_is__no_contains__no_block previous_owners = verify_element_on_parent_with_intermediary_class(jon, :previous_owners, AePageObjects::Collection, previous_owners_page_object) first_owner_page_object = stub(:allow_reload!) - previous_owners_page_object.expects(:all).with(:xpath, ".//*", wait: false).returns([first_owner_page_object]) + previous_owners_page_object.expects(:all).with(:xpath, ".//*", {}).returns([first_owner_page_object]) previous_owners_page_object.expects(:first).with(:xpath, "(.//*)[1]", minimum: 0).returns(first_owner_page_object) first_owner = verify_item_element(previous_owners, 0, AePageObjects::Element, first_owner_page_object) end @@ -375,7 +375,7 @@ def test_collection__locator previous_owners = verify_element_on_parent_with_intermediary_class(jon, :previous_owners, AePageObjects::Collection, previous_owners_page_object) first_owner_page_object = stub(:allow_reload!) - previous_owners_page_object.expects(:all).with(:xpath, ".//*", wait: false).returns([first_owner_page_object]) + previous_owners_page_object.expects(:all).with(:xpath, ".//*", {}).returns([first_owner_page_object]) previous_owners_page_object.expects(:first).with(:xpath, "(.//*)[1]", minimum: 0).returns(first_owner_page_object) first_owner = verify_item_element_with_intermediary_class(previous_owners, 0, AePageObjects::Element, first_owner_page_object) @@ -410,7 +410,7 @@ def test_nested_element__locator__proc previous_owners = verify_element_on_parent_with_intermediary_class(jon, :previous_owners, AePageObjects::Collection, previous_owners_page_object) first_owner_page_object = stub(:allow_reload!) - previous_owners_page_object.expects(:all).with(:xpath, ".//*", wait: false).returns([first_owner_page_object]) + previous_owners_page_object.expects(:all).with(:xpath, ".//*", {}).returns([first_owner_page_object]) previous_owners_page_object.expects(:first).with(:xpath, "(.//*)[1]", minimum: 0).returns(first_owner_page_object) first_owner = verify_item_element_with_intermediary_class(previous_owners, 0, AePageObjects::Element, first_owner_page_object)