![Mutation testing badge](https://badge.stryker-mutator.io/github.com/netsells/simulate-user/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
Classes
- SimulateUser
Simulate a user
Typedefs
- SearchProperties :
Object
- ValueSelector :
SearchProperties
| String
| Number
A generic value selector. For a textarea
or input
it should always be a
string or number, for a select
it can be a string or a SearchProperties
SimulateUser
Simulate a user
Kind: global class
new SimulateUser(node)
Create a SimulateUser class for a page element
simulateUser.visible ⇒ Boolean
Check if the node is visible
Kind: instance property of SimulateUser
simulateUser.hidden ⇒ Boolean
Check if the node is hidden
Kind: instance property of SimulateUser
simulateUser.nextElementSibling ⇒ SimulateUser
| null
nextElementSibling but returns a wrapper
Kind: instance property of SimulateUser
simulateUser.options ⇒ Array.<String>
Get all select option values
Kind: instance property of SimulateUser
simulateUser.text ⇒ String
Get trimmed text content
Kind: instance property of SimulateUser
simulateUser.directText ⇒ String
Get text content which is a direct child of this node
Kind: instance property of SimulateUser
simulateUser.parentElement ⇒ SimulateUser
Get the parentElement in a wrapper
Kind: instance property of SimulateUser
simulateUser.className ⇒ String
Proxy for className
Kind: instance property of SimulateUser
simulateUser.value ⇒ String
Proxy for value
Kind: instance property of SimulateUser
simulateUser.htmlFor ⇒ String
Proxy for htmlFor
Kind: instance property of SimulateUser
simulateUser.tag ⇒ String
tagName but lower case
Kind: instance property of SimulateUser
simulateUser.sleep(timeout) ⇒ Promise.<undefined>
Returns a promise which resolves in a certain amount of milliseconds
Kind: instance method of SimulateUser
simulateUser.timeout(func, limit) ⇒ Promise.<*>
Returns a promise which times out if the passed in promise doesn't
resolve in time
Kind: instance method of SimulateUser
Param | Type |
---|
func | function |
limit | Number |
simulateUser.getEventOptions(options) ⇒ Object
Get options for an event
Kind: instance method of SimulateUser
Proxy for querySelectorAll but returns an array of wrappers instead of
nodes
Kind: instance method of SimulateUser
Param | Type |
---|
query | String | Array.<String> |
simulateUser.getElementById(id) ⇒ SimulateUser
| null
getElementById but returns a wrapper
Kind: instance method of SimulateUser
getElementsByName but returns an array of wrappers
Kind: instance method of SimulateUser
simulateUser.closest() ⇒ SimulateUser
| null
closest but returns a wrapper
Kind: instance method of SimulateUser
simulateUser.all(options) ⇒ SimulateUser
| null
Search through page elements as a user would, using text
Kind: instance method of SimulateUser
simulateUser.first(options) ⇒ SimulateUser
| null
Get the first element of a query to all
Kind: instance method of SimulateUser
simulateUser.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: instance method of SimulateUser
Throws:
Param | Type | Description |
---|
options | SearchProperties | |
[options.similar] | Boolean | If no exact matches found, fall back to a fuzzy search |
limit | Number | |
simulateUser.field(label) ⇒ SimulateUser
| null
Get a field based on its label
Kind: instance method of SimulateUser
Throws:
simulateUser.fieldSet(legend) ⇒ SimulateUser
| null
Get a fieldset based on its legend
Kind: instance method of SimulateUser
Throws:
simulateUser.dispatchEvent(event)
Proxy for dispatchEvent
Kind: instance method of SimulateUser
simulateUser.click(search)
Click this node
Kind: instance method of SimulateUser
simulateUser.attach(files)
Attach files to this input element
Kind: instance method of SimulateUser
Param | Type |
---|
files | Enumerable.<Files> |
simulateUser.check(checked)
Check this checkbox
Kind: instance method of SimulateUser
Param | Type | Default |
---|
checked | Boolean | true |
simulateUser.focus()
Focus this element
Kind: instance method of SimulateUser
simulateUser.blur()
Blur this element
Kind: instance method of SimulateUser
simulateUser.typeKey(key)
Type a single key on this element
Kind: instance method of SimulateUser
simulateUser.type(text)
Type a string on this element
Kind: instance method of SimulateUser
simulateUser.typeValue(text)
Type into a fields value. Only simulates the final key press then
triggers a single change event
Kind: instance method of SimulateUser
Param | Type |
---|
text | String | Number |
simulateUser.fillIn(label, value) ⇒ SimulateUser
Find a field by its label then fill it in
Kind: instance method of SimulateUser
Returns: SimulateUser
- - The field wrapper
simulateUser.fill(value)
Fill in this node as a field
Kind: instance method of SimulateUser
simulateUser.select(value)
Change a value by the option text
Kind: instance method of SimulateUser
simulateUser.sendChangeEvent()
Send a change event
Kind: instance method of SimulateUser
Generate a instance using the same class constructor
Kind: static method of SimulateUser
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 | Boolean | If element must be visible or not |
direct | Boolean | If text should be a direct child or not |
ValueSelector : SearchProperties
| String
| Number
A generic value selector. For a textarea
or input
it should always be a
string or number, for a select
it can be a string or a SearchProperties
Kind: global typedef