Security News
tea.xyz Spam Plagues npm and RubyGems Package Registries
Tea.xyz, a crypto project aimed at rewarding open source contributions, is once again facing backlash due to an influx of spam packages flooding public package registries.
cypress-macros
Advanced tools
Readme
This package contains a Cypress command called evalMacros
that makes it
easier to write tests for web applications whose test data is generated
dynamically. By binding API responses to cypress @
aliases, you can
refer to dynamic data when verifying the contents of the DOM. For
example, if the logged-in user is bound to the @user
alias, you could
check for a greeting:
cy.evalMacros('Hello, {@user.firstName}')
.then(greeting => cy.contains('.header div', greeting))
The @
is optional when referring to a Cypress alias; in the example above,
{user.firstName}
would yield the same result.
In addition to Cypress aliases, this package supports a system of lightweight
macro variables for values that don't need the full power of Cypress aliases
(asynchrony, rebinding, etc). Macro variables begin with a $
to distinguish
them from Cypress variables:
cy.evalMacros('I last saw {@user.nickname} on {$today.dayOfWeek}).then(
seen => cy.contains(seen)
)
The $
is mandatory when referencing a macro variable in order to distinguish
them from Cypress aliases.
Macro variables are good for values such as "today's date" that vary between test runs but are known before the tests begin executing. You can register as many variables as you want, and you decide how long the variables remain in scope by calling a reset function at the appropriate time.
If your variables are expected to change between test cases, we
strongly suggest that you hook the Cypress test:before:run
event
to reset the variables before every test case (see sample code below).
The default behavior is to instantiate them the first time evalMacros
is called and never to reset them, which makes them act more like
constants than variables.
In cypress/support/commands.js
, add the custom commands:
import { commands as macros } from 'cypress-macros';
macros.add()
You can rename the custom command to something different if desired, or even
overwrite the contains
built-in command so that it automatically
interpolates macro expressions.
If you want to use macro variables in addition to Cypress variables, we
recommend that you create a cypress/support/variables.js
to define
macro variables and install a required Cypress event hook.
import { variables } from 'cypress-macros';
// Optional: recompute all variables before every test case to avoid
// stale values.
Cypress.on("test:before:run", () => {
variables.reset();
});
variables.add("$today", () => {
return {
dayOfWeek: new Date().getDay(),
year: new Date().getFullYear()
};
});
Don't forget to import "./variables"
in cypress/support/index.js
!
If you are using cypress-cucumber-preprocessor, you can define a custom parameter type that allows you to use a macro as a placeholder in a step definition.
In a step definition file named _setup.js
(so it runs first), place the
following code:
import {canonicalize} from 'cypress-macros/parser'
defineParameterType({
name: "macro",
regexp: /\{(.*)\}/,
transformer: s => canonicalize(s)
})
Because macros are evaluated asynchronously, you will still need to evaluate
the macro in each step definition where it is used by calling evalMacros
on your step parameters, but the use of curly braces allows for some nice
syntactic sugar in your Cucumber features:
When I click the link {@user.nickname}
This avoids the excessive puncutation that would result if you used a {string}
to capture macro expressions (When I do "{@foo}"
).
FAQs
Cypress commands to interpolate aliases & their properties into strings and arrays. Pairs well with cucumber-js.
The npm package cypress-macros receives a total of 458 weekly downloads. As such, cypress-macros popularity was classified as not popular.
We found that cypress-macros demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 2 open source maintainers collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
Tea.xyz, a crypto project aimed at rewarding open source contributions, is once again facing backlash due to an influx of spam packages flooding public package registries.
Security News
As cyber threats become more autonomous, AI-powered defenses are crucial for businesses to stay ahead of attackers who can exploit software vulnerabilities at scale.
Security News
UnitedHealth Group disclosed that the ransomware attack on Change Healthcare compromised protected health information for millions in the U.S., with estimated costs to the company expected to reach $1 billion.