Skip to content

Commit 770ff0f

Browse files
committed
0.10.0 - improved -q option, it is now acts as document.querySelectorAll() so it will return all the matching nodes.
1 parent 2557204 commit 770ff0f

File tree

9 files changed

+82
-79
lines changed

9 files changed

+82
-79
lines changed

.travis.yml

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,29 @@
11
language: go
2+
23
go:
3-
- 1.13.x
4+
- 1.14.x
5+
46
env:
5-
- GO111MODULE=on
6-
install:
7-
- go get -v ./...
7+
- GO111MODULE=on
8+
9+
before_install:
10+
- go get -t -v ./...
11+
812
script:
9-
- "./_bin/test.sh"
13+
- go test -coverprofile=coverage.txt -covermode=atomic
14+
15+
after_success:
16+
- bash <(curl -s https://codecov.io/bash)
17+
1018
before_deploy:
11-
- "./_bin/build.sh darwin ${TRAVIS_TAG}"
12-
- "./_bin/build.sh linux ${TRAVIS_TAG}"
19+
- "./_bin/build.sh darwin ${TRAVIS_TAG}"
20+
- "./_bin/build.sh linux ${TRAVIS_TAG}"
21+
1322
deploy:
1423
provider: releases
1524
api_key:
1625
secure: dDafGtm1CpthsN3pHyciGvq+n7OiFJfiq0A32/FujYz9aBx8w4ZxMH8X4wiqw1Cy0SVRKx7Z35gVBBrYM0PZQ/Anpkm0epnZ6RfoGOkpgklKvK6T27pLYlwMnHBVPDbMVluD3I0XRvVT1dRuT0eMWsdnGy6Dh07BJtnKlsRdAbMaTlWI4OkdpS1/WoOUkxs5WuGtJ3qY7KjF0EJj21yC/8kYqrKOhjKGknggJWByvcVUNA8N6xb28TM+yDHiYAuRkUrCgnM64gFlSr8C/Tt5F/I+BaOlC7U4kdcyX+6HM06vZHN2yxLKtsKjuyhZK8D7F1Xa1heisH44jTVCg7hnwRasGKzkc9PHB+eeCTcnKjFTREgMmkEVQHD8YyJKYPxtp0ouS7Srtb/5yylUbijd8JyY8wXbUdjbnRWTYuauDajUiPM6rq/iXIpzbsM5emWRhQ14H7DMzlZnEI8DvovB0/WEGpd+Z3u182VAlgJrbdgyqNqeTRyPEjo1jOLT8hUM6lKSDXI4mMm+DjUGMV9mCkVQgFdhXlkMjRcNranNi3aa6QddwLnb5O5SOgNwkEeRuzX/opG0tC3tkbiMbijIgeE9CMZbJWJGGrGl/6KzcB3D2H/SEMlSx8ONLCAWmkRniFsEwDOc/4OJDA2x4lEhkpprzKonryqoJ4a9yBgHOBo=
17-
file:
26+
file:
1827
- "inout_darwin_${TRAVIS_TAG}"
1928
- "inout_linux_${TRAVIS_TAG}"
2029
on:

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
# Changelog
22
All notable changes to this project will be documented in this file.
33

4+
## 0.10.0
5+
- improved `-q` option, it is now acts as `document.querySelectorAll()` so it will return all the matching nodes.
6+
47
## 0.9.1
58
- added retries with backoff to HTML calls;
69
- fixed `-q` option, which allows querying with CSS selectors.

client_test.go

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,16 @@
11
package inout
22

33
import (
4-
"fmt"
54
"io/ioutil"
65
"testing"
76

87
"github.com/stretchr/testify/assert"
98
)
109

1110
func Test_fetch(t *testing.T) {
12-
defer stopServer(startServer(fmt.Sprintf(":%d", 8666), clientHTML, 200))
13-
res := fetch("http://localhost:8666")
11+
ts := startServer(clientHTML, 200)
12+
defer ts.Close()
13+
res := fetch(ts.URL)
1414

1515
assert.NotNil(t, res)
1616

@@ -24,8 +24,9 @@ func Test_fetch(t *testing.T) {
2424
}
2525

2626
func Test_fetch_retries(t *testing.T) {
27-
defer stopServer(startServer(fmt.Sprintf(":%d", 8666), "", 429))
28-
res := fetch("http://localhost:8666")
27+
ts := startServer("", 429)
28+
defer ts.Close()
29+
res := fetch(ts.URL)
2930
assert.NotNil(t, res)
3031
assert.NotNil(t, res.err)
3132
assert.Equal(t, 3, res.attempts)

go.mod

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
module github.com/zoomio/inout
22

3-
go 1.12
3+
go 1.14
44

55
require (
6-
github.com/chromedp/cdproto v0.0.0-20190429085128-1aa4f57ff2a9
7-
github.com/chromedp/chromedp v0.3.1
6+
github.com/chromedp/cdproto v0.0.0-20200116234248-4da64dd111ac
7+
github.com/chromedp/chromedp v0.5.3
88
github.com/davecgh/go-spew v1.1.1 // indirect
99
github.com/kr/pretty v0.2.0 // indirect
1010
github.com/stretchr/testify v1.5.1
11-
golang.org/x/sys v0.0.0-20200223170610-d5e6a3e2c0ae // indirect
11+
golang.org/x/sys v0.0.0-20200302150141-5c8b2ff67527 // indirect
1212
gopkg.in/check.v1 v1.0.0-20190902080502-41f04d3bba15 // indirect
1313
gopkg.in/yaml.v2 v2.2.8 // indirect
1414
)

go.sum

Lines changed: 13 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,36 +1,34 @@
1-
github.com/chromedp/cdproto v0.0.0-20190429085128-1aa4f57ff2a9 h1:ARnDd2vEk91rLNra8yk1hF40H8z+1HrD6juNpe7FsI0=
2-
github.com/chromedp/cdproto v0.0.0-20190429085128-1aa4f57ff2a9/go.mod h1:xquOK9dIGFlLaIGI4c6IyfLI/Gz0LiYYuJtzhsUODgI=
3-
github.com/chromedp/chromedp v0.3.1 h1:QV2NmprlDOMssqCjxemgHehVldLB2wp+Spa+bRbQGmc=
4-
github.com/chromedp/chromedp v0.3.1/go.mod h1:EktsZcC2iycVrRhC9fDmshBpCK9lNnZYi6x2q9uE7zI=
1+
github.com/chromedp/cdproto v0.0.0-20200116234248-4da64dd111ac h1:T7V5BXqnYd55Hj/g5uhDYumg9Fp3rMTS6bykYtTIFX4=
2+
github.com/chromedp/cdproto v0.0.0-20200116234248-4da64dd111ac/go.mod h1:PfAWWKJqjlGFYJEidUM6aVIWPr0EpobeyVWEEmplX7g=
3+
github.com/chromedp/chromedp v0.5.3 h1:F9LafxmYpsQhWQBdCs+6Sret1zzeeFyHS5LkRF//Ffg=
4+
github.com/chromedp/chromedp v0.5.3/go.mod h1:YLdPtndaHQ4rCpSpBG+IPpy9JvX0VD+7aaLxYgYj28w=
55
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
66
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
77
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
88
github.com/gobwas/httphead v0.0.0-20180130184737-2c6c146eadee h1:s+21KNqlpePfkah2I+gwHF8xmJWRjooY+5248k6m4A0=
99
github.com/gobwas/httphead v0.0.0-20180130184737-2c6c146eadee/go.mod h1:L0fX3K22YWvt/FAX9NnzrNzcI4wNYi9Yku4O0LKYflo=
1010
github.com/gobwas/pool v0.2.0 h1:QEmUOlnSjWtnpRGHF3SauEiOsy82Cup83Vf2LcMlnc8=
1111
github.com/gobwas/pool v0.2.0/go.mod h1:q8bcK0KcYlCgd9e7WYLm9LpyS+YeLd8JVDW6WezmKEw=
12-
github.com/gobwas/ws v1.0.0 h1:1WdyfgUcImUfVBvYbsW2krIsnko+1QU2t45soaF8v1M=
13-
github.com/gobwas/ws v1.0.0/go.mod h1:szmBTxLgaFppYjEmNtny/v3w89xOydFnnZMcgRRu/EM=
14-
github.com/knq/sysutil v0.0.0-20181215143952-f05b59f0f307 h1:vl4eIlySbjertFaNwiMjXsGrFVK25aOWLq7n+3gh2ls=
15-
github.com/knq/sysutil v0.0.0-20181215143952-f05b59f0f307/go.mod h1:BjPj+aVjl9FW/cCGiF3nGh5v+9Gd3VCgBQbod/GlMaQ=
12+
github.com/gobwas/ws v1.0.2 h1:CoAavW/wd/kulfZmSIBt6p24n4j7tHgNVCjsfHVNUbo=
13+
github.com/gobwas/ws v1.0.2/go.mod h1:szmBTxLgaFppYjEmNtny/v3w89xOydFnnZMcgRRu/EM=
14+
github.com/knq/sysutil v0.0.0-20191005231841-15668db23d08 h1:V0an7KRw92wmJysvFvtqtKMAPmvS5O0jtB0nYo6t+gs=
15+
github.com/knq/sysutil v0.0.0-20191005231841-15668db23d08/go.mod h1:dFWs1zEqDjFtnBXsd1vPOZaLsESovai349994nHx3e0=
1616
github.com/kr/pretty v0.2.0 h1:s5hAObm+yFO5uHYt5dYjxi2rXrsnmRpJx4OYvIWUaQs=
1717
github.com/kr/pretty v0.2.0/go.mod h1:ipq/a2n7PKx3OHsz4KJII5eveXtPO4qwEXGdVfWzfnI=
1818
github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ=
1919
github.com/kr/text v0.1.0 h1:45sCR5RtlFHMR4UwH9sdQ5TC8v0qDQCHnXt+kaKSTVE=
2020
github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI=
21-
github.com/mailru/easyjson v0.0.0-20180823135443-60711f1a8329/go.mod h1:C1wdFJiN94OJF2b5HbByQZoLdCWB1Yqtg26g4irojpc=
22-
github.com/mailru/easyjson v0.0.0-20190403194419-1ea4449da983 h1:wL11wNW7dhKIcRCHSm4sHKPWz0tt4mwBsVodG7+Xyqg=
23-
github.com/mailru/easyjson v0.0.0-20190403194419-1ea4449da983/go.mod h1:C1wdFJiN94OJF2b5HbByQZoLdCWB1Yqtg26g4irojpc=
21+
github.com/mailru/easyjson v0.7.0 h1:aizVhC/NAAcKWb+5QsU1iNOZb4Yws5UO2I+aIprQITM=
22+
github.com/mailru/easyjson v0.7.0/go.mod h1:KAzv3t3aY1NaHWoQz1+4F1ccyAH66Jk7yos7ldAVICs=
2423
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
2524
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
2625
github.com/stretchr/objx v0.1.0 h1:4G4v2dO3VZwixGIRoQ5Lfboy6nUhCyYzaqnIAPPhYs4=
2726
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
2827
github.com/stretchr/testify v1.5.1 h1:nOGnQDM7FYENwehXlg/kFVnos3rEvtKTjRvOWSzb6H4=
2928
github.com/stretchr/testify v1.5.1/go.mod h1:5W2xD1RspED5o8YsWQXVCued0rvSQ+mT+I5cxcmMvtA=
30-
golang.org/x/sys v0.0.0-20190509141414-a5b02f93d862 h1:rM0ROo5vb9AdYJi1110yjWGMej9ITfKddS89P3Fkhug=
31-
golang.org/x/sys v0.0.0-20190509141414-a5b02f93d862/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
32-
golang.org/x/sys v0.0.0-20200223170610-d5e6a3e2c0ae h1:/WDfKMnPU+m5M4xB+6x4kaepxRw6jWvR5iDRdvjHgy8=
33-
golang.org/x/sys v0.0.0-20200223170610-d5e6a3e2c0ae/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
29+
golang.org/x/sys v0.0.0-20200116001909-b77594299b42/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
30+
golang.org/x/sys v0.0.0-20200302150141-5c8b2ff67527 h1:uYVVQ9WP/Ds2ROhcaGPeIdVq0RIXVLwsHlnvJ+cT1So=
31+
golang.org/x/sys v0.0.0-20200302150141-5c8b2ff67527/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
3432
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
3533
gopkg.in/check.v1 v1.0.0-20190902080502-41f04d3bba15 h1:YR8cESwS4TdDjEe65xsg0ogRM/Nc3DYOhEAlW+xobZo=
3634
gopkg.in/check.v1 v1.0.0-20190902080502-41f04d3bba15/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=

headless.go

Lines changed: 19 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2,28 +2,35 @@ package inout
22

33
import (
44
"context"
5+
"fmt"
6+
"strings"
57

8+
"github.com/chromedp/cdproto/cdp"
69
"github.com/chromedp/chromedp"
710
)
811

9-
func waitForDomElement(ctx context.Context, selector, host string, verbose bool) (string, error) {
12+
func waitForDomElement(ctx context.Context, selector, addr string, verbose bool) (string, error) {
1013
// create context
1114
childCtx, cancel := chromedp.NewContext(ctx)
1215
defer cancel()
1316

14-
var content string
15-
err := chromedp.Run(childCtx, visible(host, selector, &content))
16-
if err != nil {
17-
return "", err
17+
// navigate
18+
if err := chromedp.Run(childCtx, chromedp.Navigate(addr)); err != nil {
19+
return "", fmt.Errorf("could not navigate to %s: %v", addr, err)
1820
}
1921

20-
return content, nil
21-
}
22+
var nodes []*cdp.Node
23+
err := chromedp.Run(childCtx, chromedp.Nodes(selector, &nodes, chromedp.ByQueryAll, chromedp.NodeVisible))
24+
if err != nil {
25+
return "", fmt.Errorf("could not get nodes %s: %v", selector, err)
26+
}
2227

23-
func visible(host, selector string, content *string) chromedp.Tasks {
24-
return chromedp.Tasks{
25-
chromedp.Navigate(host),
26-
chromedp.WaitVisible(selector),
27-
chromedp.OuterHTML(selector, content),
28+
var sb strings.Builder
29+
for _, n := range nodes {
30+
var cnt string
31+
if err := chromedp.Run(childCtx, chromedp.OuterHTML(n.FullXPath(), &cnt)); err == nil {
32+
sb.WriteString(cnt)
33+
}
2834
}
35+
return strings.TrimSpace(sb.String()), nil
2936
}

headless_test.go

Lines changed: 16 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -3,58 +3,43 @@ package inout
33
import (
44
"context"
55
"fmt"
6-
"log"
76
"net/http"
7+
"net/http/httptest"
88
"testing"
99

1010
"github.com/stretchr/testify/assert"
1111
)
1212

1313
func Test_waitForDomElement(t *testing.T) {
14-
defer stopServer(startServer(fmt.Sprintf(":%d", port), headlessIndexHTML, 200))
15-
innerContents, err := waitForDomElement(context.TODO(), "div p", fmt.Sprintf("http://localhost:%d", port), false)
14+
ts := startServer(headlessIndexHTML, 200)
15+
defer ts.Close()
16+
innerContents, err := waitForDomElement(context.TODO(), "div p", ts.URL, false)
1617
assert.Nil(t, err)
1718
assert.Equal(t, headlessExpectedHTML, innerContents)
1819
}
1920

2021
// startServer is a simple HTTP server that displays the passed headers in the html.
21-
func startServer(addr, document string, status int) *http.Server {
22+
func startServer(document string, status int) *httptest.Server {
2223
mux := http.NewServeMux()
2324
mux.HandleFunc("/", func(res http.ResponseWriter, _ *http.Request) {
2425
res.WriteHeader(status)
2526
res.Header().Set("Content-Type", "text/html; charset=utf-8")
2627
fmt.Fprint(res, document)
2728
})
28-
srv := &http.Server{Addr: addr, Handler: mux}
29-
go func() {
30-
// returns ErrServerClosed on graceful close
31-
if err := srv.ListenAndServe(); err != http.ErrServerClosed {
32-
log.Fatalf("ListenAndServe(): %s", err)
33-
}
34-
}()
35-
return srv
36-
}
37-
38-
func stopServer(srv *http.Server) {
39-
// close the server gracefully
40-
if err := srv.Shutdown(context.TODO()); err != nil {
41-
panic(err) // failure/timeout shutting down the server gracefully
42-
}
29+
return httptest.NewServer(mux)
4330
}
4431

4532
const (
46-
port = 8655
47-
48-
headlessExpectedHTML = "<p>There was a Boy whose name was Jim;</p>" +
49-
"<p>His Friends were very good to him.</p>" +
50-
"<p>They gave him Tea, and Cakes, and Jam,</p>" +
51-
"<p>And slices of delicious Ham,</p>" +
52-
"<p>And Chocolate with pink inside,</p>" +
53-
"<p>And little Tricycles to ride,</p>" +
54-
"<p>Andread him Stories through and through,</p>" +
55-
"<p>And even took him to the Zoo—</p>" +
56-
"<p>But there it was the dreadful Fate</p>" +
57-
"<p>Befell him, which I now relate.</p>"
33+
headlessExpectedHTML = "<p class=\"line\">There was a Boy whose name was Jim;</p>" +
34+
"<p class=\"line\">His Friends were very good to him.</p>" +
35+
"<p class=\"line\">They gave him Tea, and Cakes, and Jam,</p>" +
36+
"<p class=\"line\">And slices of delicious Ham,</p>" +
37+
"<p class=\"line\">And Chocolate with pink inside,</p>" +
38+
"<p class=\"line\">And little Tricycles to ride,</p>" +
39+
"<p class=\"line\">Andread him Stories through and through,</p>" +
40+
"<p class=\"line\">And even took him to the Zoo—</p>" +
41+
"<p class=\"line\">But there it was the dreadful Fate</p>" +
42+
"<p class=\"line\">Befell him, which I now relate.</p>"
5843

5944
headlessIndexHTML = `<!doctype html>
6045
<html>

reader.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ func NewInOut(ctx context.Context, options ...Option) (Reader, error) {
5151
start := time.Now()
5252
end := start.Add(r.timeout)
5353
go func() {
54-
ticker := time.NewTicker(10 * time.Millisecond)
54+
ticker := time.NewTicker(30 * time.Millisecond)
5555
for {
5656
select {
5757
case <-ctx.Done():

reader_test.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,16 +3,16 @@ package inout
33
import (
44
"bufio"
55
"context"
6-
"fmt"
76
"strings"
87
"testing"
98

109
"github.com/stretchr/testify/assert"
1110
)
1211

1312
func Test_handleHTTP(t *testing.T) {
14-
defer stopServer(startServer(fmt.Sprintf(":%d", port), readerIndexHTML, 200))
15-
reader, err := handleHTTP(context.TODO(), fmt.Sprintf("http://localhost:%d", port), "", false)
13+
ts := startServer(readerIndexHTML, 200)
14+
defer ts.Close()
15+
reader, err := handleHTTP(context.TODO(), ts.URL, "", false)
1616
assert.Nil(t, err)
1717
assert.NotNil(t, reader)
1818

0 commit comments

Comments
 (0)