Skip to content

Commit

Permalink
fix formatting, add more thorough tests
Browse files Browse the repository at this point in the history
  • Loading branch information
JonasGruenwald committed Jun 22, 2024
1 parent d58094d commit cbb46e6
Show file tree
Hide file tree
Showing 5 changed files with 196 additions and 5 deletions.
114 changes: 114 additions & 0 deletions birdie_snapshots/created_page_with_reference_html.accepted
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
---
version: 1.1.5
title: Created Page with Reference HTML
file: ./test/chrobot_test.gleam
test_name: create_page_test
---
<html><head>
<meta http-equiv="content-type" content="text/html; charset=windows-1252">
</head>

<body>
<header>
<title>The World Wide Web project</title>
<nextid n="55">
</nextid>
</header>

<h1>World Wide Web</h1>The WorldWideWeb (W3) is a wide-area<a name="0" href="http://info.cern.ch/hypertext/WWW/WhatIs.html">
hypermedia</a> information retrieval
initiative aiming to give universal
access to a large universe of documents.<p>
Everything there is online about
W3 is linked directly or indirectly
to this document, including an <a name="24" href="http://info.cern.ch/hypertext/WWW/Summary.html">executive
summary</a> of the project, <a name="29" href="http://info.cern.ch/hypertext/WWW/Administration/Mailing/Overview.html">Mailing lists</a>
, <a name="30" href="http://info.cern.ch/hypertext/WWW/Policy.html">Policy</a> , November's <a name="34" href="http://info.cern.ch/hypertext/WWW/News/9211.html">W3 news</a> ,
<a name="41" href="http://info.cern.ch/hypertext/WWW/FAQ/List.html">Frequently Asked Questions</a> .
</p>
<dl>
<dt><a name="44" href="http://info.cern.ch/hypertext/DataSources/Top.html">What's out there?</a>
</dt>
<dd> Pointers to the
world's online information,<a name="45" href="http://info.cern.ch/hypertext/DataSources/bySubject/Overview.html">
subjects</a>
, <a name="z54" href="http://info.cern.ch/hypertext/DataSources/WWW/Servers.html">W3 servers</a>, etc.
</dd>
<dt><a name="46" href="http://info.cern.ch/hypertext/WWW/Help.html">Help</a>
</dt>
<dd> on the browser you are using
</dd>
<dt><a name="13" href="http://info.cern.ch/hypertext/WWW/Status.html">Software Products</a>
</dt>
<dd> A list of W3 project
components and their current state.
(e.g. <a name="27" href="http://info.cern.ch/hypertext/WWW/LineMode/Browser.html">Line Mode</a> ,X11 <a name="35" href="http://info.cern.ch/hypertext/WWW/Status.html#35">Viola</a> , <a name="26" href="http://info.cern.ch/hypertext/WWW/NeXT/WorldWideWeb.html">NeXTStep</a>
, <a name="25" href="http://info.cern.ch/hypertext/WWW/Daemon/Overview.html">Servers</a> , <a name="51" href="http://info.cern.ch/hypertext/WWW/Tools/Overview.html">Tools</a> ,<a name="53" href="http://info.cern.ch/hypertext/WWW/MailRobot/Overview.html"> Mail robot</a> ,<a name="52" href="http://info.cern.ch/hypertext/WWW/Status.html#57">
Library</a> )
</dd>
<dt><a name="47" href="http://info.cern.ch/hypertext/WWW/Technical.html">Technical</a>
</dt>
<dd> Details of protocols, formats,
program internals etc
</dd>
<dt><a name="40" href="http://info.cern.ch/hypertext/WWW/Bibliography.html">Bibliography</a>
</dt>
<dd> Paper documentation
on W3 and references.
</dd>
<dt><a name="14" href="http://info.cern.ch/hypertext/WWW/People.html">People</a>
</dt>
<dd> A list of some people involved
in the project.
</dd>
<dt><a name="15" href="http://info.cern.ch/hypertext/WWW/History.html">History</a>
</dt>
<dd> A summary of the history
of the project.
</dd>
<dt><a name="37" href="http://info.cern.ch/hypertext/WWW/Helping.html">How can I help</a> ?
</dt>
<dd> If you would like
to support the web..
</dd>
<dt><a name="48" href="http://info.cern.ch/hypertext/README.html">Getting code</a>
</dt>
<dd> Getting the code by<a name="49" href="http://info.cern.ch/hypertext/WWW/LineMode/Defaults/Distribution.html">
anonymous FTP</a> , etc.
</dd>
</dl>

<div>
<h2>And now for something completely different</h2>
<div id="wibble">
Wibble
</div>
<div id="wobble" title="wobble" data-foo="bar">
Wobble
</div>

<div>
<label for="demo-checkbox">This is a demo checkbox to test page interactivity</label>
<input type="checkbox" id="demo-checkbox" name="demo-checkbox" value="demo-checkbox" checked="">
</div>

<div>
<label for="demo-text-input"> This is a demo text input to test page interactivity</label>
<input type="text" id="demo-text-input" name="demo-text-input">
</div>
</div>

<script>
document.querySelector("#demo-text-input").addEventListener("keydown", function (e) {
if (e.key === "Enter") {
e.target.value = "ENTER KEY PRESSED"
}
console.log('keydown :', e)
})
document.querySelector("#demo-text-input").addEventListener("keyup", function (e) {
console.log('keyup:', e)
})
</script>


</body></html>
15 changes: 15 additions & 0 deletions birdie_snapshots/opened_sample_page.accepted
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
---
version: 1.1.5
title: Opened Sample Page
file: ./test/chrobot_test.gleam
test_name: open_test
---
<html><head>
<title>Chrobot Test Page</title>
</head>
<body>
<h1>Chrobot Test Page</h1>
<div id="wibble">wobble</div>


</body></html>
4 changes: 3 additions & 1 deletion src/chrobot.gleam
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,9 @@ pub fn launch_window() -> Result(Subject(chrome.Message), chrome.LaunchError) {
/// )
/// let assert Ok(browser_subject) = launch_with_config(config)
/// ```
pub fn launch_with_config(config: chrome.BrowserConfig) -> Result(Subject(chrome.Message), chrome.LaunchError) {
pub fn launch_with_config(
config: chrome.BrowserConfig,
) -> Result(Subject(chrome.Message), chrome.LaunchError) {
validate_launch(chrome.launch_with_config(config))
}

Expand Down
2 changes: 1 addition & 1 deletion src/chrome.gleam
Original file line number Diff line number Diff line change
Expand Up @@ -444,7 +444,7 @@ pub fn get_local_chrome_path_at(base_dir: String) {
}

/// Try to find a system chrome installation in some obvious places.
pub fn get_system_chrome_path() {
pub fn get_system_chrome_path() {
case os.family() {
os.Darwin ->
get_first_existing_path([
Expand Down
66 changes: 63 additions & 3 deletions test/chrobot_test.gleam
Original file line number Diff line number Diff line change
Expand Up @@ -69,21 +69,81 @@ pub fn open_test() {
|> chrobot.as_value(dynamic.string)
|> should.be_ok()
|> should.equal(test_url)

chrobot.get_all_html(page)
|> should.be_ok()
|> birdie.snap(title: "Opened Sample Page")
}

pub fn create_page_test() {
let browser = test_utils.get_browser_instance()
use <- chrobot.defer_quit(browser)
let reference_html = test_utils.get_reference_html()
let _page = should.be_ok(chrobot.create_page(browser, reference_html, 10_000))
let page = should.be_ok(chrobot.create_page(browser, reference_html, 10_000))

chrobot.get_all_html(page)
|> should.be_ok()
|> birdie.snap(title: "Created Page with Reference HTML")
}

pub fn eval_test() {
use page <- test_utils.with_reference_page()
let expression = "2 * Math.PI"
chrobot.eval(page, expression)
|> chrobot.as_value(dynamic.float)
|> should.be_ok()
|> should.equal(6.283185307179586)
}

pub fn eval_async_test() {
use page <- test_utils.with_reference_page()
let expression =
"new Promise((resolve, reject) => setTimeout(() => resolve(42), 50))"
chrobot.eval_async(page, expression)
|> chrobot.as_value(dynamic.int)
|> should.be_ok()
|> should.equal(42)
}

pub fn eval_async_failure_test() {
use page <- test_utils.with_reference_page()
let expression = "Promise.reject(new Error('This is a test error'))"
let result = chrobot.eval_async(page, expression)
case result {
Error(chrome.RuntimeException(text: text, column: column, line: line)) -> {
text
|> should.equal("Uncaught (in promise) Error: This is a test error")
column
|> should.equal(0)
line
|> should.equal(0)
}
other -> {
utils.err(
"Expected a chrome.RuntimeException, got: \n" <> string.inspect(other),
)
panic as "Test failed! the result was not a chrome.RuntimeException!"
}
}
}

pub fn await_selector_test() {
use page <- test_utils.with_reference_page()
chrobot.await_selector(page, "body")
|> should.be_ok()
}

pub fn await_selector_failure_test() {
let browser = test_utils.get_browser_instance()
use <- chrobot.defer_quit(browser)
let reference_html = test_utils.get_reference_html()
let page = should.be_ok(chrobot.create_page(browser, reference_html, 10_000))
should.be_ok(chrobot.await_selector(page, "body"))
let page =
chrobot.create_page(browser, reference_html, 10_000)
|> should.be_ok()
|> chrobot.with_timeout(100)

chrobot.await_selector(page, "#bogus")
|> should.be_error()
}

pub fn get_all_html_test() {
Expand Down

0 comments on commit cbb46e6

Please sign in to comment.