From 85c6fed375ffc61888c7283fb7e801b9d133e84a Mon Sep 17 00:00:00 2001 From: deancn Date: Wed, 8 Sep 2021 01:23:40 +0800 Subject: [PATCH] add new plugin --- Makefile | 11 +----- README.md | 3 +- main.go | 88 ++++------------------------------------- plugin/play_golang.go | 87 ++++++++++++++++++++++++++++++++++++++++ plugin/visit_website.go | 13 ++++++ 5 files changed, 111 insertions(+), 91 deletions(-) create mode 100644 plugin/play_golang.go create mode 100644 plugin/visit_website.go diff --git a/Makefile b/Makefile index 75e6945..46f5cdf 100644 --- a/Makefile +++ b/Makefile @@ -1,9 +1,2 @@ -run: setenv - go run main.go - -setenv: - @echo export GOPATH=`pwd` - @echo export GOBIN=`pwd`/bin - -indent: - gofmt -w main.go +run: + go run main.go \ No newline at end of file diff --git a/README.md b/README.md index 23980f1..639a500 100644 --- a/README.md +++ b/README.md @@ -1,2 +1,3 @@ -# go-browserstack +# Go automate for BrowserStack + https://automate.browserstack.com/ diff --git a/main.go b/main.go index d20480a..83692de 100644 --- a/main.go +++ b/main.go @@ -1,108 +1,34 @@ -/* - * Go automate for BrowserStack - * taken from sourcegraph go-selenium examples - */ - package main import ( "fmt" "log" "os" - "strings" - "time" + + "github.com/airdb/go-browserstack/plugin" "github.com/joho/godotenv" "github.com/tebeka/selenium" ) -// This example shows how to navigate to a http://play.golang.org page, input a -// short program, run it, and inspect its output. -// -// If you want to actually run this example: -// -// 1. Ensure the file paths at the top of the function are correct. -// 2. Remove the word "Example" from the comment at the bottom of the -// function. -// 3. Run: -// go test -test.run=Example$ github.com/tebeka/selenium func main() { err := godotenv.Load() if err != nil { log.Fatal("Error loading .env file") } - // Start a Selenium WebDriver server instance (if one is not already - // running). - // Connect to the WebDriver instance running locally. - caps := selenium.Capabilities{"browserName": "firefox"} + browserstackUsername := os.Getenv("BROWSERSTACK_USERNAME") browserstackAccessKey := os.Getenv("BROWSERSTACK_ACCESS_KEY") + browserstackURL := os.Getenv("BROWSERSTACK_URL") remoteUrl := fmt.Sprintf("https://%s:%s@hub-cloud.browserstack.com/wd/hub", browserstackUsername, browserstackAccessKey) + + caps := selenium.Capabilities{"browserName": "firefox"} wd, err := selenium.NewRemote(caps, remoteUrl) if err != nil { panic(err) } defer wd.Quit() - // Navigate to the simple playground interface. - if err := wd.Get("http://play.golang.org/?simple=1"); err != nil { - panic(err) - } - - // Get a reference to the text box containing code. - elem, err := wd.FindElement(selenium.ByCSSSelector, "#code") - if err != nil { - panic(err) - } - // Remove the boilerplate code already in the text box. - if err := elem.Clear(); err != nil { - panic(err) - } - - // Enter some new code in text box. - err = elem.SendKeys(` - package main - import "fmt" - func main() { - fmt.Println("Hello WebDriver!") - } - `) - if err != nil { - panic(err) - } - - // Click the run button. - btn, err := wd.FindElement(selenium.ByCSSSelector, "#run") - if err != nil { - panic(err) - } - if err := btn.Click(); err != nil { - panic(err) - } - - // Wait for the program to finish running and get the output. - outputDiv, err := wd.FindElement(selenium.ByCSSSelector, "#output") - if err != nil { - panic(err) - } - - var output string - for { - output, err = outputDiv.Text() - if err != nil { - panic(err) - } - if output != "Waiting for remote server..." { - break - } - time.Sleep(time.Millisecond * 100) - } - - fmt.Printf("%s", strings.Replace(output, "\n\n", "\n", -1)) - - // Example Output: - // Hello WebDriver! - // - // Program exited. + plugin.Visit(wd, browserstackURL) } diff --git a/plugin/play_golang.go b/plugin/play_golang.go new file mode 100644 index 0000000..2eb7e43 --- /dev/null +++ b/plugin/play_golang.go @@ -0,0 +1,87 @@ +/* + * Go automate for BrowserStack + * taken from sourcegraph go-selenium examples + */ + +package plugin + +import ( + "fmt" + "strings" + "time" + + "github.com/tebeka/selenium" +) + +// This example shows how to navigate to a http://play.golang.org page, input a +// short program, run it, and inspect its output. +// +// If you want to actually run this example: +// +// 1. Ensure the file paths at the top of the function are correct. +// 2. Remove the word "Example" from the comment at the bottom of the +// function. +// 3. Run: +// go test -test.run=Example$ github.com/tebeka/selenium +func golangPlay(wd selenium.WebDriver) { + // Navigate to the simple playground interface. + if err := wd.Get("http://play.golang.org/?simple=1"); err != nil { + panic(err) + } + + // Get a reference to the text box containing code. + elem, err := wd.FindElement(selenium.ByCSSSelector, "#code") + if err != nil { + panic(err) + } + // Remove the boilerplate code already in the text box. + if err := elem.Clear(); err != nil { + panic(err) + } + + // Enter some new code in text box. + err = elem.SendKeys(` + package main + import "fmt" + func main() { + fmt.Println("Hello WebDriver!") + } + `) + if err != nil { + panic(err) + } + + // Click the run button. + btn, err := wd.FindElement(selenium.ByCSSSelector, "#run") + if err != nil { + panic(err) + } + if err := btn.Click(); err != nil { + panic(err) + } + + // Wait for the program to finish running and get the output. + outputDiv, err := wd.FindElement(selenium.ByCSSSelector, "#output") + if err != nil { + panic(err) + } + + var output string + for { + output, err = outputDiv.Text() + if err != nil { + panic(err) + } + if output != "Waiting for remote server..." { + break + } + time.Sleep(time.Millisecond * 100) + } + + fmt.Printf("%s", strings.Replace(output, "\n\n", "\n", -1)) + + // Example Output: + // Hello WebDriver! + // + // Program exited. +} diff --git a/plugin/visit_website.go b/plugin/visit_website.go new file mode 100644 index 0000000..cfabcda --- /dev/null +++ b/plugin/visit_website.go @@ -0,0 +1,13 @@ +package plugin + +import ( + "log" + + "github.com/tebeka/selenium" +) + +func Visit(wd selenium.WebDriver, url string) { + log.Println("start visit website...") + wd.Get("https://www.baidu.com") + log.Println("visit website finished!") +}