Skip to content

Commit

Permalink
Add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
ww24 committed Apr 29, 2020
1 parent 858c080 commit 754e21e
Show file tree
Hide file tree
Showing 5 changed files with 290 additions and 8 deletions.
229 changes: 221 additions & 8 deletions helper_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,33 +4,246 @@ import (
"bytes"
"context"
"errors"
"fmt"
"image"
_ "image/png"
"io"
"io/ioutil"
"log"
"net/http"
"net/http/httptest"
"os"
"path"
"path/filepath"
"sync"
"testing"
"time"

"github.com/chromedp/cdproto/network"
cdpruntime "github.com/chromedp/cdproto/runtime"
"github.com/chromedp/chromedp"
)

var (
allocOpts = append(chromedp.DefaultExecAllocatorOptions[:],
chromedp.DisableGPU, chromedp.NoSandbox)
browserOpts []chromedp.ContextOption
allocateOnce sync.Once
startServerOnce sync.Once
allocCtx context.Context
browserCtx context.Context
testServer *httptest.Server
testdataDir string
testdataURL string
)

func init() {
wd, err := os.Getwd()
if err != nil {
panic(fmt.Sprintf("could not get working directory: %v", err))
}
testdataDir = filepath.Join(wd, "testdata")
testdataURL = "file://" + path.Join(wd, "testdata")
}

func TestMain(m *testing.M) {
var cancel context.CancelFunc
allocCtx, cancel = chromedp.NewExecAllocator(context.Background(), allocOpts...)

if debug := os.Getenv("DEBUG"); debug != "" && debug != "false" {
browserOpts = append(browserOpts, chromedp.WithDebugf(log.Printf))
}

code := m.Run()
cancel()

if testServer != nil {
testServer.Close()
}

os.Exit(code)
}

func testAllocate(tb testing.TB) (context.Context, context.CancelFunc) {
allocateOnce.Do(func() {
ctx, _ := chromedp.NewContext(allocCtx, browserOpts...)
if err := chromedp.Run(ctx); err != nil {
tb.Fatal(err)
}
chromedp.ListenBrowser(ctx, func(ev interface{}) {
switch ev := ev.(type) {
case *cdpruntime.EventExceptionThrown:
tb.Errorf("%+v\n", ev.ExceptionDetails)
}
})
browserCtx = ctx
})

if browserCtx == nil {
tb.FailNow()
}

// Create new tab of existing browser.
ctx, _ := chromedp.NewContext(browserCtx)
cancel := func() {
if err := chromedp.Cancel(ctx); err != nil {
tb.Error(err)
}
}
return ctx, cancel
}

func testStartServer(tb testing.TB) string {
startServerOnce.Do(func() {
mux := http.NewServeMux()
mux.HandleFunc("/image.png", func(w http.ResponseWriter, r *http.Request) {
time.Sleep(2 * time.Second)
http.ServeFile(w, r, filepath.Join(testdataDir, "image.png"))
})
mux.Handle("/", http.FileServer(http.Dir(testdataDir)))
testServer = httptest.NewServer(mux)
})

if testServer == nil {
tb.FailNow()
}

return testServer.URL
}

func TestScreenshot(t *testing.T) {
t.Skip("TODO")
t.Parallel()
ctx, cancel := testAllocate(t)
defer cancel()

dir, err := ioutil.TempDir("", "chromedp-helper-test")
if err != nil {
t.Fatalf("failed to create temporary directory: %v", err)
}
sspath := filepath.Join(dir, "screenshot.png")
log.Println("sspath:", sspath)

tasks := chromedp.Tasks{
chromedp.Navigate(testdataURL + "/screenshot.html"),
Screenshot(sspath),
}
if err := chromedp.Run(ctx, tasks); err != nil {
t.Fatal(err)
}

f, err := os.Open(sspath)
if err != nil {
t.Fatalf("failed to open screenshot file: %v", err)
}
defer f.Close()

config, format, err := image.DecodeConfig(f)
if err != nil {
t.Fatalf("failed to decode image config: %v", err)
}

const wantFormat = "png"
const wantWidth = 1200
const wantHeight = 1234
if format != wantFormat {
t.Fatalf("expected format to be %q, got %q", wantFormat, format)
}
if config.Width != wantWidth || config.Height != wantHeight {
t.Fatalf("expected dimensions to be %d*%d, got %d*%d",
wantWidth, wantHeight, config.Width, config.Height)
}
}

func TestNavigate(t *testing.T) {
t.Skip("TODO")
t.Parallel()
ctx, cancel := testAllocate(t)
defer cancel()
endpoint := testStartServer(t)

var got string
tasks := chromedp.Tasks{
network.Enable(),
EnableLifeCycleEvents(),
Navigate(endpoint+"/navigate.html", 5*time.Second),
chromedp.Text("#text", &got),
}
if err := chromedp.Run(ctx, tasks); err != nil {
t.Fatal(err)
}
const want = "DOMContentLoaded"
if got != want {
t.Fatalf("expected text to be %q, got %q", want, got)
}
}

func TestIgnoreCacheReload(t *testing.T) {
t.Skip("TODO")
}
t.Parallel()
ctx, cancel := testAllocate(t)
defer cancel()
endpoint := testStartServer(t)

func TestEnableLifeCycleEvents(t *testing.T) {
t.Skip("TODO")
var got string
tasks := chromedp.Tasks{
network.Enable(),
EnableLifeCycleEvents(),
chromedp.Navigate(endpoint + "/navigate.html"),
IgnoreCacheReload(5 * time.Second),
chromedp.Text("#text", &got),
}
if err := chromedp.Run(ctx, tasks); err != nil {
t.Fatal(err)
}
const want = "DOMContentLoaded"
if got != want {
t.Fatalf("expected text to be %q, got %q", want, got)
}
}

func TestWaitResponse(t *testing.T) {
t.Skip("TODO")
t.Parallel()
ctx, cancel := testAllocate(t)
defer cancel()
endpoint := testStartServer(t)

var got string
tasks := chromedp.Tasks{
network.Enable(),
EnableLifeCycleEvents(),
chromedp.Navigate(endpoint + "/index.html"),
WaitResponse(endpoint+"/navigate.html", 5*time.Second,
chromedp.Click(`a[href="navigate.html"]`),
),
chromedp.Text("#text", &got),
}
if err := chromedp.Run(ctx, tasks); err != nil {
t.Fatal(err)
}
const want = "DOMContentLoaded"
if got != want {
t.Fatalf("expected text to be %q, got %q", want, got)
}
}

func TestWaitLoaded(t *testing.T) {
t.Skip("TODO")
t.Parallel()
ctx, cancel := testAllocate(t)
defer cancel()
endpoint := testStartServer(t)

var got string
tasks := chromedp.Tasks{
chromedp.Navigate(endpoint + "/index.html"),
chromedp.Click(`a[href="navigate.html"]`),
WaitLoaded(5 * time.Second),
chromedp.Text("#text", &got),
}
if err := chromedp.Run(ctx, tasks); err != nil {
t.Fatal(err)
}
const want = "loaded"
if got != want {
t.Fatalf("expected text to be %q, got %q", want, got)
}
}

func TestWaitInput(t *testing.T) {
Expand Down
Binary file added testdata/image.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
17 changes: 17 additions & 0 deletions testdata/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<!DOCTYPE html>
<html>

<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document for the test</title>
</head>

<body>
<article>
<header style="background-color: turquoise;">Test</header>
<a href="navigate.html">navigate</a>
</article>
</body>

</html>
28 changes: 28 additions & 0 deletions testdata/navigate.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<!DOCTYPE html>
<html>

<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document for the test</title>
</head>

<body>
<article>
<header style="background-color: turquoise;">Test</header>
<div id="text">
not loaded
</div>
</article>
<script>
window.addEventListener("DOMContentLoaded", () => {
document.getElementById("text").innerText = "DOMContentLoaded";
});
window.addEventListener("load", () => {
document.getElementById("text").innerText = "loaded";
});
</script>
<img src="./image.png" alt="test image">
</body>

</html>
24 changes: 24 additions & 0 deletions testdata/screenshot.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<!DOCTYPE html>
<html>

<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document for the test</title>
<style>
body {
width: 1200px;
height: 1234px;
margin: 0;
}
</style>
</head>

<body>
<article>
<header style="background-color: turquoise;">Test</header>
testtesttest.
</article>
</body>

</html>

0 comments on commit 754e21e

Please sign in to comment.