![Build Status](https://travis-ci.org/netsells/simulate-user.svg?branch=master)
@netsells/simulate-user
Library for simulating user interactions using JavaScript in the browser
Installation
yarn add @netsells/simulate-user
Usage
import SimulateUser from '@netsells/simulate-user';
const sim = new SimulateUser();
const el = await sim.find({ query: 'a', text: 'Click me' });
await el.click();
Documentation
Members
- visible ⇒
Boolean
Check if the node is visible
- hidden ⇒
Boolean
Check if the node is hidden
- nextElementSibling ⇒
SimulateUser
| null
nextElementSibling but returns a wrapper
- options ⇒
Array.<String>
Get all select option values
- text ⇒
String
Get trimmed text content
- value ⇒
String
Proxy for value
- htmlFor ⇒
String
Proxy for htmlFor
- tag ⇒
String
tagName but lower case
Functions
- build() ⇒
SimulateUser
Generate a instance using the same class constructor
- log()
Proxy for console.log
- error()
Proxy for console.error
- sleep(timeout) ⇒
Promise.<undefined>
Returns a promise which resolves in a certain amount of milliseconds
- timeout(func, limit) ⇒
Promise.<*>
Returns a promise which times out if the passed in promise doesn't
resolve in time
- getEventOptions(options) ⇒
Object
Get options for an event
- querySelectorAll(query) ⇒
Array.<SimulateUser>
Proxy for querySelectorAll but returns an array of wrappers instead of
nods
- getElementById(id) ⇒
SimulateUser
| null
getElementById but returns a wrapper
- getElementsByName(name) ⇒
Array.<SimulateUser>
getElementsByName but returns an array of wrappers
- closest() ⇒
SimulateUser
| null
closest but returns a wrapper
- all(options) ⇒
SimulateUser
| null
Search through page elements as a user would, using text
- first(options) ⇒
SimulateUser
| null
Get the first element of a query to all
- find(options, limit) ⇒
SimulateUser
Get the first element of a query to all
, but throws an error if it's
not found. Will wait for an element to appear (e.g. if a form is
updating)
- field(label) ⇒
SimulateUser
| null
Get a field based on its label
- fieldSet(legend) ⇒
SimulateUser
| null
Get a fieldset based on its legend
- dispatchEvent(event)
Proxy for dispatchEvent
- click()
Click this node
- attach(files)
Attach files to this input element
- check(checked)
Check this checkbox
- focus()
Focus this element
- typeKey(key)
Type a single key on this element
- type(text)
Type a string on this element
- typeValue(text)
Type into a fields value. Only simulates the final key press then
triggers a single change event
- fillIn(label, value) ⇒
SimulateUser
Find a field by its label then fill it in
- fillSelect(label, text, options) ⇒
SimulateUser
Find a select by its label then fill it in
- select(options)
Change a value by the option text
- sendChangeEvent()
Send a change event
Typedefs
- SearchProperties :
Object
visible ⇒ Boolean
Check if the node is visible
Kind: global variable
hidden ⇒ Boolean
Check if the node is hidden
Kind: global variable
nextElementSibling ⇒ SimulateUser
| null
nextElementSibling but returns a wrapper
Kind: global variable
options ⇒ Array.<String>
Get all select option values
Kind: global variable
text ⇒ String
Get trimmed text content
Kind: global variable
value ⇒ String
Proxy for value
Kind: global variable
htmlFor ⇒ String
Proxy for htmlFor
Kind: global variable
tag ⇒ String
tagName but lower case
Kind: global variable
build() ⇒ SimulateUser
Generate a instance using the same class constructor
Kind: global function
log()
Proxy for console.log
Kind: global function
error()
Proxy for console.error
Kind: global function
sleep(timeout) ⇒ Promise.<undefined>
Returns a promise which resolves in a certain amount of milliseconds
Kind: global function
timeout(func, limit) ⇒ Promise.<*>
Returns a promise which times out if the passed in promise doesn't
resolve in time
Kind: global function
Param | Type | Default |
---|
func | function | |
limit | Number | 2000 |
getEventOptions(options) ⇒ Object
Get options for an event
Kind: global function
querySelectorAll(query) ⇒ Array.<SimulateUser>
Proxy for querySelectorAll but returns an array of wrappers instead of
nods
Kind: global function
Param | Type |
---|
query | String | Array.<String> |
getElementById(id) ⇒ SimulateUser
| null
getElementById but returns a wrapper
Kind: global function
getElementsByName(name) ⇒ Array.<SimulateUser>
getElementsByName but returns an array of wrappers
Kind: global function
closest() ⇒ SimulateUser
| null
closest but returns a wrapper
Kind: global function
all(options) ⇒ SimulateUser
| null
Search through page elements as a user would, using text
Kind: global function
first(options) ⇒ SimulateUser
| null
Get the first element of a query to all
Kind: global function
find(options, limit) ⇒ SimulateUser
Get the first element of a query to all
, but throws an error if it's
not found. Will wait for an element to appear (e.g. if a form is
updating)
Kind: global function
Throws:
Param | Type | Description |
---|
options | SearchProperties | |
[options.similar] | Boolean | If no exact matches found, fall back to a fuzzy search |
limit | Number | |
field(label) ⇒ SimulateUser
| null
Get a field based on its label
Kind: global function
Throws:
fieldSet(legend) ⇒ SimulateUser
| null
Get a fieldset based on its legend
Kind: global function
Throws:
dispatchEvent(event)
Proxy for dispatchEvent
Kind: global function
click()
Click this node
Kind: global function
attach(files)
Attach files to this input element
Kind: global function
Param | Type |
---|
files | Enumerable.<Files> |
check(checked)
Check this checkbox
Kind: global function
Param | Type | Default |
---|
checked | Boolean | true |
focus()
Focus this element
Kind: global function
typeKey(key)
Type a single key on this element
Kind: global function
type(text)
Type a string on this element
Kind: global function
typeValue(text)
Type into a fields value. Only simulates the final key press then
triggers a single change event
Kind: global function
fillIn(label, value) ⇒ SimulateUser
Find a field by its label then fill it in
Kind: global function
Returns: SimulateUser
- - The field wrapper
Param | Type |
---|
label | String |
value | String |
fillSelect(label, text, options) ⇒ SimulateUser
Find a select by its label then fill it in
Kind: global function
Returns: SimulateUser
- - The field wrapper
Param | Type |
---|
label | String |
text | String |
options | Object |
select(options)
Change a value by the option text
Kind: global function
sendChangeEvent()
Send a change event
Kind: global function
SearchProperties : Object
Kind: global typedef
Properties
Name | Type | Description |
---|
text | String | Text to search on |
query | String | Optional query to filter on |
caseSensitive | Boolean | Whether text is case sensitive |
exact | Boolean | Whether text match should be exact (not including trimmed white space) |
predicate | function | Predicate function wrappers must match |
visible | function | If element must be visible or not |