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

implement Selenium::Helpers::Wait #25

Merged
merged 1 commit into from
Feb 28, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
51 changes: 51 additions & 0 deletions spec/features/wait_spec.cr
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
require "../spec_helper"

describe Selenium::Helpers::Wait do
describe ".wait" do
it "success if true" do
TestServer.route "/home", <<-HTML
<div id="div"></div>
HTML

with_session do |session|
session.navigate_to("http://localhost:3002/home")

wait = Selenium::Helpers::Wait.new(timeout: 2.seconds, interval: 1.second)

wait.until { session.find_element(:css, "#div") }
end
end

it "raises an error if false" do
TestServer.route "/home", <<-HTML
<div id="div"></div>
HTML

with_session do |session|
session.navigate_to("http://localhost:3002/home")

wait = Selenium::Helpers::Wait.new(timeout: 2.seconds, interval: 1.second)

expect_raises(IO::TimeoutError, /timed out after 2 seconds \(no such element.*/) do
wait.until { session.find_element(:css, "#div1") }
end
end
end

it "raises an error with custom message" do
TestServer.route "/home", <<-HTML
<div id="div"></div>
HTML

with_session do |session|
session.navigate_to("http://localhost:3002/home")

wait = Selenium::Helpers::Wait.new(timeout: 2.seconds, interval: 1.second, message: "custom message")

expect_raises(IO::TimeoutError, /custom message \(no such element.*/) do
wait.until { session.find_element(:css, "#div1") }
end
end
end
end
end
50 changes: 50 additions & 0 deletions src/selenium/helpers/wait.cr
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
class Selenium::Helpers::Wait
@timeout : Time::Span
@interval : Time::Span
@ignored = [] of Exception

# Create a new `Wait` instance.
#
# `timeout` - seconds to wait before timing out
# `interval` - seconds to sleep between polls
# `message` - exception mesage if timed out
# `ignored` - exceptions to ignore while polling
def initialize(@timeout = 5.seconds, @interval = 0.2.seconds, @ignored = [Selenium::Error], @message : String? = nil)
end

# Wait until the given block returns a `true` value.
# Otherwise raise an exception `IO::TimeoutError`.
#
# ```
# wait = Selenium::Helpers::Wait.new(timeout: 10.seconds, interval: 1.second)
# wait.until { session.document_manager.execute_script("return document.readyState;") == "complete" }
# ```
def until(&) : Nil
end_time = current_time + @timeout
last_error : Exception? = nil

until current_time > end_time
begin
result = yield

return result if result
rescue ex
last_error = ex

raise ex unless @ignored.includes?(ex.class)
end

sleep @interval
end

message = @message || "timed out after #{@timeout.seconds} seconds"

message = message + " (#{last_error.message})" if last_error

raise IO::TimeoutError.new(message)
end

private def current_time
Time.utc
end
end