Class: Playwright::HarRouter

Inherits:
Object
  • Object
show all
Defined in:
lib/playwright/har_router.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(local_utils:, har_id:, not_found_action:, url_match: nil) ⇒ HarRouter

Returns a new instance of HarRouter.

Parameters:

  • local_utils (LocalUtils)
  • har_id (String)
  • not_found_action (String)

    ‘abort’ or ‘fallback’

  • url_match (String||Regexp|nil) (defaults to: nil)


22
23
24
25
26
27
28
29
30
31
32
# File 'lib/playwright/har_router.rb', line 22

def initialize(local_utils:, har_id:, not_found_action:,  url_match: nil)
  unless ['abort', 'fallback'].include?(not_found_action)
    raise ArgumentError.new("not_found_action must be either 'abort' or 'fallback'. '#{not_found_action}' is specified.")
  end

  @local_utils = local_utils
  @har_id = har_id
  @not_found_action = not_found_action
  @url_match = url_match || '**/*'
  @debug = ENV['DEBUG'].to_s == 'true' || ENV['DEBUG'].to_s == '1'
end

Class Method Details

.create(local_utils, file, not_found_action, url_match: nil) ⇒ Object

Parameters:

  • local_utils (LocalUtils)
  • file (String)
  • not_found_action (String)

    ‘abort’ or ‘fallback’

  • url_match (String||Regexp|nil) (defaults to: nil)


7
8
9
10
11
12
13
14
15
16
# File 'lib/playwright/har_router.rb', line 7

def self.create(local_utils, file, not_found_action, url_match: nil)
  har_id = local_utils.har_open(file)

  new(
    local_utils: local_utils,
    har_id: har_id,
    not_found_action: not_found_action,
    url_match: url_match,
  )
end

Instance Method Details

#add_context_route(context) ⇒ Object



68
69
70
71
# File 'lib/playwright/har_router.rb', line 68

def add_context_route(context)
  context.route(@url_match, method(:handle))
  context.once(Events::BrowserContext::Close, method(:dispose))
end

#add_page_route(page) ⇒ Object



73
74
75
76
# File 'lib/playwright/har_router.rb', line 73

def add_page_route(page)
  page.route(@url_match, method(:handle))
  page.once(Events::Page::Close, method(:dispose))
end

#disposeObject



78
79
80
# File 'lib/playwright/har_router.rb', line 78

def dispose
  @local_utils.async_har_close(@har_id)
end