-
Notifications
You must be signed in to change notification settings - Fork 145
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #73 from TheoAndersen/log
Added ability to fetch the console log
- Loading branch information
Showing
4 changed files
with
101 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
defmodule Hound.Helpers.Log do | ||
@moduledoc "Functions to work with the log" | ||
|
||
import Hound.RequestUtils | ||
require Hound.NotSupportedError | ||
|
||
@doc """ | ||
Fetches console.log() from the browser as a single string of log-lines. | ||
""" | ||
def fetch_log() do | ||
fail_if_webdriver_selenium("fetch_log()") | ||
|
||
get_browser_log() | ||
|> Enum.map(&(Map.get(&1, "message"))) | ||
|> Enum.join("\n") | ||
end | ||
|
||
@doc """ | ||
Fetches all console.log() lines of type 'WARINING' and CRITICAL, from the browser as a single string of log-lines. | ||
""" | ||
def fetch_errors() do | ||
fail_if_webdriver_selenium("fetch_errors()") | ||
|
||
get_browser_log() | ||
|> Enum.filter(&(is_error(&1))) | ||
|> Enum.map(&(Map.get(&1, "message"))) | ||
|> Enum.join("\n") | ||
end | ||
|
||
defp fail_if_webdriver_selenium(function) do | ||
Hound.NotSupportedError.raise_for(%{driver: "selenium", browser: "firefox"}, function) | ||
end | ||
|
||
defp get_browser_log() do | ||
session_id = Hound.current_session_id | ||
make_req(:post, "session/#{session_id}/log", %{type: "browser"}) | ||
end | ||
|
||
defp is_error(map) do | ||
level(map, "WARNING") || level(map, "CRITICAL") | ||
end | ||
|
||
defp level(map, value) do | ||
Map.get(map, "level") == value | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
defmodule LogTest do | ||
use ExUnit.Case | ||
use Hound.Helpers | ||
|
||
hound_session | ||
|
||
test "Should be able to extract log written in javascript" do | ||
navigate_to "http://localhost:9090/page1.html" | ||
|
||
execute_script("console.log(\"Some log\");") | ||
execute_script("console.log(\"Next log\");") | ||
|
||
if is_webdriver_selenium() do | ||
assert_raise Hound.NotSupportedError, "fetch_log() is not supported by driver selenium with browser firefox", fn -> | ||
fetch_log() | ||
end | ||
else | ||
log = fetch_log() | ||
|
||
assert log =~ "Some log" | ||
assert log =~ "Next log" | ||
end | ||
end | ||
|
||
test "Should be able to detect if theres any errors in the javascript" do | ||
navigate_to "http://localhost:9090/page_with_javascript_error.html" | ||
execute_script("console.log(\"Should not return normal logs\");") | ||
|
||
if is_webdriver_selenium() do | ||
assert_raise Hound.NotSupportedError, "fetch_errors() is not supported by driver selenium with browser firefox", fn -> | ||
fetch_errors() | ||
end | ||
else | ||
log = fetch_errors() | ||
refute log =~ "Should not return normal logs" | ||
assert log =~ "This is a javascript error" | ||
end | ||
end | ||
|
||
defp is_webdriver_selenium() do | ||
match?({:ok, %{driver: "selenium"}}, Hound.driver_info) | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
<!DOCTYPE html> | ||
<html> | ||
<head> | ||
<title>Hound Test Page</title> | ||
</head> | ||
<body> | ||
<script type="text/javascript"> | ||
throw "This is a javascript error"; | ||
</script> | ||
</body> | ||
</html> |