-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
111 additions
and
91 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,3 @@ | ||
# go-browserstack | ||
# Go automate for BrowserStack | ||
|
||
https://automate.browserstack.com/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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:%[email protected]/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) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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. | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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!") | ||
} |