Skip to content

Commit

Permalink
Merge pull request #258 from twalpole/fix_waiters
Browse files Browse the repository at this point in the history
Remove use of Timeout.timeout and fix merging of find_args and runtime_args - Fixes Issue #257
  • Loading branch information
luke-hill authored May 28, 2018
2 parents 464052b + 31d1593 commit c5f42e5
Show file tree
Hide file tree
Showing 6 changed files with 32 additions and 17 deletions.
1 change: 1 addition & 0 deletions features/elements.feature
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ Feature: Interaction with groups of elements

Scenario: Get groups of elements from within a section
Then I can see individual people in the people list
And I can see optioned individual people in the people list

Scenario: Page with no elements
Then the page does not have a group of elements
Expand Down
6 changes: 6 additions & 0 deletions features/step_definitions/elements_steps.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,12 @@
expect(@test_site.home.people).to have_individuals(count: 4)
end

Then('I can see optioned individual people in the people list') do
expect(@test_site.home.people.optioned_individuals.size).to eq(4)

expect(@test_site.home.people).to have_optioned_individuals(count: 4)
end

Then('I can wait a variable time and pass specific parameters') do
@test_site.home.wait_for_lots_of_links(0.1, count: 2)
Capybara.using_wait_time(0.3) do
Expand Down
2 changes: 1 addition & 1 deletion features/step_definitions/section_steps.rb
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@
end

Then("I can see a section's full text") do
expect(@test_site.home.people.text).to eq('People person 1 person 2 person 3 person 4')
expect(@test_site.home.people.text).to eq('People person 1 person 2 person 3 person 4 object 1')

expect(@test_site.home.container_with_element.text).to eq('This will be removed when you click submit above')
end
Expand Down
38 changes: 22 additions & 16 deletions lib/site_prism/element_container.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ def element(element_name, *find_args)
build(element_name, *find_args) do
define_method(element_name.to_s) do |*runtime_args, &element_block|
self.class.raise_if_block(self, element_name.to_s, !element_block.nil?)
find_first(*find_args, *runtime_args)
find_first(*self.class.merge_args(find_args, runtime_args))
end
end
end
Expand All @@ -17,7 +17,7 @@ def elements(collection_name, *find_args)
build(collection_name, *find_args) do
define_method(collection_name.to_s) do |*runtime_args, &element_block|
self.class.raise_if_block(self, collection_name.to_s, !element_block.nil?)
find_all(*find_args, *runtime_args)
find_all(*self.class.merge_args(find_args, runtime_args))
end
end
end
Expand All @@ -31,7 +31,7 @@ def section(section_name, *args, &block)
section_class, find_args = extract_section_options(args, &block)
build(section_name, *find_args) do
define_method section_name do |*runtime_args, &runtime_block|
section_class.new self, find_first(*find_args, *runtime_args), &runtime_block
section_class.new self, find_first(*self.class.merge_args(find_args, runtime_args)), &runtime_block
end
end
end
Expand All @@ -41,7 +41,7 @@ def sections(section_collection_name, *args, &block)
build(section_collection_name, *find_args) do
define_method(section_collection_name) do |*runtime_args, &element_block|
self.class.raise_if_block(self, section_collection_name.to_s, !element_block.nil?)
find_all(*find_args, *runtime_args).map do |element|
find_all(*self.class.merge_args(find_args, runtime_args)).map do |element|
section_class.new(self, element)
end
end
Expand Down Expand Up @@ -85,6 +85,16 @@ def raise_wait_for_no_if_failed(obj, name, timeout, failed)
"Timed out after #{timeout}s waiting for no #{obj.class}##{name}"
end

def merge_args(find_args, runtime_args, override_options = {})
find_args = find_args.dup
runtime_args = runtime_args.dup
options = {}
options.merge!(find_args.pop) if find_args.last.is_a? Hash
options.merge!(runtime_args.pop) if runtime_args.last.is_a? Hash
options.merge!(override_options)
[*find_args, *runtime_args, options]
end

private

def build(name, *find_args)
Expand Down Expand Up @@ -127,7 +137,7 @@ def create_existence_checker(element_name, *find_args)
define_method(method_name) do |*runtime_args|
wait_time = SitePrism.use_implicit_waits ? Capybara.default_max_wait_time : 0
Capybara.using_wait_time(wait_time) do
element_exists?(*find_args, *runtime_args)
element_exists?(*self.class.merge_args(find_args, runtime_args))
end
end
end
Expand All @@ -139,7 +149,7 @@ def create_nonexistence_checker(element_name, *find_args)
define_method(method_name) do |*runtime_args|
wait_time = SitePrism.use_implicit_waits ? Capybara.default_max_wait_time : 0
Capybara.using_wait_time(wait_time) do
element_does_not_exist?(*find_args, *runtime_args)
element_does_not_exist?(*self.class.merge_args(find_args, runtime_args))
end
end
end
Expand All @@ -150,7 +160,7 @@ def create_waiter(element_name, *find_args)
create_helper_method(method_name, *find_args) do
define_method(method_name) do |timeout = Capybara.default_max_wait_time, *runtime_args|
result = Capybara.using_wait_time(timeout) do
element_exists?(*find_args, *runtime_args)
element_exists?(*self.class.merge_args(find_args, runtime_args))
end
self.class.raise_wait_for_if_failed(self, element_name.to_s, timeout, !result)
result
Expand All @@ -163,7 +173,7 @@ def create_nonexistence_waiter(element_name, *find_args)
create_helper_method(method_name, *find_args) do
define_method(method_name) do |timeout = Capybara.default_max_wait_time, *runtime_args|
result = Capybara.using_wait_time(timeout) do
element_does_not_exist?(*find_args, *runtime_args)
element_does_not_exist?(*self.class.merge_args(find_args, runtime_args))
end
self.class.raise_wait_for_no_if_failed(self, element_name.to_s, timeout, !result)
result
Expand All @@ -175,10 +185,8 @@ def create_visibility_waiter(element_name, *find_args)
method_name = "wait_until_#{element_name}_visible"
create_helper_method(method_name, *find_args) do
define_method(method_name) do |timeout = Capybara.default_max_wait_time, *runtime_args|
Timeout.timeout(timeout, SitePrism::TimeOutWaitingForElementVisibility) do
Capybara.using_wait_time 0 do
sleep 0.05 until element_exists?(*find_args, *runtime_args, visible: true)
end
unless element_exists?(*self.class.merge_args(find_args, runtime_args, visible: true, wait: timeout))
raise SitePrism::TimeOutWaitingForElementVisibility
end
end
end
Expand All @@ -188,10 +196,8 @@ def create_invisibility_waiter(element_name, *find_args)
method_name = "wait_until_#{element_name}_invisible"
create_helper_method(method_name, *find_args) do
define_method(method_name) do |timeout = Capybara.default_max_wait_time, *runtime_args|
Timeout.timeout(timeout, SitePrism::TimeOutWaitingForElementInvisibility) do
Capybara.using_wait_time 0 do
sleep 0.05 while element_exists?(*find_args, *runtime_args, visible: true)
end
unless element_does_not_exist?(*self.class.merge_args(find_args, runtime_args, visible: true, wait: timeout))
raise SitePrism::TimeOutWaitingForElementInvisibility
end
end
end
Expand Down
1 change: 1 addition & 0 deletions test_site/html/home.htm
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ <h2>People</h2>
<span class='person'>person 2</span>
<span class='person'>person 3</span>
<span class='person'>person 4</span>
<span class='object'>object 1</span>
</article>

<input type='submit' id='remove_container_with_element' onClick='removeElement(document.getElementById("container_with_element"))'/>
Expand Down
1 change: 1 addition & 0 deletions test_site/sections/people.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ class People < SitePrism::Section
element :dinosaur, '.dinosaur' # doesn't exist on the page

elements :individuals, '.person'
elements :optioned_individuals, 'span', class: 'person'

element :welcome_message_on_the_parent, 'span.welcome' # should not be found here
end

0 comments on commit c5f42e5

Please sign in to comment.