Skip to content
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
6 changes: 3 additions & 3 deletions lib/capybara/cuprite/browser.rb
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,9 @@ def self.start(*args)
clear_network_traffic response_headers refresh click right_click
double_click hover set click_coordinates drag drag_by select
trigger scroll_to send_keys evaluate evaluate_on evaluate_async
execute frame_url frame_title within_frame switch_to_frame
current_url title go_back go_forward find_modal accept_confirm
dismiss_confirm accept_prompt dismiss_prompt reset_modals) => :page
execute frame_url frame_title switch_to_frame current_url title
go_back go_forward find_modal accept_confirm dismiss_confirm
accept_prompt dismiss_prompt reset_modals) => :page

attr_reader :process, :logger
attr_writer :timeout
Expand Down
3 changes: 3 additions & 0 deletions lib/capybara/cuprite/browser/client.rb
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,9 @@ def subscribe(event, &block)

def close
@ws.close
# Give a thread some time to handle a tail of messages
Timeout.timeout(1) { @thread.join }
rescue Timeout::Error
@thread.kill
end

Expand Down
4 changes: 2 additions & 2 deletions lib/capybara/cuprite/browser/dom.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@ module Capybara::Cuprite
class Browser
module DOM
def current_url
evaluate_in(@execution_context_id, "location.href")
evaluate_in(execution_context_id, "window.top.location.href")
end

def title
evaluate_in(@execution_context_id, "document.title")
evaluate_in(execution_context_id, "window.top.document.title")
end

def body
Expand Down
28 changes: 19 additions & 9 deletions lib/capybara/cuprite/browser/web_socket.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,11 @@

require "json"
require "socket"
require "forwardable"
require "websocket/driver"

module Capybara::Cuprite
class Browser
class WebSocket
extend Forwardable

delegate close: :@driver

attr_reader :url, :messages

def initialize(url, logger)
Expand All @@ -22,7 +17,9 @@ def initialize(url, logger)
@driver = ::WebSocket::Driver.client(self)
@messages = Queue.new

@driver.on(:open, &method(:on_open))
@driver.on(:message, &method(:on_message))
@driver.on(:close, &method(:on_close))

@thread = Thread.new do
begin
Expand All @@ -39,10 +36,8 @@ def initialize(url, logger)
@driver.start
end

def send_message(data)
json = data.to_json
@driver.text(json)
@logger&.puts("\n\n>>> #{json}")
def on_open(_event)
sleep 0.01 # https://github.com/faye/websocket-driver-ruby/issues/46
Copy link
Contributor

Choose a reason for hiding this comment

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

I've just read that github issue now - would it be possible to loop/sleep here until the state of the socket is not "connecting"? I'm not sure if that is possible, but if it is it might be more reliable, and also it would be easier to understand why the code is sleeping

Copy link
Member Author

Choose a reason for hiding this comment

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

I believe that looping in here will hang the only thread we have and make it even worse but I haven't tried it so I can be wrong. All callbacks get executed in @thread and if we don't return from on_open we may never change state. I don't fully understand how to handle it properly so asked @jcoglan for a perfect solution but he didn't have time to answer yet.

end

def on_message(event)
Expand All @@ -51,9 +46,24 @@ def on_message(event)
@logger&.puts(" <<< #{event.data}\n")
end

def on_close(_event)
@messages.close
@thread.kill
end

def send_message(data)
json = data.to_json
@driver.text(json)
@logger&.puts("\n\n>>> #{json}")
end

def write(data)
@sock.write(data)
end

def close
@driver.close
Copy link
Contributor

Choose a reason for hiding this comment

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

Should Socket#close also call @thread.join, just like Client#close above? When I was reading the examples for websocket-driver I noticed that they do that in their socket class but curprite does not

Copy link
Member Author

Choose a reason for hiding this comment

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

websocket-driver has two versions of close. One is event-driven https://github.com/faye/websocket-driver-ruby/blob/master/examples/tcp_client.rb#L25 and another one is plain close like the one we use https://github.com/faye/websocket-driver-ruby/blob/master/examples/tcp_client.rb#L42. I think it's pretty much close to what they have in example. Though @thread.join doesn't help as thread can be blocked on IO and they have to use kill as we do.

end
end
end
end
4 changes: 0 additions & 4 deletions lib/capybara/cuprite/driver.rb
Original file line number Diff line number Diff line change
Expand Up @@ -95,10 +95,6 @@ def execute_script(script, *args)
nil
end

def within_frame(name, &block)
browser.within_frame(name, &block)
end

def switch_to_frame(locator)
browser.switch_to_frame(locator)
end
Expand Down
2 changes: 1 addition & 1 deletion spec/integration/driver_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -1486,7 +1486,7 @@ def create_screenshot(file, *args)
it "can get the frames url" do
@session.visit "/cuprite/frames"

@session.within_frame 0 do
@session.within_frame(0) do
expect(@session.driver.frame_url).to end_with("/cuprite/slow")
if Capybara::VERSION.to_f < 3.0
expect(@session.driver.current_url).to end_with("/cuprite/slow")
Expand Down
8 changes: 1 addition & 7 deletions spec/spec_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ def set_capybara_wait_time(t)
#check when checkbox hidden with Capybara.automatic_label_click == false with allow_label_click == true should not wait the full time if label can be clicked
#choose with hidden radio buttons with Capybara.automatic_label_click == true should select self by clicking the label if no locator specified
#reset_session! handles already open modals
#scroll_to
#click_link can download a file
#attach_file with normal form should set a file path by id
#attach_file with normal form should set a file path by label
Expand Down Expand Up @@ -84,13 +85,6 @@ def set_capybara_wait_time(t)

Capybara::SpecHelper.configure(config)

config.filter_run_excluding full_description: lambda { |description, _metadata|
[
# test is marked pending in Capybara but Cuprite passes - disable here - have our own test in driver spec
/Capybara::Session Cuprite node #set should allow me to change the contents of a contenteditable elements child/,
].any? { |desc| description =~ desc }
}

config.before(:each) do
Cuprite::SpecHelper.set_capybara_wait_time(0)
end
Expand Down