0.11.0 • Published 2 years ago

@bucky24/jsbehave v0.11.0

Weekly downloads
176
License
MIT
Repository
github
Last release
2 years ago

jsbehave

A module that allows an easy way to write browser based testing in Selenium

Usage

In order to use this, you must have the webdriver for your chosen browser accessible in your path. For example, using Chrome:

  • Download chromedriver for your browser version
  • Move the binary to /usr/local/bin

Operations

open <browser> (note the name for this browser is default)

navigate to <url>

type <text> into <selector>

wait for title to be <text>

[test <string>]

[endtest]

click <selector>

sleep <number>

wait until located <selector>

load <config file>

expect element <selector> to have text <text>

expect element <selector> to exist

expect element <selector> to not exist

close browser

reload page

require test <test name> runs the test only if it has not already been run

expect elements <selector> to have count of <count>

expect variable <variable> to match <text>

open <browser> as <name>

close browser <name>

set active browser to <name>

expect element <selector> to have contents <text>

set variable <variable name> to <text>

concat variable <variable name> with <text>

run test <test name> runs the test regardless if it has been run before

[action <action name>]

[endaction]

run action <action name> runs the given action

switch to window <index> switches context to the window handle at the index (useful for popups)

Tests

The operations [test <name>] and [endtest] indicate a test block.

Text

Text that is wrapped in double quotes is treated as text

Text that starts with a $ will load a variable of the same name

Text that starts with a / may be considered a regex by some commands

Special text elements:

These should be entered without quotes or any other text.

NameDescription
uuidGenerates a new uuid-v4
returnSends enter key
upSends up key
downSends down key
leftSends left key
rightSends right key

Variables

There are some special variables:

NameDescription
clipboardReads the active value from the clipboard

Selector

Selectors are of the format type=value

TypeDescription
nameSearches by element name
textSearches for elements containing text (note this is not Text, it can handle variables but nothing else)
selectorSearches for elements matching css selector
idSearches for elements with the given id
data-idSearches for elements with the given data-test-id
xpathSearches for elements matching the given xpath selector

Config File

The config file is of the following format:

var_name=value
var_name2=value2

Each value is stored as a variable with the given name.

The following are configs that the jsbehave system will use for various operations:

TypeDescription
screenshotDirectoryThe directory where screenshots will be saved. Defaults to .
screenshotOnFailureBoolean. If true (default), takes a screenshot when the test fails.

Before and After blocks

Code in these blocks looks like the following:

[before all]
open chrome
[endbefore]

[before each]
reload page
[endbefore]

[test blah]

[endtest]

[after each]
# cleanup
[endafter]

[after all]
close browser
[endafter]

[before all] blocks will run before all the tests. [before each] runs before each test. [after each] runs after each test, and [after all] runs after all the tests are done. This is similar to most other test systems.

Action blocks

Code in these blocks looks like the following:

[action some action]
open chrome
[endaction]

These actions are identical to test blocks, except they are not automatically run when tests are run. They can be run as follows:

[test a test]
run action some action
[endtest]

Custom code

In order to load custom code, put load funcs <filename> in your test file.

The function should set module.exports to an object that looks like the following:

module.exports = {
    "selectors": {
        "<selector type>": <selector function>
    },
    "operations": {
        "<operation regex>": <operation function>
    }
}

Selectors

Selector functions should have the following header:

function selectorFunction({ By, getVariable }, inputText)

And should return a selector method By.*. They can be used as follows:

type Blah into selector-type=foo

In this case the system will expect that you've setup selector-type as a selector. In this case foo would be the value of inputText.

Operations

Operation functions should have the following header:

function operationFunction(paramsFromRegex, sdk)

Any return value is ignored (unless it's a promise, in which case it is awaited). Any error thrown from within an operation shows up as a test failure.

Params are any params from capture groups in the regex, and the sdk contains the following:

NameDescription
driverThe selenium webdriver instance
getVariableMethod to get any variable (including config variables)
setVariableMethod to set a variable
runActionMethod that can run any action group
runTestMethod that can run any test group
executeJSMethod that can execute arbitrary JavaScript code on the page
handleLinesMethod that runs an array of JSBehave operations
0.11.0

2 years ago

0.10.3

3 years ago

0.10.0

3 years ago

0.10.1

3 years ago

0.10.2

3 years ago

0.9.0

3 years ago

0.8.3

3 years ago

0.8.2

3 years ago

0.8.1

3 years ago

0.8.0

3 years ago