Skip to content

Commit

Permalink
use Must
Browse files Browse the repository at this point in the history
resolve #116
  • Loading branch information
ysmood committed Aug 4, 2020
1 parent 899b00a commit 1416ddf
Show file tree
Hide file tree
Showing 17 changed files with 36 additions and 49 deletions.
4 changes: 2 additions & 2 deletions browser.go
Original file line number Diff line number Diff line change
Expand Up @@ -163,13 +163,13 @@ func (b *Browser) ConnectE() (err error) {
b.client = launcher.NewRemote(u).Client()
} else {
if u == "" {
u = launcher.New().Context(b.ctx).Launch()
u = launcher.New().Context(b.ctx).MustLaunch()
}
b.client = cdp.New(u)
}
}

b.client.Context(b.ctx, b.ctxCancel).Connect()
b.client.Context(b.ctx, b.ctxCancel).MustConnect()

b.monitorServer = b.ServeMonitor(defaults.Monitor, !defaults.Blind)

Expand Down
4 changes: 2 additions & 2 deletions examples_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ func Example_headless_with_debug() {
url := launcher.New().
Headless(false).
Devtools(true).
Launch()
MustLaunch()

// Trace shows verbose debug information for each action executed
// Slowmotion is a debug related function that waits 2 seconds between
Expand Down Expand Up @@ -226,7 +226,7 @@ func Example_customize_browser_launch() {
Set("proxy-server", "127.0.0.1:8080").
// Delete a flag- remove the mock-keychain flag
Delete("use-mock-keychain").
Launch()
MustLaunch()

browser := rod.New().ControlURL(url).Connect()
defer browser.Close()
Expand Down
2 changes: 1 addition & 1 deletion fixtures/gen-fonts/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import (
)

func main() {
url := launcher.New().Headless(true).Launch()
url := launcher.New().Headless(true).MustLaunch()
b := rod.New().ControlURL(url).Connect()
defer b.Close()

Expand Down
4 changes: 2 additions & 2 deletions lib/cdp/example_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,10 @@ import (

func ExampleClient() {
// launch a browser
url := launcher.New().Headless(false).Launch()
url := launcher.New().Headless(false).MustLaunch()

// create a controller
client := cdp.New(url).Connect()
client := cdp.New(url).MustConnect()

// Such as call this endpoint on the api doc:
// https://chromedevtools.github.io/devtools-protocol/tot/Page#method-navigate
Expand Down
10 changes: 5 additions & 5 deletions lib/cdp/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -125,8 +125,8 @@ func (cdp *Client) DebugLog(fn func(interface{})) *Client {
return cdp
}

// ConnectE to browser
func (cdp *Client) ConnectE() error {
// Connect to browser
func (cdp *Client) Connect() error {
if cdp.ws == nil {
cdp.ws = NewDefaultWsClient()
}
Expand All @@ -145,9 +145,9 @@ func (cdp *Client) ConnectE() error {
return nil
}

// Connect to browser
func (cdp *Client) Connect() *Client {
utils.E(cdp.ConnectE())
// MustConnect to browser
func (cdp *Client) MustConnect() *Client {
utils.E(cdp.Connect())
return cdp
}

Expand Down
8 changes: 4 additions & 4 deletions lib/cdp/main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,9 @@ func TestBasic(t *testing.T) {
ctx, done := context.WithCancel(context.Background())
defer done()

url := launcher.New().Launch()
url := launcher.New().MustLaunch()

client := cdp.New(url).Context(ctx, done).Websocket(nil).Header(http.Header{"test": {}}).Connect()
client := cdp.New(url).Context(ctx, done).Websocket(nil).Header(http.Header{"test": {}}).MustConnect()

defer func() {
utils.E(client.Call(ctx, "", "Browser.close", nil))
Expand Down Expand Up @@ -119,15 +119,15 @@ func TestError(t *testing.T) {
assert.Equal(t, "{\"code\":10,\"message\":\"err\",\"data\":\"data\"}", cdpErr.Error())

assert.Panics(t, func() {
cdp.New("").Connect()
cdp.New("").MustConnect()
})
}

func TestCrash(t *testing.T) {
ctx := context.Background()
l := launcher.New()

client := cdp.New(l.Launch()).Debug(true).Connect()
client := cdp.New(l.MustLaunch()).Debug(true).MustConnect()

go func() {
for range client.Event() {
Expand Down
2 changes: 1 addition & 1 deletion lib/devices/utils_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import (
"github.com/stretchr/testify/assert"
)

func TestE(t *testing.T) {
func TestErr(t *testing.T) {
v := devices.GetViewport(devices.IPad, false)
assert.EqualValues(t, 768, v.Width)
assert.EqualValues(t, 1024, v.Height)
Expand Down
2 changes: 1 addition & 1 deletion lib/examples/use-rod-like-chrome-extension/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import (
func main() {
// Make sure you have closed your browser completely, UserMode can't control a browser that is not launched by it.
// Launches a new browser with the "new user mode" option, and returns the URL to control that browser.
url := launcher.NewUserMode().Launch()
url := launcher.NewUserMode().MustLaunch()

browser := rod.New().ControlURL(url).Connect()

Expand Down
14 changes: 7 additions & 7 deletions lib/launcher/basic_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ func TestLaunch(t *testing.T) {
_ = kit.KillTree(l.PID())
}()

url := l.Launch()
url := l.MustLaunch()

assert.NotEmpty(t, url)
}
Expand All @@ -71,11 +71,11 @@ func TestLaunchUserMode(t *testing.T) {
Headless(false).Headless(true).RemoteDebuggingPort(port).
Devtools(true).Devtools(false).Reap(true).
UserDataDir("test").UserDataDir(dir).
Launch()
MustLaunch()

assert.Equal(t,
url,
launcher.NewUserMode().RemoteDebuggingPort(port).Launch(),
launcher.NewUserMode().RemoteDebuggingPort(port).MustLaunch(),
)
}

Expand All @@ -84,10 +84,10 @@ func TestOpen(t *testing.T) {
}

func TestUserModeErr(t *testing.T) {
_, err := launcher.NewUserMode().RemoteDebuggingPort(48277).Bin("not-exists").LaunchE()
_, err := launcher.NewUserMode().RemoteDebuggingPort(48277).Bin("not-exists").Launch()
assert.Error(t, err)

_, err = launcher.NewUserMode().RemoteDebuggingPort(58217).Bin("echo").LaunchE()
_, err = launcher.NewUserMode().RemoteDebuggingPort(58217).Bin("echo").Launch()
assert.Error(t, err)
}

Expand All @@ -98,9 +98,9 @@ func TestGetWebSocketDebuggerURLErr(t *testing.T) {

func TestLaunchErr(t *testing.T) {
assert.Panics(t, func() {
launcher.New().Bin("not-exists").Launch()
launcher.New().Bin("not-exists").MustLaunch()
})
assert.Panics(t, func() {
launcher.New().Headless(false).Bin("not-exists").Launch()
launcher.New().Headless(false).Bin("not-exists").MustLaunch()
})
}
10 changes: 5 additions & 5 deletions lib/launcher/launcher.go
Original file line number Diff line number Diff line change
Expand Up @@ -245,17 +245,17 @@ func (l *Launcher) Reap(enable bool) *Launcher {
return l
}

// Launch a standalone temp browser instance and returns the debug url.
// MustLaunch a standalone temp browser instance and returns the debug url.
// bin and profileDir are optional, set them to empty to use the default values.
// If you want to reuse sessions, such as cookies, set the userDataDir to the same location.
func (l *Launcher) Launch() string {
u, err := l.LaunchE()
func (l *Launcher) MustLaunch() string {
u, err := l.Launch()
utils.E(err)
return u
}

// LaunchE doc is similar to the method Launch
func (l *Launcher) LaunchE() (wsURL string, err error) {
// Launch doc is similar to the method Launch
func (l *Launcher) Launch() (wsURL string, err error) {
defer func() {
if e := recover(); e != nil {
err = e.(error)
Expand Down
4 changes: 2 additions & 2 deletions lib/launcher/private_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ func TestRemoteLaunch(t *testing.T) {
u := "ws://" + srv.Listener.Addr().String()
l := NewRemote(u).KeepUserDataDir()
client := l.Delete("keep-user-data-dir").Client()
b := client.Context(ctx, cancel).Connect()
b := client.Context(ctx, cancel).MustConnect()
utils.E(b.Call(ctx, "", "Browser.getVersion", nil))
_, _ = b.Call(ctx, "", "Browser.close", nil)
dir, _ := l.Get("user-data-dir")
Expand All @@ -96,6 +96,6 @@ func TestLaunchErr(t *testing.T) {
go func() {
l.exit <- utils.Nil{}
}()
_, err := l.LaunchE()
_, err := l.Launch()
assert.Error(t, err)
}
2 changes: 1 addition & 1 deletion lib/launcher/proxy.go
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ func (p *Proxy) launch(w http.ResponseWriter, r *http.Request) {
utils.E(json.Unmarshal([]byte(options), l))
}

u := l.Launch()
u := l.MustLaunch()
defer func() {
proc, err := os.FindProcess(l.PID())
l.kill()
Expand Down
2 changes: 1 addition & 1 deletion lib/proto/generate/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ func getSchema() gjson.Result {
}
}()

u := l.Launch()
u := l.MustLaunch()
parsed, err := url.Parse(u)
utils.E(err)
parsed.Scheme = "http"
Expand Down
7 changes: 0 additions & 7 deletions lib/proto/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,13 +85,6 @@ func Normalize(m interface{}) (json.RawMessage, error) {
return json.Marshal(m)
}

// E panics err if err not nil
func E(err error) {
if err != nil {
panic(err)
}
}

// JSON value
type JSON struct {
gjson.Result
Expand Down
6 changes: 0 additions & 6 deletions lib/proto/utils_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,12 +58,6 @@ func TestCall(t *testing.T) {
assert.Error(t, err)
}

func TestE(t *testing.T) {
assert.Panics(t, func() {
proto.E(errors.New("err"))
})
}

func TestParseMethodName(t *testing.T) {
d, n := proto.ParseMethodName("Page.enable")
assert.Equal(t, "Page", d)
Expand Down
2 changes: 1 addition & 1 deletion lib/utils/main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import (
"github.com/stretchr/testify/assert"
)

func TestE(t *testing.T) {
func TestErr(t *testing.T) {
utils.E(nil)

assert.Panics(t, func() {
Expand Down
2 changes: 1 addition & 1 deletion setup_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ func Test(t *testing.T) {
u := launcher.New().
Delete("disable-extensions").
Set("load-extension", extPath).
Launch()
MustLaunch()

s := new(S)
s.client = cdp.New(u)
Expand Down

0 comments on commit 1416ddf

Please sign in to comment.