How do I inject JS with this library?

main
ae3cad1Link attributions in Changelog

how do i inject js with this library??

Avatar of Claude Haiku 4.5Claude Haiku 4.5commented

I'll help you figure out how to inject JavaScript with this library. Let me explore the codebase first.

Let me check the README first to understand what this library does:

Now let me explore the lib directory to find JavaScript injection functionality:

Let me check what files are in the lib directory:

Let me try a different approach and look at the actual files:

Great! Let me look at the main API files:

Let me continue reading the file to find JavaScript injection functions:

Let me search more broadly:

Perfect! Now let me read more of the file to understand the API better:

Excellent! Now let me check the test file to see actual examples of JavaScript injection:

Perfect! Now let me get the complete section showing how to use unwrap and JavaScript injection:

Now let me check the documentation section in the main file more thoroughly:

Perfect! I have all the information I need. Let me provide you with a comprehensive answer.

##How to Inject JavaScript with phoenix_test_playwright

To inject and execute JavaScript in your tests, use the unwrap/2 function combined with PlaywrightEx.Frame.evaluate/2:

###Basic Usage

test "execute javascript", %{conn: conn} do
  conn
  |> visit(~p"/")
  |> unwrap(&Frame.evaluate(&1.frame_id, "console.log('Hey')"))
end

###How it Works

  1. unwrap/2 - Provides an escape hatch that gives you access to the underlying Playwright IDs:

    • context_id
    • page_id
    • frame_id
  2. PlaywrightEx.Frame.evaluate/2 - The underlying Playwright function from PlaywrightEx that executes JavaScript in the browser

###Examples

Log to console:

|> unwrap(&Frame.evaluate(&1.frame_id, "console.log('TESTME 42')"))

Run JavaScript and capture output:

|> unwrap(fn %{frame_id: frame_id} ->
  {:ok, result} = PlaywrightEx.Frame.evaluate(frame_id, expression: "2 + 2")
  IO.inspect(result)
end)

With timeout:

|> unwrap(&Frame.evaluate(&1.frame_id, expression: "console.log('test')", timeout: 5000))

###Note

The library also uses unwrap to access lower-level Playwright methods like PlaywrightEx.Frame.click/2 for more advanced browser interactions beyond the high-level PhoenixTest API.


END OF POST

Log in to add a comment
Claude Haiku 4.5
How do I inject JS with this library? — ftes/phoenix_test_playwright