Skip to content

Commit 6b148fe

Browse files
authored
Log location urls for nicer error messages (#41)
* log location urls for nicer error messages like 14:41:19.464 [error] Javascript console: Failed to load resource: the server responded with a status of 404 (Not Found) (http://localhost:4002/afile.css) * don't log unknown location and add the line number * Add tests for PhoenixTest.Playwright.Connection.add_location/1
1 parent 3a38e71 commit 6b148fe

File tree

2 files changed

+118
-4
lines changed

2 files changed

+118
-4
lines changed

lib/phoenix_test/playwright/connection.ex

Lines changed: 30 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,9 @@ defmodule PhoenixTest.Playwright.Connection do
4949
def launch_browser(type, opts) do
5050
types = initializer("Playwright")
5151
type_id = Map.fetch!(types, type).guid
52-
timeout = opts[:browser_launch_timeout] || opts[:timeout] || Config.global(:browser_launch_timeout)
52+
53+
timeout =
54+
opts[:browser_launch_timeout] || opts[:timeout] || Config.global(:browser_launch_timeout)
5355

5456
params =
5557
opts
@@ -93,7 +95,12 @@ defmodule PhoenixTest.Playwright.Connection do
9395
"""
9496
def post(msg, timeout \\ nil) do
9597
default = %{params: %{}, metadata: %{}}
96-
msg = msg |> Enum.into(default) |> update_in(~w(params timeout)a, &(&1 || timeout || Config.global(:timeout)))
98+
99+
msg =
100+
msg
101+
|> Enum.into(default)
102+
|> update_in(~w(params timeout)a, &(&1 || timeout || Config.global(:timeout)))
103+
97104
call_timeout = max(@min_genserver_timeout, round(msg.params.timeout * @timeout_grace_factor))
98105
GenServer.call(@name, {:post, msg}, call_timeout)
99106
end
@@ -161,7 +168,10 @@ defmodule PhoenixTest.Playwright.Connection do
161168
end
162169

163170
defp log_js_error(state, %{method: :page_error} = msg) do
164-
Logger.error("Javascript error: #{inspect(msg.params.error)}")
171+
"Javascript error: #{inspect(msg.params.error)}"
172+
|> add_location(msg)
173+
|> Logger.error()
174+
165175
state
166176
end
167177

@@ -177,7 +187,9 @@ defmodule PhoenixTest.Playwright.Connection do
177187
_ -> :info
178188
end
179189

180-
Logger.log(level, "Javascript console: #{msg.params.text}")
190+
"Javascript console: #{msg.params.text}"
191+
|> add_location(msg)
192+
|> tap(&Logger.log(level, &1))
181193

182194
fun when is_function(fun, 1) ->
183195
fun.(msg)
@@ -194,6 +206,20 @@ defmodule PhoenixTest.Playwright.Connection do
194206

195207
defp log_console(state, _), do: state
196208

209+
def add_location(text, %{params: %{location: %{url: url, line_number: 0}}} = _message) do
210+
"#{text} (#{url})"
211+
end
212+
213+
def add_location(text, %{params: %{location: %{url: url, line_number: line_number}}} = _message) do
214+
"#{text} (#{url}:#{line_number})"
215+
end
216+
217+
def add_location(text, %{params: %{location: %{url: url}}} = _message) do
218+
"#{text} (#{url})"
219+
end
220+
221+
def add_location(text, _message), do: text
222+
197223
defp handle_started(state, %{method: :__create__, params: %{type: "Playwright"}}) do
198224
for from <- state.awaiting_started, do: GenServer.reply(from, :ok)
199225
%{state | status: :started, awaiting_started: :none}

test/phoenix_test/playwright/connection_test.exs

Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,4 +25,92 @@ defmodule PhoenixTest.Playwright.ConnectionTest do
2525
end
2626
end
2727
end
28+
29+
test "add_location/1 accepts unknown data" do
30+
message = %{a: 1}
31+
assert "Hi" == Connection.add_location("Hi", message)
32+
end
33+
34+
test "add_location/1 adds the file location to console messages" do
35+
# a complete message
36+
message = %{
37+
params: %{
38+
args: [%{guid: "handle@2ae9bdee66c01d900097b961352f69f4"}],
39+
type: "log",
40+
location: %{
41+
url: "http://localhost:4002/assets/app.js",
42+
line_number: 7085,
43+
column_number: 10
44+
},
45+
text: "hello world",
46+
page: %{guid: "page@47f9839e9773e11062e7794049ae830c"}
47+
},
48+
method: :console,
49+
guid: "browser-context@6cb68e0f88cb4d3de5f56d4d91de65da"
50+
}
51+
52+
assert "Hi (http://localhost:4002/assets/app.js:7085)" == Connection.add_location("Hi", message)
53+
54+
file_not_found = %{
55+
params: %{
56+
args: [],
57+
type: "error",
58+
location: %{
59+
url: "http://localhost:4002/file.css",
60+
line_number: 0,
61+
column_number: 0
62+
},
63+
text: "Failed to load resource: the server responded with a status of 404 (Not Found)",
64+
page: %{guid: "page@7afde413c6394658641b41c790d79647"}
65+
},
66+
guid: "browser-context@a02c2c526d9615e458f0ad540722911d",
67+
method: :console
68+
}
69+
70+
assert "Hi (http://localhost:4002/file.css)" == Connection.add_location("Hi", file_not_found)
71+
72+
# shortend, line number 0
73+
message = %{
74+
params: %{
75+
type: "log",
76+
location: %{
77+
url: "http://localhost:4002/assets/app.js",
78+
line_number: 0
79+
},
80+
text: "hello world"
81+
},
82+
method: :console
83+
}
84+
85+
assert "Hi (http://localhost:4002/assets/app.js)" == Connection.add_location("Hi", message)
86+
87+
# shortend, line number 0
88+
message = %{
89+
params: %{
90+
type: "log",
91+
location: %{
92+
url: "http://localhost:4002/assets/app.js"
93+
},
94+
text: "hello world"
95+
},
96+
method: :console
97+
}
98+
99+
assert "Hi (http://localhost:4002/assets/app.js)" == Connection.add_location("Hi", message)
100+
101+
# shortend, line number 0, an error
102+
message = %{
103+
params: %{
104+
type: "log",
105+
location: %{
106+
url: "http://localhost:4002/assets/app.js",
107+
line_number: 0
108+
},
109+
text: "hello world"
110+
},
111+
method: :console
112+
}
113+
114+
assert "Hi (http://localhost:4002/assets/app.js)" == Connection.add_location("Hi", message)
115+
end
28116
end

0 commit comments

Comments
 (0)