Skip to content

Commit

Permalink
add spec: browser.connected? ,and fix some problems
Browse files Browse the repository at this point in the history
  • Loading branch information
YusukeIwaki committed Jul 23, 2020
1 parent 2f8ce36 commit 277183b
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 2 deletions.
6 changes: 5 additions & 1 deletion lib/puppeteer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,11 @@ def connect(
}.compact
browser = launcher.connect(options)
if block_given?
yield(browser)
begin
yield(browser)
ensure
browser.close
end
else
browser
end
Expand Down
2 changes: 1 addition & 1 deletion lib/puppeteer/browser.rb
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,7 @@ def handle_target_info_changed(event)
end

# @return [String]
def websocket_endpoint
def ws_endpoint
@connection.url
end

Expand Down
5 changes: 5 additions & 0 deletions lib/puppeteer/connection.rb
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,11 @@ def initialize(url, transport, delay = 0)
@closed = false
end

# used only in Browser#connected?
def closed?
@closed
end

private def sleep_before_handling_message(message)
# Puppeteer doesn't handle any Network monitoring responses.
# So we don't have to sleep.
Expand Down
11 changes: 11 additions & 0 deletions spec/integration/browser_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,15 @@
expect(browser.target.type).to eq('browser')
end
end

describe 'connected?' do
it 'should return the browser connected state' do
ws_endpoint = browser.ws_endpoint
new_browser = Puppeteer.connect(browser_ws_endpoint: ws_endpoint)
expect(new_browser.connected?).to eq(true)
new_browser.disconnect
expect(new_browser.connected?).to eq(false)
expect(browser.connected?).to eq(true)
end
end
end

0 comments on commit 277183b

Please sign in to comment.