Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions phantom.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,13 +27,13 @@ Create a new `Phantomjs` instance and return it as a pointer.

If an error occurs during command start, return it instead.
*/
func Start(args ...string) (*Phantom, error) {
func Start(cmdPath string, args ...string) (*Phantom, error) {
if nbInstance == 0 {
wrapperFileName, _ = createWrapperFile()
}
nbInstance += 1
args = append(args, wrapperFileName)
cmd := exec.Command("phantomjs", args...)
cmd := exec.Command(cmdPath, args...)

inPipe, err := cmd.StdinPipe()
if err != nil {
Expand Down
4 changes: 2 additions & 2 deletions phantom_example_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import (
)

func ExampleWithResult() {
p, err := Start()
p, err := Start("phantomjs")
if err != nil {
panic(err)
}
Expand All @@ -24,7 +24,7 @@ func ExampleWithResult() {
}

func ExampleWithError() {
p, err := Start()
p, err := Start("phantomjs")
if err != nil {
panic(err)
}
Expand Down
22 changes: 11 additions & 11 deletions phantom_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,53 +5,53 @@ import (
)

func TestStartStop(t *testing.T) {
p, err := Start()
p, err := Start("phantomjs")
failOnError(err, t)
err = p.Exit()
failOnError(err, t)
}

func TestStartStopWithArgs(t *testing.T) {
p, err := Start("--web-security=no")
p, err := Start("phantomjs", "--web-security=no")
failOnError(err, t)
err = p.Exit()
failOnError(err, t)
}

func TestRunACommand(t *testing.T) {
p, err := Start()
p, err := Start("phantomjs")
defer p.Exit()
failOnError(err, t)
assertFloatResult("function(){ return 2 + 1; }\n", 3, p, t)
}

func TestRunACommandWithoutLineBreak(t *testing.T) {
p, err := Start()
p, err := Start("phantomjs")
defer p.Exit()
failOnError(err, t)
assertFloatResult("function(){ return 2 + 2; }", 4, p, t)
}

func TestRunAnAsyncCommand(t *testing.T) {
p, err := Start()
p, err := Start("phantomjs")
failOnError(err, t)
defer p.Exit()
assertFloatResult("function(done){ done(2 + 3) ; }\n", 5, p, t)
p1, err := Start()
p1, err := Start("phantomjs")
failOnError(err, t)
defer p1.Exit()
assertFloatResult("function(done){ setTimeout(function() { done(3 + 3) ; }, 0); }\n", 6, p1, t)
}

func TestRunMultilineCommand(t *testing.T) {
p, err := Start()
p, err := Start("phantomjs")
failOnError(err, t)
defer p.Exit()
assertFloatResult("function() {\n\t return 3+4;\n}\n", 7, p, t)
}

func TestRunMultipleCommands(t *testing.T) {
p, err := Start()
p, err := Start("phantomjs")
failOnError(err, t)
defer p.Exit()
assertFloatResult("function() {return 1}", 1, p, t)
Expand All @@ -60,22 +60,22 @@ func TestRunMultipleCommands(t *testing.T) {
}

func TestLoadGlobal(t *testing.T) {
p, err := Start()
p, err := Start("phantomjs")
failOnError(err, t)
defer p.Exit()
p.Load("function result(result) { return result; }\nvar a = 2")
assertFloatResult("function() {return result(a);}", 2, p, t)
}

func TestMessageSentAfterAnErrorDontCrash(t *testing.T) {
p, err := Start()
p, err := Start("phantomjs")
failOnError(err, t)
defer p.Exit()
p.Run("function(done) {done(null, 'manual'); done('should not panic');}", nil)
}

func TestDoubleErrorSendDontCrash(t *testing.T) {
p, err := Start()
p, err := Start("phantomjs")
failOnError(err, t)
defer p.Exit()
p.Run("function(done) {done(null, 'manual'); done(null, 'should not panic');}", nil)
Expand Down