Optional
options: CypressHelperOptions = {}The get property will hold methods which will give our tests access to the “output” of the component in a “black box” fashion
Get stub as Cypress.Chainable
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
```ts
then(serviceMock.function).shouldHaveBeenCalled();
Get one or more DOM elements by selector.
Optional
attribute: string = ...Get an element with shape="filter-grid"
<clr-icon shape="filter-grid"></clr-icon>
helper.get.bySelector("filter-grid", "shape")
Get the current URL of the page that is currently active.
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 ***
Optional
index: numberget.element('.list>li', 3) // Yield the <li>'s in <.list>
get.element('ul li:first')
get.element('.dropdown-menu')
Get one or more DOM elements by attribute.
Get an element with shape="filter-grid"
<clr-icon shape="filter-grid"></clr-icon>
helper.get.byAttribute("shape", "filter-grid")
Get one or more DOM elements by selector. Same as bySelector
Optional
attribute: string = ...Get an element with shape="filter-grid"
<clr-icon shape="filter-grid"></clr-icon>
helper.get.elementBySelector("filter-grid", "shape")
Get A DOM element at a specific index from elements.
Optional
index: numberhelper.when.dragAndDrop(
helper.get.elementByTestId('selected-item', 2),
helper.get.elementByTestId('available-items')
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 ***
Optional
index: numberthen(helper.get.elementByText("Avamar")).shouldExist();
Optional
index: numberthen(helper.get.elementsAttribute('avatar-picture', 'style')).shouldInclude('background-image: url("assets/avatar/def-user-male.png")')
Returns element's computed style, including pseudo elements
Optional
index: numberOptional
pseudoElement: stringhelper.get.elementsComputedStyle('element-test-id', 0, ':before')
Get element's property value
Optional
index: numberget.elementsProperty("image", "height")
Returns element's style attribute
Optional
index: numberget.elementsStyleAttribute("button-data-hook", "background-color")
Optional
index: numberthen(helper.get.elementsText("parent-job-name", 3)).shouldInclude("Job 3 Name")
Returns specific environment variable or undefined
// Keeping password in cypress.config file
e2e: {
env: {
password: "Changeme@1",
}
}
// using password during test
helper.get.env("password");
Get fixture
given.fixture("user.json", "user");
then(
get.fixture("user").shouldDeepNestedInclude({
name: "Jane Doe",
id: "1234",
nested: {
attr1: "something",
attr2: "the other thing"
}
})
);
Get the element currently focused in the document.
Get value of input element
Optional
index: numberthen(helper.get.inputValue('credentials-password')).shouldEqual("initial password");
Get A DOM element at a specific index from elements.
Optional
index: numberOptional
attribute: string = ...// Get the 3rd checkbox
helper.get.nthBySelector("checkbox", 3, "type")
Get number of elements with a specific dataTestID
then(helper.get.numberOfElements("migrated-vcenter")).shouldEqual(2);
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.
Get intercepted request's header
Get intercepted request's query param
Get intercepted request's url
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.
Get intercepted response's header
Get spy by alias
Get spy by function name alias
Get stub by alias
Get the window object of the page that is currently active.
helper.get.window().then((win) => { win.localStorage.getItem("key")}
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.
Load a fixture
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"
}
});
});
Use intercept() to intercept HTTP requests and responses
Optional
method: stringhelper.given.intercept("/streets/sprite.png", "streetSprite");
Use interceptAndMockResponse to stub and intercept HTTP requests and responses.
Optional
alias?: stringOptional
method?: stringOptional
response?: Object// adds token to response header
helper.given.interceptAndMockResponse({
url: '** /sysmgmt/2015/bmc/session',
response: {
headers:{
'XSRF-Token': 'token',
},
},
alias: 'login'
})
// mocks response to login request
helper.given.interceptAndMockResponse({
method: "POST",
url: "** /login",
alias: "login",
response : {
token: 'token'
}
})
// mocks network error
helper.given.interceptAndMockResponse({
method: "POST",
url: "** /avamars",
alias: "avamar",
response : {
forceNetworkError: true
}
})
// mocks missing image
helper.given.interceptAndMockResponse({
method: "POST",
url: "** /image.png",
alias: "image",
response: { headers: 404 }
})
// using a fixture
helper.given.interceptAndMockResponse({
url: "** /shellygo/whatever**",
response: { fixture: "user.json" },
alias: "whatever"
});
Returns a new spy function, and creates an alias for the newly created spy
spy name
Spy on a method and create an alias for the spy
object containing function to spy on
function to spy on
given.spyOnObject(window, "alert");
alert("whatever");
then(helper.get.spyFromFunction(window.alert)).shouldHaveBeenCalledWith("whatever");
// Or
then(helper.get.spy("alert")).shouldHaveBeenCalledTimes(1);
given.spyOnObject(serviceMock, "functionName");
serviceMock.functionName();
then(helper.get.spyFromFunction(serviceMock.functionName)).shouldHaveBeenCalledTimes(1);
Replace a function, record its usage and control its behavior.
Optional
alias: stringgiven.stub("alias");
then(get.spy("alias")).shouldHaveBeenCalled();
Stub an object's method and create an alias for the stub
object containing function to stub
function to stub
//stubbing a service method
helper.given.stubObjectMethod(serviceMock, "functionName").returns(3);
//stubbing setters and getters
helper.given.stubObjectMethod(serviceMock, "count").get(() => 3).set(() => {});
Creates a new object with the given functions as the prototype and stubs all implemented functions.
Rest
...args: any[]const serviceMock = helper.given.stubbedInstance(Service);
class Service {
public func1() {...}
public get prop1() {...}
}
const serviceMock = helper.given.stubbedInstance(Service, {prop1: 3});
helper.given.stubbedInstance(Router, { events: new Observable() })
helper.given.stubbedInstance(
PokemonService,
{
pokemonTypes: new BehaviorSubject<NamedAPIResource[]>([]),
pokemons: new BehaviorSubject<BetterPokemon[]>([]),
}
)
Creates a new object with the given functions as the prototype and stubs all functions.
const serviceMock = helper.given.stubbedInterface<IService>("IService");
interface IService {
propertyFunc: (int: number) => number
get prop1() : number
}
const serviceMock = helper.given.stubbedInterface<IService>("IService", {prop1: 3});
Private
Readonly
optionsThe when property will hold methods of “events” which will take place like render, click, hover, etc.
Fires when your app calls the global window.confirm() method. The confirmation will be accepted.
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().
Optional
index: numberFires when your app calls the global window.confirm() method. The confirmation will be canceled.
Fires when your app calls the global window.prompt() method. The prompt will be cancelled.
Check checkbox(es) or radio(s). This element must be an html input element with type checkbox or radio.
Optional
index: numberClear the value of an input or textarea
Optional
index: numberClick a DOM element.
Optional
index: number<button data-cy="move-right">Move</button>
helper.when.click('move-right')
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.
Fires when your app calls the global window.alert() method. The alert will be closed.
Double-click a DOM element.
Optional
index: numberScopes the execution of a function within an element
Optional
index: numberhelper.when.doWithin(() => when.click('button-test-id'), 'button-row', 2)
Drag an element and drop it in target element
element to be dragged
target of drag operation
helper.when.dragAndDrop(
helper.get.elementByTestId('selected-item', 2),
helper.get.elementByTestId('available-items')
)
Focus on a DOM element.
Optional
index: numberhelper.when.focus('credentials-password')
Fires native hover event. Yes, it can test :hover preprocessor.
Optional
index: numberhelper.when.hover('consent-terms-agree')
Fires native system click event.
Optional
index: number<button data-cy="move-right">Move</button>
helper.when.realClick('move-right')
Runs a sequence of native press event (via cy.press) Type event is global. Make sure that it is not attached to any field.
Optional
index: numberRight click a DOM element.
Optional
index: numberScroll to the bottom.
Select an option with specific text, value, or index within a select html element.
Optional
index: number<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')
Move time after overriding a native time function with helper.when.clock(). helper.when.clock() must be called before helper.when.tick()
helper.when.clock();
helper.when.click('login-button');
helper.when.tick(2000);
Check radio(s). This element must be an html input element with type radio.
Toggle radio(s) by dataTestID This element must be an html input element with type radio.
Optional
index: numberType into a DOM element, not including special characters
Optional
index: numberhelper.when.type('credentials-password', 'new password')
Type into a DOM element, including special characters
Optional
index: numberhelper.when.typeSpecialChar('credentials-password', '{backspace}')
Uncheck checkbox(es).
Optional
index: numberVisit a given url
Wait for a number of milliseconds.
Wait for a last request to complete.
Wait for a specific request to complete.
Wait for multiples requests to complete.
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
Optional
options: WaitUntilOptions<any>helper.when.waitUntil(() =>
helper.get.elementByTestId('element-test-id', index)
);
Scopes the execution of a function within an element
Optional
index: numberhelper.when.within(() => when.click('button-test-id'), 'button-row', 2)
The method should not be used anymore. Use helper.when.doWithin instead.
Private
isPrivate
isPrivate
waitGenerated using TypeDoc
Classdes
CypressHelper exposes the following public properties: