@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
- DebugUser
-
Helper class for providing debug information.
- SimulateUser
-
Simulate a user.
Functions
-
getDebugUser(Klass) ⇒
object
-
Get a debug user extending a user class.user.
-
get(target, prop) ⇒
any
-
Get the needed property.
Typedefs
-
SearchProperties :
object
-
ValueSelector :
SearchProperties
|string
|number
-
A generic value selector. For a
textarea
orinput
it should always be a string or number, for aselect
it can be a string or aSearchProperties
.
DebugUser
Helper class for providing debug information.
Kind: global class
-
DebugUser
- new DebugUser(...args)
-
.build(...args) ⇒
Proxy.<object>
- .emit(callback, ...args)
- .on(...args)
new DebugUser(...args)
Setup the class.
Param | Type |
---|---|
...args | any |
Proxy.<object>
debugUser.build(...args) ⇒ Generate a instance using the same class constructor and debug emitter.
Kind: instance method of DebugUser
Param | Type |
---|---|
...args | any |
debugUser.emit(callback, ...args)
Emit and log an event.
Kind: instance method of DebugUser
Param | Type |
---|---|
callback | string |
...args | any |
debugUser.on(...args)
Listen to a debug event.
Kind: instance method of DebugUser
Param | Type |
---|---|
...args | any |
SimulateUser
Simulate a user.
Kind: global class
-
SimulateUser
- new SimulateUser(node)
-
.visible ⇒
boolean
-
.hidden ⇒
boolean
-
.nextElementSibling ⇒
SimulateUser
|null
-
.options ⇒
Array.<string>
-
.text ⇒
string
-
.directText ⇒
string
-
.parentElement ⇒
SimulateUser
-
.className ⇒
string
-
.value ⇒
string
-
.htmlFor ⇒
string
-
.tag ⇒
string
-
.build(...args) ⇒
Proxy.<SimulateUser>
-
.sleep(timeout) ⇒
Promise.<undefined>
-
.timeout(func, limit) ⇒
Promise.<*>
-
.getEventOptions(options) ⇒
object
-
.querySelectorAll(query) ⇒
Array.<SimulateUser>
-
.getElementById(id) ⇒
SimulateUser
|null
-
.getElementsByName(name) ⇒
Array.<SimulateUser>
-
.closest(...args) ⇒
SimulateUser
|null
-
.all(options) ⇒
SimulateUser
|null
-
.first(options) ⇒
SimulateUser
|null
-
.find(options, limit) ⇒
SimulateUser
-
.field(label, [findOptions]) ⇒
SimulateUser
|null
-
.fieldSet(legend) ⇒
SimulateUser
|null
- .dispatchEvent(event)
- .click(search)
- .attach(files)
- .check(checked)
- .focus()
- .blur()
- .typeKey(key)
- .type(text)
- .typeValue(text)
-
.fillIn(label, value) ⇒
SimulateUser
- .fill(value)
- .select(value)
- .sendChangeEvent()
new SimulateUser(node)
Create a SimulateUser class for a page element.
Param | Type |
---|---|
node | HTMLElement |
boolean
simulateUser.visible ⇒ Check if the node is visible.
Kind: instance property of SimulateUser
boolean
simulateUser.hidden ⇒ Check if the node is hidden.
Kind: instance property of SimulateUser
SimulateUser
| null
simulateUser.nextElementSibling ⇒ NextElementSibling but returns a wrapper.
Kind: instance property of SimulateUser
Array.<string>
simulateUser.options ⇒ Get all select option values.
Kind: instance property of SimulateUser
string
simulateUser.text ⇒ Get trimmed text content.
Kind: instance property of SimulateUser
string
simulateUser.directText ⇒ Get text content which is a direct child of this node.
Kind: instance property of SimulateUser
SimulateUser
simulateUser.parentElement ⇒ Get the parentElement in a wrapper.
Kind: instance property of SimulateUser
string
simulateUser.className ⇒ Proxy for className.
Kind: instance property of SimulateUser
string
simulateUser.value ⇒ Proxy for value.
Kind: instance property of SimulateUser
string
simulateUser.htmlFor ⇒ Proxy for htmlFor.
Kind: instance property of SimulateUser
string
simulateUser.tag ⇒ TagName but lower case.
Kind: instance property of SimulateUser
Proxy.<SimulateUser>
simulateUser.build(...args) ⇒ Generate a instance using the same class constructor and debug emitter.
Kind: instance method of SimulateUser
Param | Type |
---|---|
...args | any |
Promise.<undefined>
simulateUser.sleep(timeout) ⇒ Returns a promise which resolves in a certain amount of milliseconds.
Kind: instance method of SimulateUser
Param | Type |
---|---|
timeout | number |
Promise.<*>
simulateUser.timeout(func, limit) ⇒ 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 |
object
simulateUser.getEventOptions(options) ⇒ Get options for an event.
Kind: instance method of SimulateUser
Param | Type |
---|---|
options | object |
Array.<SimulateUser>
simulateUser.querySelectorAll(query) ⇒ Proxy for querySelectorAll but returns an array of wrappers instead of nodes.
Kind: instance method of SimulateUser
Param | Type |
---|---|
query |
string | Array.<string>
|
SimulateUser
| null
simulateUser.getElementById(id) ⇒ GetElementById but returns a wrapper.
Kind: instance method of SimulateUser
Param | Type |
---|---|
id | string |
Array.<SimulateUser>
simulateUser.getElementsByName(name) ⇒ GetElementsByName but returns an array of wrappers.
Kind: instance method of SimulateUser
Param | Type |
---|---|
name | string |
SimulateUser
| null
simulateUser.closest(...args) ⇒ Closest but returns a wrapper.
Kind: instance method of SimulateUser
Param | Type |
---|---|
...args | any |
SimulateUser
| null
simulateUser.all(options) ⇒ Search through page elements as a user would, using text.
Kind: instance method of SimulateUser
Param | Type |
---|---|
options | SearchProperties |
SimulateUser
| null
simulateUser.first(options) ⇒ Get the first element of a query to all
.
Kind: instance method of SimulateUser
Param | Type |
---|---|
options | SearchProperties |
SimulateUser
simulateUser.find(options, limit) ⇒ 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:
Error
Param | Type | Description |
---|---|---|
options | SearchProperties |
|
[options.similar] | boolean |
If no exact matches found, fall back to a fuzzy search. |
limit | number |
SimulateUser
| null
simulateUser.field(label, [findOptions]) ⇒ Get a field based on its label.
Kind: instance method of SimulateUser
Throws:
Error
Param | Type | Default |
---|---|---|
label | string |
|
[findOptions] | object |
{} |
SimulateUser
| null
simulateUser.fieldSet(legend) ⇒ Get a fieldset based on its legend.
Kind: instance method of SimulateUser
Throws:
Error
Param | Type |
---|---|
legend | string |
simulateUser.dispatchEvent(event)
Proxy for dispatchEvent.
Kind: instance method of SimulateUser
Param | Type |
---|---|
event | Event |
simulateUser.click(search)
Click this node.
Kind: instance method of SimulateUser
Param | Type | Default |
---|---|---|
search | SearchProperties |
|
simulateUser.attach(files)
Attach files to this input element.
Kind: instance method of SimulateUser
Param | Type |
---|---|
files | Array.<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
Param | Type |
---|---|
key | string |
simulateUser.type(text)
Type a string on this element.
Kind: instance method of SimulateUser
Param | Type |
---|---|
text | string |
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
simulateUser.fillIn(label, value) ⇒ Find a field by its label then fill it in.
Kind: instance method of SimulateUser
Returns: SimulateUser
- - The field wrapper.
Param | Type |
---|---|
label | string |
value | ValueSelector |
simulateUser.fill(value)
Fill in this node as a field.
Kind: instance method of SimulateUser
Param | Type |
---|---|
value | ValueSelector |
simulateUser.select(value)
Change a value by the option text.
Kind: instance method of SimulateUser
Param | Type |
---|---|
value | ValueSelector |
simulateUser.sendChangeEvent()
Send a change event.
Kind: instance method of SimulateUser
object
getDebugUser(Klass) ⇒ Get a debug user extending a user class.user.
Kind: global function
Param | Type |
---|---|
Klass | SimulateUser |
any
get(target, prop) ⇒ Get the needed property.
Kind: global function
Param | Type |
---|---|
target | object |
prop | any |
object
SearchProperties : 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. |
SearchProperties
| string
| number
ValueSelector : 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