sequence

package module
v0.0.7 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jul 28, 2018 License: MIT Imports: 11 Imported by: 0

README

sequence

Sequence is a frontend for Webdriver testing. It's main goal are to be easy to use and to make writing frontend tests easier in Go.

Overview

A sequence is a chain of functions that test a web page. If any item in the sequence fails, it drops out early and returns the error. Compare the test example from the Go Selenium webdriver to Sequence:

err = sequence.Start(wd).
    Get("http://play.golang.org/?simple=1").
    Find("#code").Clear().SendKeys(`
        package main
        import "fmt"

        func main() {
            fmt.Println("Hello WebDriver!\n")
        }
    `).
    Find("#run").Click().
    Find("#output").Text().Contains("Hello WebDriver").Eventually().
    End()
if err != nil {
    panic(err)
}

The underlying WebDriver library does all the work. Sequence simply presents a easier API to work with.

See the documentation for more details.

Documentation

Overview

Example

This is a Sequence of the same example from https://github.com/tebeka/selenium/blob/master/example_test.go This example shows how to navigate to a http://play.golang.org page, input a short program, run it, and inspect its output.

package main

import (
	"fmt"
	"os"

	"github.com/lexLibrary/sequence"
	"github.com/tebeka/selenium"
)

func main() {
	// Start a Selenium WebDriver server instance (if one is not already
	// running).
	const (
		// These paths will be different on your system.
		seleniumPath    = "vendor/selenium-server-standalone-3.4.jar"
		geckoDriverPath = "vendor/geckodriver-v0.18.0-linux64"
		port            = 8080
	)
	opts := []selenium.ServiceOption{
		selenium.StartFrameBuffer(),           // Start an X frame buffer for the browser to run in.
		selenium.GeckoDriver(geckoDriverPath), // Specify the path to GeckoDriver in order to use Firefox.
		selenium.Output(os.Stderr),            // Output debug information to STDERR.
	}
	selenium.SetDebug(true)
	service, err := selenium.NewSeleniumService(seleniumPath, port, opts...)
	if err != nil {
		panic(err) // panic is used only as an example and is not otherwise recommended.
	}
	defer service.Stop()

	// Connect to the WebDriver instance running locally.
	caps := selenium.Capabilities{"browserName": "firefox"}
	wd, err := selenium.NewRemote(caps, fmt.Sprintf("http://localhost:%d/wd/hub", port))
	if err != nil {
		panic(err)
	}
	defer wd.Quit()

	// once you have a webdriver initiated, then you simply pass it into a new sequence

	err = sequence.Start(wd).
		Get("http://play.golang.org/?simple=1").
		Find("#code").Clear().SendKeys(`
			package main
			import "fmt"

			func main() {
				fmt.Println("Hello WebDriver!\n")
			}
		`).
		Find("#run").Click().
		Find("#output").
		Text().Contains("Hello WebDriver").Eventually().
		End()
	if err != nil {
		panic(err)
	}
}
Output:

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Elements

type Elements struct {
	// contains filtered or unexported fields
}

Elements is a collections of web elements

func (*Elements) All

func (e *Elements) All() *Elements

All means the following tests will pass if they pass only if pass for ALL of the selected elements

func (*Elements) And

func (e *Elements) And() *Sequence

And allows you chain additional sequences

func (*Elements) Any

func (e *Elements) Any() *Elements

Any means the following tests will pass if they pass for ANY of the selected elements

func (*Elements) Attribute

func (e *Elements) Attribute(attribute string) *StringMatch

Attribute tests if the elements attribute matches

func (*Elements) CSSProperty

func (e *Elements) CSSProperty(property string) *StringMatch

CSSProperty tests if the elements attribute matches

func (*Elements) Clear

func (e *Elements) Clear() *Elements

Clear clears the elements

func (*Elements) Click

func (e *Elements) Click() *Elements

Click sends a click to all of the elements

func (*Elements) Count

func (e *Elements) Count(count int) *Elements

Count verifies that the number of elements in the selection matches the argument

func (*Elements) Disabled

func (e *Elements) Disabled() *Elements

Disabled tests if the elements are hidden

func (*Elements) Enabled

func (e *Elements) Enabled() *Elements

Enabled tests if the elements are hidden

func (*Elements) End

func (e *Elements) End() error

End Completes a sequence and returns any errors

func (*Elements) Eventually

func (e *Elements) Eventually() *Elements

Eventually will retry the previous test if it returns an error every EventuallyPoll duration until EventualTimeout is reached

func (*Elements) Filter added in v0.0.6

func (e *Elements) Filter(fn func(we *Elements) error) *Elements

Filter filters out any elements for which the passed in function returns an error, useful for matching elements by text contents, since they can't be selected for with css selectors

func (*Elements) Find

func (e *Elements) Find(selector string) *Elements

Find finds a new element

func (*Elements) FindChildren

func (e *Elements) FindChildren(selector string) *Elements

FindChildren returns a new Elements object for all the elements that match the selector

func (*Elements) Hidden

func (e *Elements) Hidden() *Elements

Hidden tests if the elements are hidden

func (*Elements) Ok added in v0.0.7

func (e *Elements) Ok(tb testing.TB)

Ok is a shortcut for Sequence.Ok

func (*Elements) Selected

func (e *Elements) Selected() *Elements

Selected tests if the elements are selected

func (*Elements) SendKeys

func (e *Elements) SendKeys(keys string) *Elements

SendKeys sends a string of key to the elements

func (*Elements) Submit

func (e *Elements) Submit() *Elements

Submit sends a submit event to the elements

func (*Elements) TagName

func (e *Elements) TagName() *StringMatch

TagName tests if the elements match the given tag name

func (*Elements) Test

func (e *Elements) Test(testName string, fn func(e selenium.WebElement) error) *Elements

Test tests an arbitrary function against all the elements in this sequence if the function returns an error then the test fails

func (*Elements) Text

func (e *Elements) Text() *StringMatch

Text tests if the elements matches

func (*Elements) Unselected

func (e *Elements) Unselected() *Elements

Unselected tests if the elements aren't selected

func (*Elements) Visible

func (e *Elements) Visible() *Elements

Visible tests if the elements are visible

func (*Elements) Wait

func (e *Elements) Wait(duration time.Duration) *Elements

Wait sleeps for the given duration

type Error

type Error struct {
	Stage   string
	Element selenium.WebElement
	Err     error
	Caller  string
}

Error describes an error that occured during the sequence processing.

func (*Error) Error

func (e *Error) Error() string

Error fulfills the error interface

type Errors

type Errors []error

Errors is multiple sequence errors

func (Errors) Error

func (e Errors) Error() string

type Sequence

type Sequence struct {
	EventualPoll    time.Duration
	EventualTimeout time.Duration
	// contains filtered or unexported fields
}

Sequence is a helper structs of chaining selecting elements and testing them if any part of the sequence fails the sequence ends and returns the error built to make writing tests easier

func Start

func Start(driver selenium.WebDriver) *Sequence

Start starts a new sequence of tests

func (*Sequence) Back

func (s *Sequence) Back() *Sequence

Back moves back in the browser's history

func (*Sequence) Debug

func (s *Sequence) Debug() *Sequence

Debug will print the current page's title and source For use with debugging issues mostly

func (*Sequence) Driver

func (s *Sequence) Driver() selenium.WebDriver

Driver returns the underlying WebDriver

func (*Sequence) End

func (s *Sequence) End() error

End ends a sequence and returns any errors

func (*Sequence) Eventually

func (s *Sequence) Eventually() *Sequence

Eventually will retry the previous test if it returns an error every EventuallyPoll duration until EventualTimeout is reached

func (*Sequence) Find

func (s *Sequence) Find(selector string) *Elements

Find returns a selection of one or more elements to apply a set of actions against If .Any or.All are not specified, then it is assumed that the selection will contain a single element and the tests will fail if more than one element is found

func (*Sequence) Forward

func (s *Sequence) Forward() *Sequence

Forward moves forward in the browser's history

func (*Sequence) Get

func (s *Sequence) Get(uri string) *Sequence

Get navigates to the passed in URI

func (*Sequence) Ok added in v0.0.7

func (s *Sequence) Ok(tb testing.TB)

OK ends a sequence and fails and stopped the tests passed in if the sequence is in error

func (*Sequence) OnError added in v0.0.3

func (s *Sequence) OnError(fn func(err Error, s *Sequence)) *Sequence

OnError registers a function to call when an error occurs in the sequence. Handy for calling things like .Debug() and .Screenshot("err.png") in error scenarios to output to a CI server OnError must be called before any errors in order for it to be triggered properly

func (*Sequence) Refresh

func (s *Sequence) Refresh() *Sequence

Refresh refreshes the page

func (*Sequence) Screenshot

func (s *Sequence) Screenshot(filename string) *Sequence

Screenshot takes a screenshot

func (*Sequence) Test

func (s *Sequence) Test(testName string, fn func(d selenium.WebDriver) error) *Sequence

Test runs an arbitrary test against the entire page

func (*Sequence) Title

func (s *Sequence) Title() *TitleMatch

Title checks the match against the page's title

func (*Sequence) URL

func (s *Sequence) URL() *URLMatch

URL tests against the current page URL

func (*Sequence) Wait

func (s *Sequence) Wait(duration time.Duration) *Sequence

Wait will wait for the given duration before continuing in the sequence

type StringMatch

type StringMatch struct {
	// contains filtered or unexported fields
}

StringMatch is for testing the value of strings in elements

func (*StringMatch) Contains

func (s *StringMatch) Contains(match string) *Elements

Contains tests if the string value contains the passed in value

func (*StringMatch) EndsWith

func (s *StringMatch) EndsWith(match string) *Elements

EndsWith tests if the string value end with the passed in value

func (*StringMatch) Equals

func (s *StringMatch) Equals(match string) *Elements

Equals tests if the string value matches the passed in value exactly

func (*StringMatch) Regexp

func (s *StringMatch) Regexp(exp *regexp.Regexp) *Elements

Regexp tests if the string value matches the regular expression

func (*StringMatch) StartsWith

func (s *StringMatch) StartsWith(match string) *Elements

StartsWith tests if the string value starts with the passed in value

type TitleMatch

type TitleMatch struct {
	// contains filtered or unexported fields
}

TitleMatch is for testing the value of the title

func (*TitleMatch) Contains

func (t *TitleMatch) Contains(match string) *Sequence

Contains tests if the title contains the passed in value

func (*TitleMatch) EndsWith

func (t *TitleMatch) EndsWith(match string) *Sequence

EndsWith tests if the title ends with the passed in value

func (*TitleMatch) Equals

func (t *TitleMatch) Equals(match string) *Sequence

Equals tests if the title matches the passed in value exactly

func (*TitleMatch) Regexp

func (t *TitleMatch) Regexp(exp *regexp.Regexp) *Sequence

Regexp tests if the title matches the regular expression

func (*TitleMatch) StartsWith

func (t *TitleMatch) StartsWith(match string) *Sequence

StartsWith tests if the title starts with the passed in value

type URLMatch

type URLMatch struct {
	// contains filtered or unexported fields
}

URLMatch is for testing the value of the page's URL

func (*URLMatch) Fragment

func (u *URLMatch) Fragment(match string) *Sequence

Fragment tests if the page's url fragment (#) matches the passed in value

func (*URLMatch) Path

func (u *URLMatch) Path(match string) *Sequence

Path tests if the page's url path matches the passed in value

func (*URLMatch) QueryValue

func (u *URLMatch) QueryValue(key, value string) *Sequence

QueryValue tests if the page's url contains the url query matches the value

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL