Skip to content

Commit

Permalink
Merge pull request #8 from YusukeIwaki/integration_spec
Browse files Browse the repository at this point in the history
Introduce integration spec (run with Puppeteer)
  • Loading branch information
YusukeIwaki authored Jul 19, 2020
2 parents 9e6c3aa + d36b6aa commit 99bad54
Show file tree
Hide file tree
Showing 5 changed files with 75 additions and 3 deletions.
2 changes: 1 addition & 1 deletion .circleci/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ jobs:
- run:
name: rspec
command: |
bundle exec rspec --profile 10 \
DEBUG=1 bundle exec rspec --profile 10 \
--format RspecJunitFormatter \
--out test_results/rspec.xml \
--format progress
Expand Down
16 changes: 14 additions & 2 deletions lib/puppeteer/browser.rb
Original file line number Diff line number Diff line change
Expand Up @@ -196,7 +196,7 @@ def targets

# @return {!Target}
def target
targets.first { |target| target.type == 'browser' }
targets.find { |target| target.type == 'browser' }
end

# used only in Target#opener
Expand Down Expand Up @@ -266,7 +266,19 @@ def connected?
!@connection.closed?
end

class Version
def initialize(hash)
@protocol_version = hash['protocolVersion']
@product = hash['product']
@revision = hash['revision']
@user_agent = hash['userAgent']
@js_version = hash['jsVersion']
end

attr_reader :protocol_version, :product, :revision, :user_agent, :js_version
end

private def get_version
@connection.send_message('Browser.getVersion')
Version.new(@connection.send_message('Browser.getVersion'))
end
end
21 changes: 21 additions & 0 deletions spec/integration/browser_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
require 'spec_helper'

RSpec.describe Puppeteer::Browser, puppeteer: :browser do
describe 'version' do
it 'should indicate we are in headless' do
expect(browser.version).to start_with('Headless')
end
end

describe 'user_agent' do
it 'should include WebKit' do
expect(browser.user_agent).to include('WebKit')
end
end

describe 'target' do
it 'should return browser target' do
expect(browser.target.type).to eq('browser')
end
end
end
10 changes: 10 additions & 0 deletions spec/integration/page_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
require 'spec_helper'

RSpec.describe Puppeteer::Page do
describe 'goto' do
it 'can fetch title soon after goto.' do
page.goto("https://github.com/YusukeIwaki/puppeteer-ruby")
expect(page.title).to include("YusukeIwaki/puppeteer-ruby")
end
end
end
29 changes: 29 additions & 0 deletions spec/spec_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,33 @@
config.expect_with :rspec do |c|
c.syntax = :expect
end

config.around(:each, type: :puppeteer) do |example|
if example.metadata[:puppeteer].to_s == 'browser'
Puppeteer.launch do |browser|
@puppeteer_browser = browser
example.run
end
else
Puppeteer.launch do |browser|
@puppeteer_page = browser.pages.first || browser.new_page
example.run
end
end
end

config.define_derived_metadata(file_path: %r(/spec/integration/)) do |metadata|
metadata[:type] = :puppeteer
end

module PuppeteerMethods
def browser
@puppeteer_browser or raise NoMethodError.new('undefined method "browser" (If you intended to use puppeteer#browser, you have to add `puppeteer: :browser` to metadata.)')
end

def page
@puppeteer_page or raise NoMethodError.new('undefined method "page"')
end
end
config.include PuppeteerMethods, type: :puppeteer
end

0 comments on commit 99bad54

Please sign in to comment.