Skip to content

Instantly share code, notes, and snippets.

@AndreKR
Created March 15, 2017 16:00
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save AndreKR/213996c501ed9da927ede1ff7d23699c to your computer and use it in GitHub Desktop.
Save AndreKR/213996c501ed9da927ede1ff7d23699c to your computer and use it in GitHub Desktop.
package main
import (
agwd "github.com/sclevine/agouti"
tbwd "github.com/tebeka/selenium"
"log"
sgwd "sourcegraph.com/sourcegraph/go-selenium"
)
// Helper for sourcegraph/go-selenium
type fatalfer struct{}
func (fatalfer) Fatalf(fmt string, v ...interface{}) {
panic([]interface{}{fmt, v})
}
var errorhandler fatalfer
// Helper for tebeka/selenium and agouti
func checkError(err error) {
if err != nil {
panic(err)
}
}
func main() {
defer func() {
r := recover()
// Catch the errors from the tebeka/selenium and agouti helpers
err, is_error := r.(error)
if is_error {
log.Fatal(err)
}
// Catch the errors from the sourcegraph/go-selenium helper
arr, is_array := r.([]interface{})
if is_array {
fmt := arr[0].(string)
v := arr[1].([]interface{})
log.Fatalf(fmt, v...)
}
}()
//sourcegraphExample()
//tebekaExample()
//agoutiExample()
}
func sourcegraphExample() {
// Pro: .T
// Con: Doesn't report the full error
var err error
wdRaw, err := sgwd.NewRemote(sgwd.Capabilities(map[string]interface{}{"browserName": "chrome"}), "http://localhost:4444/wd/hub")
checkError(err)
driver := wdRaw.T(errorhandler)
defer driver.Quit()
// Make sure the debug panel doesn't overlay the login
driver.ResizeWindow(driver.CurrentWindowHandle(), sgwd.Size{1200, 500})
driver.Get("http://localhost/shop")
driver.FindElement(sgwd.ByCSSSelector, "header.header li.login a").Click()
mailInput := driver.FindElement(sgwd.ByCSSSelector, "section.login-area input[name=email_address]")
mailInput.Click()
mailInput.SendKeys("info@example.com")
passwordInput := driver.FindElement(sgwd.ByCSSSelector, "section.login-area input[name=password]")
passwordInput.Click()
passwordInput.SendKeys("xxx" + sgwd.EnterKey)
// go-selenium doesn't seem to have explicit wait
driver.SetImplicitWaitTimeout(30000)
// Wait for login to complete (either the "My Account" button or the confirm address popup appears)
driver.FindElement(sgwd.ByCSSSelector, "header.header li.account a, a[data-widget=confirm_address_link]")
// Go to "My Account" page
driver.FindElement(sgwd.ByCSSSelector, "header.header li.account a").Click()
}
func tebekaExample() {
// Pro: Easier capabilities, provides full error info
// Con: No .T
var err error
driver, err := tbwd.NewRemote(tbwd.Capabilities{"browserName": "chrome"}, "http://localhost:4444/wd/hub")
checkError(err)
defer driver.Quit()
// Make sure the debug panel doesn't overlay the login
cwh, err := driver.CurrentWindowHandle()
checkError(err)
err = driver.ResizeWindow(cwh, 1200, 500)
checkError(err)
err = driver.Get("http://localhost/shop")
checkError(err)
loginButton, err := driver.FindElement(tbwd.ByCSSSelector, "header.header li.login a")
checkError(err)
err = loginButton.Click()
mailInput, err := driver.FindElement(tbwd.ByCSSSelector, "section.login-area input[name=email_address]")
checkError(err)
err = mailInput.Click()
checkError(err)
err = mailInput.SendKeys("info@example.com")
checkError(err)
passwordInput, err := driver.FindElement(tbwd.ByCSSSelector, "section.login-area input[name=password]")
checkError(err)
err = passwordInput.SendKeys("xxx" + tbwd.EnterKey)
checkError(err)
// tebeka/selenium doesn't seem to have explicit wait
err = driver.SetImplicitWaitTimeout(30000)
checkError(err)
// Wait for login to complete (either the "My Account" button or the confirm address popup appears)
driver.FindElement(sgwd.ByCSSSelector, "header.header li.account a, a[data-widget=confirm_address_link]")
// Go to "My Account" page
accountButton, err := driver.FindElement(sgwd.ByCSSSelector, "header.header li.account a")
checkError(err)
err = accountButton.Click()
checkError(err)
}
func agoutiExample() {
var err error
driver, err := agwd.NewPage("http://localhost:4444/wd/hub", agwd.Browser("chrome"))
checkError(err)
defer driver.Destroy()
// Make sure the debug panel doesn't overlay the login
err = driver.Size(1200, 500)
checkError(err)
err = driver.Navigate("http://localhost/shop")
checkError(err)
err = driver.Find("header.header li.login a").Click()
checkError(err)
mailInput := driver.Find("section.login-area input[name=email_address]")
err = mailInput.Click()
checkError(err)
err = mailInput.SendKeys("info@example.com")
checkError(err)
passwordInput := driver.Find("section.login-area input[name=password]")
err = passwordInput.SendKeys("xxx" + string('\ue007'))
checkError(err)
// agoudi doesn't seem to have explicit wait
err = driver.SetImplicitWait(30000)
checkError(err)
// Wait for login to complete (either the "My Account" button or the confirm address popup appears)
driver.Find("header.header li.account a, a[data-widget=confirm_address_link]")
// Go to "My Account" page
err = driver.Find("header.header li.account a").Click()
checkError(err)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment