@shellygo/cypress-test-utils - v4.1.10 / Modules / CypressHelper
Class: CypressHelper
Classdes
CypressHelper exposes the following public properties:
Table of contents
Constructors
Properties
Methods
Constructors
constructor
• new CypressHelper(options?
)
Parameters
Name | Type |
---|---|
options? |
CypressHelperOptions |
Properties
get
• get: Object
The get property will hold methods which will give our tests access to the “output” of the component in a “black box” fashion
Type declaration
Name | Type |
---|---|
assertableStub |
(stub : any ) => Chainable <JQuery <any >> |
bySelector |
(selector : string , attribute? : string ) => Chainable <JQuery <HTMLElement >> |
currentLocation |
() => Chainable <string > |
element |
(selector : string , index? : number ) => Chainable <JQuery <HTMLElement >> |
elementByAttribute |
(attribute : string , selector : string ) => Chainable <JQuery <HTMLElement >> |
elementBySelector |
(selector : string , attribute? : string ) => Chainable <JQuery <HTMLElement >> |
elementByTestId |
(dataTestID : string , index? : number ) => Chainable <JQuery <HTMLElement >> |
elementByText |
(content : string | RegExp , index? : number ) => Chainable <JQuery <HTMLElement >> | Chainable <undefined > |
elementComputedStyleProperty |
(dataTestID : string , styleProperty : keyof CSSStyleDeclaration , __namedParameters? : { index? : number ; pseudoElement? : string }) => Chainable <null | string | number | CSSRule | (property : string ) => string | (property : string ) => string | (index : number ) => string | (property : string ) => string | (property : string , value : null | string , priority? : string ) => void > |
elementsAttribute |
(dataTestID : string , attributeName : string , index? : number ) => Chainable <undefined | string > |
elementsComputedStyle |
(dataTestID : string , index? : number , pseudoElement? : string ) => Chainable <CSSStyleDeclaration > |
elementsProperty |
(dataTestID : string , propertyName : keyof JQuery <HTMLElement >, index? : number ) => Chainable <any > |
elementsStyleAttribute |
(dataTestID : string , attributeName : string , index? : number ) => Chainable <PlainObject <string >> |
elementsText |
(dataTestID : string , index? : number ) => Chainable <string > |
env |
(key : string ) => any |
fixture |
(alias : string ) => Chainable <any > |
focusedElement |
() => Chainable <JQuery <HTMLElement >> |
imageSnapshot |
(name : string , __namedParameters? : SnapshotOptions ) => Chainable <DiffSnapshotResult > |
inputValue |
(dataTestID : string , index? : number ) => Chainable <string | number | string []> |
nthBySelector |
(selector : string , index? : number , attribute? : string ) => Chainable <JQuery <HTMLElement >> |
numberOfElements |
(dataTestID : string ) => Chainable <number > |
numberOfRequests |
(alias : string ) => Chainable <number > |
requestBody |
(alias : string ) => Chainable <any > |
requestHeader |
(alias : string ) => Chainable <{ [key: string] : string | string []; }> |
requestQueryParams |
(alias : string ) => Chainable <{ [k: string] : string ; }> |
requestUrl |
(alias : string ) => Chainable <string > |
responseBody |
(alias : string ) => Chainable <any > |
responseHeader |
(alias : string ) => Chainable <{ [key: string] : string | string []; }> |
spy |
(name : string ) => Chainable <JQuery <HTMLElement >> |
spyFromFunction |
(func : Function ) => Chainable <JQuery <HTMLElement >> |
stub |
(name : string ) => Chainable <JQuery <HTMLElement >> |
window |
() => Chainable <AUTWindow > |
assertableStub: (stub
: any
) => Chainable
<JQuery
<any
>>
Get stub as Cypress.Chainable
Example
const serviceMock : Service = helper.given.stubbedInstance(Service);
helper.get.assertableStub(serviceMock.function).should('have.been.called'));
Deprecated
The method should not be used anymore, use then
instead
bySelector: (selector
: string
, attribute?
: string
) => Chainable
<JQuery
<HTMLElement
>>
Get one or more DOM elements by selector.
Example
Get an element with shape="filter-grid"
currentLocation: () => Chainable
<string
>
Get the current URL of the page that is currently active.
element: (selector
: string
, index?
: number
) => Chainable
<JQuery
<HTMLElement
>>
Get one or more DOM elements by selector. The querying behavior of this command matches exactly how $(…) works in jQuery. *** Note! Using this method may lead to flakey tests! You should use get.elementByTestId ***
Example
get.element('.list>li', 3) // Yield the <li>'s in <.list>
get.element('ul li:first')
get.element('.dropdown-menu')
elementByAttribute: (attribute
: string
, selector
: string
) => Chainable
<JQuery
<HTMLElement
>>
Get one or more DOM elements by attribute.
Example
Get an element with shape="filter-grid"
elementBySelector: (selector
: string
, attribute?
: string
) => Chainable
<JQuery
<HTMLElement
>>
Get one or more DOM elements by selector. Same as bySelector
Example
Get an element with shape="filter-grid"
elementByTestId: (dataTestID
: string
, index?
: number
) => Chainable
<JQuery
<HTMLElement
>>
Get A DOM element at a specific index from elements.
Example
helper.when.dragAndDrop(
helper.get.elementByTestId('selected-item', 2),
helper.get.elementByTestId('available-items')
elementByText: (content
: string
| RegExp
, index?
: number
) => Chainable
<JQuery
<HTMLElement
>> | Chainable
<undefined
>
Get the DOM element containing the text. DOM elements can contain more than the desired text and still match. Additionally, Cypress prefers some DOM elements over the deepest element found. *** Note! Using this method may lead to flakey tests! You should use get.elementByTestId ***
Example
elementComputedStyleProperty: (dataTestID
: string
, styleProperty
: keyof CSSStyleDeclaration
, __namedParameters?
: { index?
: number
; pseudoElement?
: string
}) => Chainable
<null
| string
| number
| CSSRule
| (property
: string
) => string
| (property
: string
) => string
| (index
: number
) => string
| (property
: string
) => string
| (property
: string
, value
: null
| string
, priority?
: string
) => void
>
Returns a specific style of an element, including pseudo elements if specified.
Example
-----
**elementsAttribute**: (`dataTestID`: `string`, `attributeName`: `string`, `index?`: `number`) => `Chainable`<`undefined` \| `string`\>
**`Example`**
```ts
then(helper.get.elementsAttribute('avatar-picture', 'style')).shouldInclude('background-image: url("assets/avatar/def-user-male.png")')
elementsComputedStyle: (dataTestID
: string
, index?
: number
, pseudoElement?
: string
) => Chainable
<CSSStyleDeclaration
>
Returns element's computed style, including pseudo elements
Example
elementsProperty: (dataTestID
: string
, propertyName
: keyof JQuery
<HTMLElement
>, index?
: number
) => Chainable
<any
>
Get element's property value
Example
elementsStyleAttribute: (dataTestID
: string
, attributeName
: string
, index?
: number
) => Chainable
<PlainObject
<string
>>
Returns element's style attribute
Example
elementsText: (dataTestID
: string
, index?
: number
) => Chainable
<string
>
Example
env: (key
: string
) => any
Returns specific environment variable or undefined
Example
// Keeping password in cypress.config file
// using password during testfixture: (alias
: string
) => Chainable
<any
>
Get fixture
Example
given.fixture("user.json", "user");
then(
get.fixture("user").shouldDeepNestedInclude({
name: "Jane Doe",
id: "1234",
nested: {
attr1: "something",
attr2: "the other thing"
}
})
);
focusedElement: () => Chainable
<JQuery
<HTMLElement
>>
Get the element currently focused in the document.
imageSnapshot: (name
: string
, __namedParameters?
: SnapshotOptions
) => Chainable
<DiffSnapshotResult
>
Capture a snapshot and compare it to baseline snapshot Run Cypress with --env updateSnapshots=true in order to update the base image files for all of your tests.
Example
Example
// capture an element by DataTestId, with threshold
get.imageSnapshot("radio-group", {
dataTestID: "radio-group",
failureThreshold: 0.2
});
inputValue: (dataTestID
: string
, index?
: number
) => Chainable
<string
| number
| string
[]>
Get value of input element
Example
nthBySelector: (selector
: string
, index?
: number
, attribute?
: string
) => Chainable
<JQuery
<HTMLElement
>>
Get A DOM element at a specific index from elements.
Example
// Get the 3rd checkbox
numberOfElements: (dataTestID
: string
) => Chainable
<number
>
Get number of elements with a specific dataTestID
Example
numberOfRequests: (alias
: string
) => Chainable
<number
>
Get number of outgoing request with a specific alias
Example
requestBody: (alias
: string
) => Chainable
<any
>
Get intercepted request's body If a JSON Content-Type was used and the body was valid JSON, this will be an object. If the body was binary content, this will be a buffer.
requestHeader: (alias
: string
) => Chainable
<{ [key: string]
: string
| string
[]; }>
Get intercepted request's header
requestQueryParams: (alias
: string
) => Chainable
<{ [k: string]
: string
; }>
Get intercepted request's query param
requestUrl: (alias
: string
) => Chainable
<string
>
Get intercepted request's url
responseBody: (alias
: string
) => Chainable
<any
>
Get intercepted response's body If a JSON Content-Type was used and the body was valid JSON, this will be an object. If the body was binary content, this will be a buffer.
responseHeader: (alias
: string
) => Chainable
<{ [key: string]
: string
| string
[]; }>
Get intercepted response's header
spy: (name
: string
) => Chainable
<JQuery
<HTMLElement
>>
Get spy by alias
spyFromFunction: (func
: Function
) => Chainable
<JQuery
<HTMLElement
>>
Get spy by function name alias
stub: (name
: string
) => Chainable
<JQuery
<HTMLElement
>>
Get stub by alias
window: () => Chainable
<AUTWindow
>
Get the window object of the page that is currently active.
Example
given
• given: Object
The given property will hold methods which will allow us to set pre-conditions before something takes place. This is a classic place to have methods which will set the inputs which are going to be passed down to our component.
Type declaration
Name | Type |
---|---|
env |
(key : string , value : any ) => void |
fixture |
(filename : string , alias : string ) => Chainable <any > |
intercept |
(url : StringMatcher , alias : string , method? : string ) => void |
interceptAndMockResponse |
(options : { alias? : string ; method? : string ; response? : Object ; url : StringMatcher }) => void |
spy |
(name : string ) => Omit <SinonSpy <any [], any >, "withArgs" > & SinonSpyAgent <SinonSpy <any [], any >> & SinonSpy <any [], any > |
spyOnObject |
obj : T , method : keyof T ) => Omit <SinonSpy <any [], any >, "withArgs" > & SinonSpyAgent <SinonSpy <any [], any >> & SinonSpy <any [], any > |
stub |
(alias? : string ) => Agent <SinonStub <any [], any >> |
stubObjectMethod |
obj : T , method : keyof T ) => Omit <SinonStub <any [], any >, "withArgs" > & SinonSpyAgent <SinonStub <any [], any >> & SinonStub <any [], any > |
stubbedInstance |
constructor : (...args : any []) => T , overrides : Partial <T >) => StubbedInstance <T , SinonStub <any [], any >> & T |
stubbedInterface |
<T>(interfaceName : string , overrides : Partial <T >) => StubbedInstance <T , SinonStub <any [], any >> & T |
env: (key
: string
, value
: any
) => void
Sets specific environment variable's value
Example
// Changing an environment variable * ```ts helper.given.env("password", "changeMe@1");
-----
**fixture**: (`filename`: `string`, `alias`: `string`) => `Chainable`<`any`\>
Load a fixture
**`Example`**
```ts
let { given, when, get } = new CypressHelper();
it("should get fixture", () => {
given.fixture("user.json", "user");
then(get.fixture("user")).shouldDeepNestedInclude({
name: "Jane Doe",
id: "1234",
nested: {
attr1: "something",
attr2: "the other thing"
}
});
});
intercept: (url
: StringMatcher
, alias
: string
, method?
: string
) => void
Use intercept() to intercept HTTP requests and responses
Example
interceptAndMockResponse: (options
: { alias?
: string
; method?
: string
; response?
: Object
; url
: StringMatcher
}) => void
Use interceptAndMockResponse to stub and intercept HTTP requests and responses.
Example
// adds token to response header
helper.given.interceptAndMockResponse({
url: '** /sysmgmt/2015/bmc/session',
response: {
headers:{
'XSRF-Token': 'token',
},
},
alias: 'login'
})
Example
// mocks response to login request
helper.given.interceptAndMockResponse({
method: "POST",
url: "** /login",
alias: "login",
response : {
token: 'token'
}
})
Example
// mocks network error
helper.given.interceptAndMockResponse({
method: "POST",
url: "** /avamars",
alias: "avamar",
response : {
forceNetworkError: true
}
})
Example
// mocks missing image
helper.given.interceptAndMockResponse({
method: "POST",
url: "** /image.png",
alias: "image",
response: { statusCode: 404 }
})
Example
// using a fixture
helper.given.interceptAndMockResponse({
url: "** /shellygo/whatever**",
response: { fixture: "user.json" },
alias: "whatever"
});
spy: (name
: string
) => Omit
<SinonSpy
<any
[], any
>, "withArgs"
> & SinonSpyAgent
<SinonSpy
<any
[], any
>> & SinonSpy
<any
[], any
>
Returns a new spy function, and creates an alias for the newly created spy
spyOnObject: obj
: T
, method
: keyof T
) => Omit
<SinonSpy
<any
[], any
>, "withArgs"
> & SinonSpyAgent
<SinonSpy
<any
[], any
>> & SinonSpy
<any
[], any
>
Spy on a method and create an alias for the spy
Example
given.spyOnObject(window, "alert");
alert("whatever");
then(helper.get.spyFromFunction(window.alert)).shouldHaveBeenCalledWith("whatever");
// Or
then(helper.get.spy("alert")).shouldHaveBeenCalledTimes(1);
Example
given.spyOnObject(serviceMock, "functionName");
serviceMock.functionName();
then(helper.get.spyFromFunction(serviceMock.functionName)).shouldHaveBeenCalledTimes(1);
stub: (alias?
: string
) => Agent
<SinonStub
<any
[], any
>>
Replace a function, record its usage and control its behavior.
Example
stubObjectMethod: obj
: T
, method
: keyof T
) => Omit
<SinonStub
<any
[], any
>, "withArgs"
> & SinonSpyAgent
<SinonStub
<any
[], any
>> & SinonStub
<any
[], any
>
Stub an object's method and create an alias for the stub
Example
//stubbing a service method
helper.given.stubObjectMethod(serviceMock, "functionName").returns(3);
//stubbing setters and getters
helper.given.stubObjectMethod(serviceMock, "count").get(() => 3).set(() => {});
stubbedInstance: constructor
: (...args
: any
[]) => T
, overrides
: Partial
<T
>) => StubbedInstance
<T
, SinonStub
<any
[], any
>> & T
Creates a new object with the given functions as the prototype and stubs all implemented functions.
Example
Example
class Service {
public func1() {...}
public get prop1() {...}
}
const serviceMock = helper.given.stubbedInstance(Service, {prop1: 3});
Example
Example
helper.given.stubbedInstance(
PokemonService,
{
pokemonTypes: new BehaviorSubject<NamedAPIResource[]>([]),
pokemons: new BehaviorSubject<BetterPokemon[]>([]),
}
)
stubbedInterface: <T>(interfaceName
: string
, overrides
: Partial
<T
>) => StubbedInstance
<T
, SinonStub
<any
[], any
>> & T
Creates a new object with the given functions as the prototype and stubs all functions.
Example
Example
interface IService {
propertyFunc: (int: number) => number
get prop1() : number
}
const serviceMock = helper.given.stubbedInterface<IService>("IService", {prop1: 3});
when
• when: Object
The when property will hold methods of “events” which will take place like render, click, hover, etc.
Type declaration
Name | Type |
---|---|
acceptConfirm |
() => EventEmitter2 | Listener |
blur |
(dataTestID : string , index? : number ) => Chainable <JQuery <HTMLElement >> |
cancelConfirm |
() => Cypress |
cancelPrompt |
() => EventEmitter2 | Listener |
check |
(dataTestID : string , index? : number ) => Chainable <JQuery <HTMLElement >> |
clear |
(dataTestID : string , index? : number ) => Chainable <JQuery <HTMLElement >> |
click |
(dataTestID : string , index? : number ) => Chainable <JQuery <HTMLElement >> |
clock |
() => Chainable <Clock > |
closeAlert |
() => Cypress |
dblclick |
(dataTestID : string , index? : number ) => Chainable <JQuery <HTMLElement >> |
doWithin |
(fn : () => void , dataTestID : string , index? : number ) => Chainable <JQuery <HTMLElement >> |
dragAndDrop |
(element : Chainable <JQuery <HTMLElement >>, targetElement : Chainable <JQuery <HTMLElement >>) => void |
focus |
(dataTestID : string , index? : number ) => Chainable <JQuery <HTMLElement >> |
hover |
(dataTestID : string , index? : number ) => Chainable <JQuery <HTMLElement >> |
realClick |
(dataTestID : string , index? : number ) => Chainable <JQuery <HTMLElement >> |
realType |
(dataTestID : string , keys : string , index? : number ) => Chainable <void > |
rightclick |
(dataTestID : string , index? : number ) => Chainable <JQuery <HTMLElement >> |
scrollToBottom |
() => Chainable <undefined > |
scrollToTop |
() => Chainable <undefined > |
selectOption |
(dataTestID : string , option : string | number , index? : number ) => Chainable <JQuery <HTMLElement >> |
tick |
(ms : number ) => Chainable <Clock > |
toggle |
(index : number ) => Chainable <JQuery <HTMLElement >> |
toggleRadioBySelector |
(dataTestID : string , index? : number ) => Chainable <JQuery <HTMLElement >> |
type |
(dataTestID : string , keys : string , index? : number ) => Chainable <JQuery <HTMLElement >> |
typeSpecialCharacter |
(dataTestID : string , keys : string , index? : number ) => Chainable <JQuery <HTMLElement >> |
uncheck |
(dataTestID : string , index? : number ) => Chainable <JQuery <HTMLElement >> |
visit |
(url : string ) => void |
wait |
(ms : number ) => Chainable <undefined > |
waitForLastCall |
(alias : string , timeout : number ) => Chainable <undefined | Interception <any , any >> |
waitForResponse |
(alias : string ) => Chainable <Interception <any , any >> |
waitForResponses |
(alias : string , responses : number ) => Chainable <Interception <any , any >[]> |
waitUntil |
checkFunction : () => Chainable <any > | ReturnType | PromiseLike <ReturnType >, options? : WaitUntilOptions <any >) => Chainable <any > |
within |
(fn : () => void , dataTestID : string , index? : number ) => Chainable <JQuery <HTMLElement >> |
acceptConfirm: () => EventEmitter2
| Listener
Fires when your app calls the global window.confirm() method. The confirmation will be accepted.
blur: (dataTestID
: string
, index?
: number
) => Chainable
<JQuery
<HTMLElement
>>
Blur a focused element. This element must currently be in focus. If you want to ensure an element is focused before blurring, try using helper.when.focus() before helper.when.blur().
cancelConfirm: () => Cypress
Fires when your app calls the global window.confirm() method. The confirmation will be canceled.
cancelPrompt: () => EventEmitter2
| Listener
Fires when your app calls the global window.prompt() method. The prompt will be cancelled.
check: (dataTestID
: string
, index?
: number
) => Chainable
<JQuery
<HTMLElement
>>
Check checkbox(es) or radio(s). This element must be an html input element with type checkbox or radio.
clear: (dataTestID
: string
, index?
: number
) => Chainable
<JQuery
<HTMLElement
>>
Clear the value of an input or textarea
click: (dataTestID
: string
, index?
: number
) => Chainable
<JQuery
<HTMLElement
>>
Click a DOM element.
Example
clock: () => Chainable
<Clock
>
overrides native global functions related to time allowing them to be controlled synchronously via helper.when.tick() This includes controlling: setTimeout clearTimeout setInterval clearInterval Date Objects The clock starts at the unix epoch (timestamp of 0). This means that when you instantiate new Date in your application, it will have a time of January 1st, 1970.
closeAlert: () => Cypress
Fires when your app calls the global window.alert() method. The alert will be closed.
dblclick: (dataTestID
: string
, index?
: number
) => Chainable
<JQuery
<HTMLElement
>>
Double-click a DOM element.
doWithin: (fn
: () => void
, dataTestID
: string
, index?
: number
) => Chainable
<JQuery
<HTMLElement
>>
Scopes the execution of a function within an element
Example
dragAndDrop: (element
: Chainable
<JQuery
<HTMLElement
>>, targetElement
: Chainable
<JQuery
<HTMLElement
>>) => void
Drag an element and drop it in target element
Example
helper.when.dragAndDrop(
helper.get.elementByTestId('selected-item', 2),
helper.get.elementByTestId('available-items')
)
focus: (dataTestID
: string
, index?
: number
) => Chainable
<JQuery
<HTMLElement
>>
Focus on a DOM element.
Example
hover: (dataTestID
: string
, index?
: number
) => Chainable
<JQuery
<HTMLElement
>>
Fires native hover event. Yes, it can test :hover preprocessor.
Example
realClick: (dataTestID
: string
, index?
: number
) => Chainable
<JQuery
<HTMLElement
>>
Fires native system click event.
Example
realType: (dataTestID
: string
, keys
: string
, index?
: number
) => Chainable
<void
>
Runs a sequence of native press event (via cy.press) Type event is global. Make sure that it is not attached to any field.
rightclick: (dataTestID
: string
, index?
: number
) => Chainable
<JQuery
<HTMLElement
>>
Right click a DOM element.
scrollToBottom: () => Chainable
<undefined
>
Scroll to the bottom.
scrollToTop: () => Chainable
<undefined
>
Scroll to the top.
selectOption: (dataTestID
: string
, option
: string
| number
, index?
: number
) => Chainable
<JQuery
<HTMLElement
>>
Select an option with specific text, value, or index within a select html element.
Example
<select data-hook="fruit-selection">
<option value="456">apples</option>
<option value="457">oranges</option>
<option value="458">bananas</option>
</select>
helper.when.selectOption('fruit-selection', 0).should('have.value', '456')
helper.when.selectOption('fruit-selection', 'oranges').should('have.value', '457')
helper.when.selectOption('fruit-selection', 458).should('have.value', '458')
tick: (ms
: number
) => Chainable
<Clock
>
Move time after overriding a native time function with helper.when.clock(). helper.when.clock() must be called before helper.when.tick()
Example
toggle: (index
: number
) => Chainable
<JQuery
<HTMLElement
>>
Check radio(s). This element must be an html input element with type radio.
toggleRadioBySelector: (dataTestID
: string
, index?
: number
) => Chainable
<JQuery
<HTMLElement
>>
Toggle radio(s) by dataTestID This element must be an html input element with type radio.
type: (dataTestID
: string
, keys
: string
, index?
: number
) => Chainable
<JQuery
<HTMLElement
>>
Type into a DOM element, not including special characters
Example
typeSpecialCharacter: (dataTestID
: string
, keys
: string
, index?
: number
) => Chainable
<JQuery
<HTMLElement
>>
Type into a DOM element, including special characters
Example
uncheck: (dataTestID
: string
, index?
: number
) => Chainable
<JQuery
<HTMLElement
>>
Uncheck checkbox(es).
visit: (url
: string
) => void
Visit a given url
wait: (ms
: number
) => Chainable
<undefined
>
Wait for a number of milliseconds.
waitForLastCall: (alias
: string
, timeout
: number
) => Chainable
<undefined
| Interception
<any
, any
>>
Wait for a last request to complete.
waitForResponse: (alias
: string
) => Chainable
<Interception
<any
, any
>>
Wait for a specific request to complete.
waitForResponses: (alias
: string
, responses
: number
) => Chainable
<Interception
<any
, any
>[]>
Wait for multiples requests to complete.
waitUntil: checkFunction
: () => Chainable
<any
> | ReturnType
| PromiseLike
<ReturnType
>, options?
: WaitUntilOptions
<any
>) => Chainable
<any
>
Wait for something to happen in the DOM. Note! you should not have any asserts in the callback function. From cypress-wait-until documentation: you cannot put assertions inside checkFunction. There is no way to avoid a test failure if an assertion throws an error. You must manually check what the assertions would check for you. The most common case is checking that an element exists or not
Example
within: (fn
: () => void
, dataTestID
: string
, index?
: number
) => Chainable
<JQuery
<HTMLElement
>>
Scopes the execution of a function within an element
Example
Deprecated
The method should not be used anymore. Use helper.when.doWithin instead.
Methods
beforeAndAfter
▸ beforeAndAfter(): void
Returns
void