Skip to content

Commit

Permalink
add new plugin
Browse files Browse the repository at this point in the history
  • Loading branch information
deancn committed Sep 7, 2021
1 parent 8a7ae97 commit 85c6fed
Show file tree
Hide file tree
Showing 5 changed files with 111 additions and 91 deletions.
11 changes: 2 additions & 9 deletions Makefile
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
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
# go-browserstack
# Go automate for BrowserStack

https://automate.browserstack.com/
88 changes: 7 additions & 81 deletions main.go
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)
}
87 changes: 87 additions & 0 deletions plugin/play_golang.go
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.
}
13 changes: 13 additions & 0 deletions plugin/visit_website.go
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!")
}

0 comments on commit 85c6fed

Please sign in to comment.