Module: Bidi2pdf::DSL

Defined in:
lib/bidi2pdf/dsl.rb

Class Method Summary collapse

Class Method Details

.with_tab(remote_browser_url: nil, port: 0, headless: true, chrome_args: Bidi2pdf::Bidi::Session::DEFAULT_CHROME_ARGS.dup) {|tab| ... } ⇒ void

This method returns an undefined value.

rubocop: disable Metrics/AbcSize

Executes a block of code within the context of a browser tab.

This method handles the setup and teardown of a browser session, user context, browser window, and tab. It ensures that resources are properly cleaned up after the block is executed.

Examples:

Using a local browser session

Bidi2pdf::DSL.with_tab(port: 9222, headless: false) do |tab|
  # Perform actions with the tab
end

Using a remote browser session

Bidi2pdf::DSL.with_tab(remote_browser_url: "http://remote-browser:9222/session") do |tab|
  # Perform actions with the tab
end

Yields:

  • (tab)

    The browser tab created within the session.

Yield Parameters:

  • tab (Object)

    The browser tab object.



40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/bidi2pdf/dsl.rb', line 40

def self.with_tab(remote_browser_url: nil, port: 0, headless: true, chrome_args: Bidi2pdf::Bidi::Session::DEFAULT_CHROME_ARGS.dup)
  manager = nil
  session = nil
  tab = nil

  begin
    session = if remote_browser_url
                Bidi2pdf::Bidi::Session.new(
                  session_url: remote_browser_url,
                  headless: true, # remote is always headless
                  chrome_args: chrome_args
                )
              else
                manager = Bidi2pdf::ChromedriverManager.new(port: port, headless: headless, chrome_args: chrome_args)
                manager.start
                manager.session
              end

    session.start
    session.client.on_close { Bidi2pdf.logger.info "WebSocket session closed" }

    browser = session.browser
    context = browser.create_user_context
    window = context.create_browser_window
    tab = window.create_browser_tab

    yield(tab)
  ensure
    tab&.close
    window&.close
    context&.close
    session&.close
    manager&.stop
  end
end