Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Replace recursion with loop in FindElement#run #83

Merged
merged 2 commits into from
May 15, 2020
Merged

Replace recursion with loop in FindElement#run #83

merged 2 commits into from
May 15, 2020

Conversation

matthewmcgarvey
Copy link
Member

@matthewmcgarvey matthewmcgarvey commented May 15, 2020

Fixes #78

This drastically simplifies the stack trace when a spec attempts to find an element and fails.

Before

Expected to find element on page, but it was not found.
       
  ▸ looking for: [flow-id='sign-up-buttons']

Did you mean...

  ▸ '@sign-up-button'
(LuckyFlow::ElementNotFoundError)
  from lib/lucky_flow/src/lucky_flow/find_element.cr:61:5 in 'raise_element_not_found_error'
  from lib/lucky_flow/src/lucky_flow/find_element.cr:24:7 in 'retry'
  from lib/lucky_flow/src/lucky_flow/find_element.cr:16:33 in 'run'
  from lib/lucky_flow/src/lucky_flow/find_element.cr:34:5 in 'retry_after_delay'
  from lib/lucky_flow/src/lucky_flow/find_element.cr:22:7 in 'retry'
  from lib/lucky_flow/src/lucky_flow/find_element.cr:16:33 in 'run'
  from lib/lucky_flow/src/lucky_flow/find_element.cr:34:5 in 'retry_after_delay'
  from lib/lucky_flow/src/lucky_flow/find_element.cr:22:7 in 'retry'
  from lib/lucky_flow/src/lucky_flow/find_element.cr:16:33 in 'run'
  from lib/lucky_flow/src/lucky_flow/find_element.cr:34:5 in 'retry_after_delay'
  from lib/lucky_flow/src/lucky_flow/find_element.cr:22:7 in 'retry'
  from lib/lucky_flow/src/lucky_flow/find_element.cr:16:33 in 'run'
  from lib/lucky_flow/src/lucky_flow/find_element.cr:34:5 in 'retry_after_delay'
  from lib/lucky_flow/src/lucky_flow/find_element.cr:22:7 in 'retry'
  from lib/lucky_flow/src/lucky_flow/find_element.cr:16:33 in 'run'
  from lib/lucky_flow/src/lucky_flow/find_element.cr:34:5 in 'retry_after_delay'
  from lib/lucky_flow/src/lucky_flow/find_element.cr:22:7 in 'retry'
  from lib/lucky_flow/src/lucky_flow/find_element.cr:16:33 in 'run'
  from lib/lucky_flow/src/lucky_flow/find_element.cr:34:5 in 'retry_after_delay'
  from lib/lucky_flow/src/lucky_flow/find_element.cr:22:7 in 'retry'
  from lib/lucky_flow/src/lucky_flow/find_element.cr:16:33 in 'run'
  from lib/lucky_flow/src/lucky_flow/find_element.cr:34:5 in 'retry_after_delay'
  from lib/lucky_flow/src/lucky_flow/find_element.cr:22:7 in 'retry'
  from lib/lucky_flow/src/lucky_flow/find_element.cr:16:33 in 'run'
  from lib/lucky_flow/src/lucky_flow/find_element.cr:34:5 in 'retry_after_delay'
  from lib/lucky_flow/src/lucky_flow/find_element.cr:22:7 in 'retry'
  from lib/lucky_flow/src/lucky_flow/find_element.cr:16:33 in 'run'
  from lib/lucky_flow/src/lucky_flow/find_element.cr:34:5 in 'retry_after_delay'
  from lib/lucky_flow/src/lucky_flow/find_element.cr:22:7 in 'retry'
  from lib/lucky_flow/src/lucky_flow/find_element.cr:16:33 in 'run'
  from lib/lucky_flow/src/lucky_flow/find_element.cr:34:5 in 'retry_after_delay'
  from lib/lucky_flow/src/lucky_flow/find_element.cr:22:7 in 'retry'
  from lib/lucky_flow/src/lucky_flow/find_element.cr:16:33 in 'run'
  from lib/lucky_flow/src/lucky_flow/find_element.cr:34:5 in 'retry_after_delay'
  from lib/lucky_flow/src/lucky_flow/find_element.cr:22:7 in 'retry'
  from lib/lucky_flow/src/lucky_flow/find_element.cr:16:33 in 'run'
  from lib/lucky_flow/src/lucky_flow/find_element.cr:34:5 in 'retry_after_delay'
  from lib/lucky_flow/src/lucky_flow/find_element.cr:22:7 in 'retry'
  from lib/lucky_flow/src/lucky_flow/find_element.cr:16:33 in 'run'
  from lib/lucky_flow/src/lucky_flow/find_element.cr:34:5 in 'retry_after_delay'
  from lib/lucky_flow/src/lucky_flow/find_element.cr:22:7 in 'retry'
  from lib/lucky_flow/src/lucky_flow/find_element.cr:16:33 in 'run'
  from lib/lucky_flow/src/lucky_flow/find_element.cr:34:5 in 'retry_after_delay'
  from lib/lucky_flow/src/lucky_flow/find_element.cr:22:7 in 'retry'
  from lib/lucky_flow/src/lucky_flow/find_element.cr:16:33 in 'run'
  from lib/lucky_flow/src/lucky_flow/find_element.cr:34:5 in 'retry_after_delay'
  from lib/lucky_flow/src/lucky_flow/find_element.cr:22:7 in 'retry'
  from lib/lucky_flow/src/lucky_flow/find_element.cr:16:33 in 'run'
  from lib/lucky_flow/src/lucky_flow/find_element.cr:34:5 in 'retry_after_delay'
  from lib/lucky_flow/src/lucky_flow/find_element.cr:22:7 in 'retry'
  from lib/lucky_flow/src/lucky_flow/find_element.cr:16:33 in 'run'
  from lib/lucky_flow/src/lucky_flow/find_element.cr:34:5 in 'retry_after_delay'
  from lib/lucky_flow/src/lucky_flow/find_element.cr:22:7 in 'retry'
  from lib/lucky_flow/src/lucky_flow/find_element.cr:16:33 in 'run'
  from lib/lucky_flow/src/lucky_flow/find_element.cr:34:5 in 'retry_after_delay'
  from lib/lucky_flow/src/lucky_flow/find_element.cr:22:7 in 'retry'
  from lib/lucky_flow/src/lucky_flow/find_element.cr:16:33 in 'run'
  from lib/lucky_flow/src/lucky_flow/find_element.cr:34:5 in 'retry_after_delay'
  from lib/lucky_flow/src/lucky_flow/find_element.cr:22:7 in 'retry'
  from lib/lucky_flow/src/lucky_flow/find_element.cr:16:33 in 'run'
  from lib/lucky_flow/src/lucky_flow/find_element.cr:12:5 in 'run'
  from lib/lucky_flow/src/lucky_flow/element.cr:13:19 in 'element'
  from lib/lucky_flow/src/lucky_flow/element.cr:4:3 in 'click'
  from lib/lucky_flow/src/lucky_flow.cr:81:5 in 'click'
  from spec/support/flows/authentication_flow.cr:13:5 in 'sign_up'
  from spec/flows/authentication_spec.cr:7:5 in '->'
  from /usr/local/Cellar/crystal/0.34.0/src/spec/example.cr:255:3 in 'internal_run'
  from /usr/local/Cellar/crystal/0.34.0/src/spec/example.cr:35:16 in 'run'
  from /usr/local/Cellar/crystal/0.34.0/src/spec/context.cr:296:23 in 'internal_run'
  from /usr/local/Cellar/crystal/0.34.0/src/spec/context.cr:291:7 in 'run'
  from /usr/local/Cellar/crystal/0.34.0/src/spec/context.cr:48:23 in 'run'
  from /usr/local/Cellar/crystal/0.34.0/src/spec/dsl.cr:270:7 in '->'
  from /usr/local/Cellar/crystal/0.34.0/src/crystal/at_exit_handlers.cr:255:3 in 'run'
  from /usr/local/Cellar/crystal/0.34.0/src/crystal/main.cr:45:14 in 'main'
  from /usr/local/Cellar/crystal/0.34.0/src/crystal/main.cr:114:3 in 'main'

After

Expected to find element on page, but it was not found.
       
  ▸ looking for: [flow-id='sign-up-buttons']

Did you mean...

  ▸ '@sign-up-button'
(LuckyFlow::ElementNotFoundError)
  from lib/lucky_flow/src/lucky_flow/find_element.cr:56:5 in 'raise_element_not_found_error'
  from lib/lucky_flow/src/lucky_flow/find_element.cr:18:41 in 'run'
  from lib/lucky_flow/src/lucky_flow/find_element.cr:12:5 in 'run'
  from lib/lucky_flow/src/lucky_flow/element.cr:13:19 in 'element'
  from lib/lucky_flow/src/lucky_flow/element.cr:4:3 in 'click'
  from lib/lucky_flow/src/lucky_flow.cr:81:5 in 'click'
  from spec/support/flows/authentication_flow.cr:13:5 in 'sign_up'
  from spec/flows/authentication_spec.cr:7:5 in '->'
  from /usr/local/Cellar/crystal/0.34.0/src/spec/example.cr:255:3 in 'internal_run'
  from /usr/local/Cellar/crystal/0.34.0/src/spec/example.cr:35:16 in 'run'
  from /usr/local/Cellar/crystal/0.34.0/src/spec/context.cr:296:23 in 'internal_run'
  from /usr/local/Cellar/crystal/0.34.0/src/spec/context.cr:291:7 in 'run'
  from /usr/local/Cellar/crystal/0.34.0/src/spec/context.cr:48:23 in 'run'
  from /usr/local/Cellar/crystal/0.34.0/src/spec/dsl.cr:270:7 in '->'
  from /usr/local/Cellar/crystal/0.34.0/src/crystal/at_exit_handlers.cr:255:3 in 'run'
  from /usr/local/Cellar/crystal/0.34.0/src/crystal/main.cr:45:14 in 'main'
  from /usr/local/Cellar/crystal/0.34.0/src/crystal/main.cr:114:3 in 'main'

This makes sure the code always runs at least once without modifying the #max_tries code
src/lucky_flow/find_element.cr Show resolved Hide resolved
@@ -46,7 +40,8 @@ class LuckyFlow::FindElement
LuckyFlow.settings
end

private def matching_elements : Array(Selenium::Element)
private def find_matching_elements : Array(Selenium::Element)
self.tries += 1
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In my refactor I forgot to increment this number and ended up in an infinite loop. Rather than fixing it and letting someone else do it in the future, I thought it made more sense to have it here.

end

private def has_retries_left?
tries < max_tries
end

private def retry_after_delay
sleep retry_delay_in_ms
run
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is the moral of this story that recursion is bad? Hmmm... 🙈

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Haha yeah I guess so!

Copy link
Member

@paulcsmith paulcsmith left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for improving this. The stack trace looks a lot nicer!

end

private def has_retries_left?
tries < max_tries
end

private def retry_after_delay
sleep retry_delay_in_ms
run
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Haha yeah I guess so!

src/lucky_flow/find_element.cr Show resolved Hide resolved
@paulcsmith paulcsmith merged commit b00a8d8 into luckyframework:master May 15, 2020
@matthewmcgarvey matthewmcgarvey deleted the find-element-recursion-fix branch May 15, 2020 17:37
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Errors should not be thrown in the retry loop
2 participants