GithubHelp home page GithubHelp logo

pombredanne / go-selenium Goto Github PK

View Code? Open in Web Editor NEW

This project forked from sourcegraph/go-selenium

0.0 1.0 0.0 172 KB

Selenium WebDriver client for Go

Home Page: https://sourcegraph.com/github.com/sourcegraph/go-selenium

License: Other

go-selenium's Introduction

============================================== go-selenium - Selenium WebDriver client for Go

xrefs funcs top func

go-selenium is a Selenium WebDriver client for Go.

Note: the public API is experimental and subject to change until further notice.

Usage

Documentation: go-selenium on Sourcegraph.

Example: example_test.go:

package selenium_test

import (
	"fmt"
	"github.com/sourcegraph/go-selenium"
)

func ExampleFindElement() {
	var webDriver selenium.WebDriver
	var err error
	caps := selenium.Capabilities(map[string]interface{}{"browserName": "firefox"})
	if webDriver, err = selenium.NewRemote(caps, "http://localhost:4444/wd/hub"); err != nil {
		fmt.Printf("Failed to open session: %s\n", err)
		return
	}
	defer webDriver.Quit()

	err = webDriver.Get("https://github.com/sourcegraph/go-selenium")
	if err != nil {
		fmt.Printf("Failed to load page: %s\n", err)
		return
	}

	if title, err := webDriver.Title(); err == nil {
		fmt.Printf("Page title: %s\n", title)
	} else {
		fmt.Printf("Failed to get page title: %s", err)
		return
	}

	var elem selenium.WebElement
	elem, err = webDriver.FindElement(selenium.ByCSSSelector, ".js-current-repository")
	if err != nil {
		fmt.Printf("Failed to find element: %s\n", err)
		return
	}

	if text, err := elem.Text(); err == nil {
		fmt.Printf("Repository: %s\n", text)
	} else {
		fmt.Printf("Failed to get text of element: %s\n", err)
		return
	}

	// output:
	// Page title: sourcegraph/go-selenium · GitHub
	// Repository: go-selenium
}

The WebDriverT and WebElementT interfaces make test code cleaner. Each method in WebDriver and WebElement has a corresponding method in the *T interfaces that omits the error from the return values and instead calls t.Fatalf upon encountering an error. For example:

package mytest

import (
  "github.com/sourcegraph/go-selenium"
  "testing"
)

var caps selenium.Capabilities
var executorURL = "http://localhost:4444/wd/hub"

// An example test using the WebDriverT and WebElementT interfaces. If you use the non-*T
// interfaces, you must perform error checking that is tangential to what you are testing,
// and you have to destructure results from method calls.
func TestWithT(t *testing.T) {
  wd, _ := selenium.NewRemote(caps, executor)

  // Call .T(t) to obtain a WebDriverT from a WebDriver (or to obtain a WebElementT from
  // a WebElement).
  wdt := wd.T(t)

  // Calls `t.Fatalf("Get: %s", err)` upon failure.
  wdt.Get("http://example.com")

  // Calls `t.Fatalf("FindElement(by=%q, value=%q): %s", by, value, err)` upon failure.
  elem := wdt.FindElement(selenium.ByCSSSelector, ".foo")

  // Calls `t.Fatalf("Text: %s", err)` if the `.Text()` call fails.
  if elem.Text() != "bar" {
    t.Fatalf("want elem text %q, got %q", "bar", elem.Text())
  }
}

See remote_test.go for more usage examples.

Running tests

Start Selenium WebDriver and run go test. To see all available options, run go test -test.h.

TODO

  • Support Firefox profiles

Contributors

License

go-selenium is distributed under the Eclipse Public License.

go-selenium's People

Contributors

sqs avatar tebeka avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.