Skip to content

Commit

Permalink
Code cleanup
Browse files Browse the repository at this point in the history
- Refactored API calls to be cleaner
- Changed types that shoudn't be exported to not-exported
- Updated examples with API changes
- Added unit test for Title action
  • Loading branch information
Kenneth Shaw committed Feb 12, 2017
1 parent f73c429 commit 3673164
Show file tree
Hide file tree
Showing 11 changed files with 285 additions and 318 deletions.
12 changes: 10 additions & 2 deletions chromedp_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,9 +54,17 @@ func TestNavigate(t *testing.T) {
if err != nil {
t.Fatal(err)
}

if !strings.HasPrefix(urlstr, "https://www.google.") {
t.Errorf("expected to be on google, got: %v", urlstr)
t.Errorf("expected to be on google domain, at: %s", urlstr)
}

var title string
err = c.Run(defaultContext, Title(&title))
if err != nil {
t.Fatal(err)
}
if !strings.Contains(strings.ToLower(title), "google") {
t.Errorf("expected title to contain google, instead title is: %s", title)
}
}

Expand Down
39 changes: 39 additions & 0 deletions errors.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package chromedp

import (
"errors"
)

// Error types.
var (
// ErrInvalidDimensions is the error returned when the retrieved box model is
// invalid.
ErrInvalidDimensions = errors.New("invalid dimensions")

// ErrNoResults is the error returned when there are no matching nodes.
ErrNoResults = errors.New("no results")

// ErrHasResults is the error returned when there should not be any
// matching nodes.
ErrHasResults = errors.New("has results")

// ErrNotVisible is the error returned when a non-visible node should be
// visible.
ErrNotVisible = errors.New("not visible")

// ErrVisible is the error returned when a visible node should be
// non-visible.
ErrVisible = errors.New("visible")

// ErrDisabled is the error returned when a disabled node should be
// enabled.
ErrDisabled = errors.New("disabled")

// ErrNotSelected is the error returned when a non-selected node should be
// selected.
ErrNotSelected = errors.New("not selected")

// ErrInvalidBoxModel is the error returned when the retrieved box model
// data is invalid.
ErrInvalidBoxModel = errors.New("invalid box model")
)
2 changes: 1 addition & 1 deletion examples/click/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ func click() cdp.Tasks {
return cdp.Tasks{
cdp.Navigate(`https://golang.org/pkg/time/`),
cdp.WaitVisible(`#footer`),
cdp.Click(`#pkg-overview`, cdp.ElementVisible),
cdp.Click(`#pkg-overview`, cdp.NodeVisible),
cdp.Sleep(150 * time.Second),
}
}
2 changes: 1 addition & 1 deletion examples/text/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,6 @@ func main() {
func text(res *string) cdp.Tasks {
return cdp.Tasks{
cdp.Navigate(`https://golang.org/pkg/time/`),
cdp.Text(`#pkg-overview`, res, cdp.ElementVisible, cdp.ByID),
cdp.Text(`#pkg-overview`, res, cdp.NodeVisible, cdp.ByID),
}
}
4 changes: 2 additions & 2 deletions handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -504,7 +504,7 @@ func (h *TargetHandler) pageEvent(ctxt context.Context, ev interface{}) {
defer h.pageWaitGroup.Done()

var id cdp.FrameID
var op FrameOp
var op frameOp

switch e := ev.(type) {
case *page.EventFrameNavigated:
Expand Down Expand Up @@ -569,7 +569,7 @@ func (h *TargetHandler) domEvent(ctxt context.Context, ev interface{}) {
}

var id cdp.NodeID
var op NodeOp
var op nodeOp

switch e := ev.(type) {
case *dom.EventSetChildNodes:
Expand Down
6 changes: 0 additions & 6 deletions input.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package chromedp

import (
"context"
"errors"
"time"

"github.com/knq/chromedp/cdp"
Expand All @@ -11,11 +10,6 @@ import (
"github.com/knq/chromedp/kb"
)

// Error types.
var (
ErrInvalidDimensions = errors.New("invalid box dimensions")
)

// MouseAction is a mouse action.
func MouseAction(typ input.MouseType, x, y int64, opts ...MouseOption) Action {
me := input.DispatchMouseEvent(typ, x, y)
Expand Down
28 changes: 21 additions & 7 deletions nav.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,16 @@ func NavigateForward(ctxt context.Context, h cdp.Handler) error {
return page.NavigateToHistoryEntry(entries[i+1].ID).Do(ctxt, h)
}

// Stop stops all navigation and pending resource retrieval.
func Stop() Action {
return page.StopLoading()
}

// Reload reloads the current page.
func Reload() Action {
return page.Reload()
}

// CaptureScreenshot captures takes a full page screenshot.
func CaptureScreenshot(res *[]byte) Action {
if res == nil {
Expand Down Expand Up @@ -113,16 +123,20 @@ func RemoveOnLoadScript(id page.ScriptIdentifier) Action {
return page.RemoveScriptToEvaluateOnLoad(id)
}

// Stop stops all navigation and pending resource retrieval.
func Stop() Action {
return page.StopLoading()
}

// Location retrieves the URL location.
// Location retrieves the document location.
func Location(urlstr *string) Action {
if urlstr == nil {
panic("urlstr cannot be nil")
}

return EvaluateAsDevTools(`location.toString()`, urlstr)
return EvaluateAsDevTools(`document.location.toString()`, urlstr)
}

// Title retrieves the document title.
func Title(title *string) Action {
if title == nil {
panic("title cannot be nil")
}

return EvaluateAsDevTools(`document.title`, title)
}
12 changes: 2 additions & 10 deletions pool.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,6 @@ import (
"github.com/knq/chromedp/runner"
)

const (
// DefaultStartPort is the default start port number.
DefaultStartPort = 9000

// DefaultEndPort is the default end port number.
DefaultEndPort = 10000
)

// Pool manages a pool of running Chrome processes.
type Pool struct {
// start is the start port.
Expand All @@ -35,8 +27,8 @@ func NewPool(opts ...PoolOption) (*Pool, error) {
var err error

p := &Pool{
start: DefaultStartPort,
end: DefaultEndPort,
start: DefaultPoolStartPort,
end: DefaultPoolEndPort,
res: make(map[int]*Res),
}

Expand Down
Loading

0 comments on commit 3673164

Please sign in to comment.