Gathering detailed insights and metrics for cypress-react-selector
Gathering detailed insights and metrics for cypress-react-selector
Gathering detailed insights and metrics for cypress-react-selector
Gathering detailed insights and metrics for cypress-react-selector
⚡ cypress plugin to locate react elements by component, props and state
npm install cypress-react-selector
Typescript
Module System
Node Version
NPM Version
97.8
Supply Chain
100
Quality
79.9
Maintenance
100
Vulnerability
100
License
JavaScript (98.95%)
HTML (1.05%)
Total Downloads
0
Last Day
0
Last Week
0
Last Month
0
Last Year
0
MIT License
275 Stars
438 Commits
28 Forks
3 Watchers
15 Branches
14 Contributors
Updated on Feb 20, 2025
Latest Version
3.0.0
Package Id
cypress-react-selector@3.0.0
Unpacked Size
39.02 kB
Size
12.37 kB
File Count
15
NPM Version
6.14.11
Node Version
12.22.0
Cumulative downloads
Total Downloads
Last Day
0%
NaN
Compared to previous day
Last Week
0%
NaN
Compared to previous week
Last Month
0%
NaN
Compared to previous month
Last Year
0%
NaN
Compared to previous year
:warning: cypress-react-selector 3.x supports cypress 10+
cypress-react-selector is a lightweight plugin to help you to locate web elements in your REACT app using components, props and states. This extension allow you to select elements in a way that is native to React. Designed to help developers in component, integration and E2E testing.
Internally, cypress-react-selector uses a library called resq to query React's VirtualDOM in order to retrieve the nodes.
1npm i --save-dev cypress-react-selector
Update cypress/support/e2e.js
and cypress/support/component.js
file to include the cypress-react-selector commands by adding:
1import 'cypress-react-selector';
component-index.html
(Component Testing only)In order for waitForReact()
to work appropriately in component testing, replace the div
inside component-index.html
with this:
1<div id="__cy_root" data-cy-root></div>
Add the following to tsconfig.json
:
1{ 2 "compilerOptions": { 3 "sourceType": "module", 4 "types": ["node", "cypress", "cypress-react-selector"] 5 } 6}
The following is a sample cypress.config.js
file that sets up Cypress e2e and component testing to work with this plugin:
1const { defineConfig } = require('cypress'); 2 3module.exports = defineConfig({ 4 video: false, 5 screenshotOnRunFailure: false, 6 env: { 7 'cypress-react-selector': { 8 root: '#__cy_root', 9 }, 10 }, 11 e2e: { 12 setupNodeEvents() {}, 13 specPattern: 'cypress/e2e/**/*.cy.{js,ts,jsx,tsx}', 14 excludeSpecPattern: ['**/__snapshots__/*', '**/__image_snapshots__/*'], 15 }, 16 component: { 17 setupNodeEvents() {}, 18 specPattern: 'cypress/component/**/*.cy.{js,ts,jsx,tsx}', 19 excludeSpecPattern: ['**/__snapshots__/*', '**/__image_snapshots__/*'], 20 devServer: { 21 framework: 'create-react-app', 22 bundler: 'webpack', 23 }, 24 }, 25});
Note: if you're not using component
testing, you can remove that from the config.
Lets take this example REACT APP:
1// imports 2 3const MyComponent = ({ someBooleanProp }) => ( 4 <div>My Component {someBooleanProp ? 'show this' : ''} </div> 5); 6 7const App = () => ( 8 <div id="root"> 9 <MyComponent /> 10 <MyComponent someBooleanProp={true} /> 11 </div> 12); 13 14ReactDOM.render(<App />, document.getElementById('root'));
cypress-react-selector
needs the react root css-selector
information to identify
In order to make sure that the React component tree has loaded, add the waitForReact
call immediately after loading a page. Here is an example where it's done in the fixture's before
hook.
1before(() => { 2 cy.visit('http://localhost:3000/myApp'); 3 cy.waitForReact(1000, '#root'); // 1000 is the timeout in milliseconds, you can provide as per AUT 4});
NOTE : The Best Configuration for React root is to declare it as an env
variable
We always recommend to declare the react root
as a env
variable in the cypress.config.js
file. It is a best approach rather than passing react root information to waitForReact
method every time.
As an example:
1{ 2 "env": { 3 "cypress-react-selector": { 4 "root": "#root" 5 } 6 } 7}
If you choose to declare the root selector
as a configuration
, then you will have the freedom to call waitForReact
method without passing the root parameter.
1before(() => { 2 cy.visit('http://localhost:3000/myApp'); 3 cy.waitForReact(); 4});
NOTE: If you are using Webpack with your project, you may need to manually pass in the resq module path.
There's an optional parameter in waitForReact
that can be passed in at runtime.
This should be the path of the resq
entrypoint
1before(() => { 2 cy.visit('http://localhost:3000/myApp'); 3 cy.waitForReact(1000, '#root', 'node_modules/resq/dist/index.js'); // Manually passing in the resq module path 4});
You should have React Develop Tool installed to spy and find out the component name as sometimes components can go though modifications. Once the React gets loaded, you can easily identify an web element by react component name:
1cy.react('MyComponent'); 2 3// you can have your assertions chained like 4it('it should validate react selection with component name', () => { 5 cy.react('MyComponent').should('have.length', '1'); 6});
You can filter the REACT components by its props and states like below:
1cy.react(componentName, reactOpts); 2 3// ReactOpts: 4//{ 5// props: { someProp: someValue }, 6// state: { someState: someValue }, 7// exact: boolean 8//} 9 10// for the example APP 11cy.react('MyComponent', { props: { name: 'John' } });
exact
flagIf you are in need of matching exactly every property and value in the object (or nested objects), you can pass the exact flag to the cy.react
or cy.getReact
function:
1cy.react('MyComponent', { props: { name: 'John' }, exact: true });
Make sure all the props
and/or state
are listed while using this flag
, if not matched it will return undefined
You can select your components by partial name use a wildcard selectors:
1// Partial Match 2cy.react('My*', { props: { name: 'John' } }); 3 4// Entire Match 5cy.react('*', { props: { name: 'John' } }); // return all components matched with the prop
Let's suppose you have an Form component
1<Form> 2 <Field name="email" type="email" component={MyTextInput} /> 3 <ErrorMessage name="email" component="div" /> 4 <br /> 5 <Field type="password" name="password" component={MyTextInput} /> 6 <ErrorMessage name="password" component="div" /> 7 <br /> 8 <button type="submit" disabled={isSubmitting}> 9 Submit 10 </button> 11</Form>
And MyTextInput component is developed as:
1const MyTextInput = (props) => { 2 const { field, type } = props; 3 4 return ( 5 <input {...field} type={type} placeholder={'ENTER YOUR ' + field.name} /> 6 ); 7};
then you can use cypress-react-selector to identify the element with nested props
1it('enter data into the fields', () => { 2 cy.react('MyTextInput', { props: { field: { name: 'email' } } }).type( 3 'john.doe@cypress.com' 4 ); 5 cy.react('MyTextInput', { props: { field: { name: 'password' } } }).type( 6 'whyMe?' 7 ); 8});
Let's take same Form example
You can get the React properties from a React element and validate the properties run time.
1// set the email in the form 2cy.react('MyTextInput', { props: { field: { name: 'email' } } }).type( 3 'john.doe@cypress.com' 4); 5 6// validate the property runtime 7cy.getReact('MyTextInput', { props: { field: { name: 'email' } } }) 8 .getProps('fields.value') 9 .should('eq', 'john.doe@cypress.com'); 10 11// to get all the props, simply do not pass anything in getProps() method 12cy.getReact('MyTextInput', { props: { field: { name: 'email' } } }).getProps();
1cy.getReact('MyTextInput', { 2 props: { field: { name: 'email' } }, 3}).getCurrentState(); // can return string | boolean | any[] | {}
You can configure the timeouts in the cypress.config.js
configuration file. Alternatively, you can also pass the timeout
as a object literal in the react commands like,
1cy.react('MyComponent', { options: { timeout: 50000 } });
cy.react
returns DOM element, so you can fetch the indexed node by .eq(index), like:1cy.react('MyComponent').eq(0).click();
cy.getReact()
return RESQ node, so you can't fetch it through .eq()
. You need to use .nthNode(index)
, like:1cy.getReact('MyComponent') 2 .nthNode(0) 3 .getProps('name') 4 .should('eq', 'First Item');
You can chain react-selector
queries like:
.find()
-1cy.react('FormComponent').find('input').type('buy milk'); 2cy.react('FormComponent').find('button').click();
HTMLElements
by chained react
queries1cy.react('MyComponent', { props: { name: 'Bob' } }) 2 .react('MyAge') 3 .should('have.text', '50');
react props and states
by chained getReact
query1cy.getReact('MyComponent', { props: { name: 'Bob' } }) 2 .getReact('MyAge') 3 .getProps('age') 4 .should('eq', '50');
:warning: Fluent commands are not working in some special cases. It is being tracked here
Credit goes to Gleb Bahmutov for drafting how cypress-react-selector
can be used in react component testing
here
Credit goes to gregfenton for presenting a formik form
example that uses Cypress-React-Selector
. Checkout the work here
[If you have a cool project, feel free to portray here]
React-Dev-Tool — You can inspect the DOM element by simply pressing the f12. But, to inspect REACT components and props, you need to install the chrome plugin.
you can raise any issue here
Any pull request is welcome.
If it works for you , give a Star! :star:
No vulnerabilities found.
Reason
no binaries found in the repo
Reason
license file detected
Details
Reason
Found 3/12 approved changesets -- score normalized to 2
Reason
0 commit(s) and 0 issue activity found in the last 90 days -- score normalized to 0
Reason
no effort to earn an OpenSSF best practices badge detected
Reason
security policy file not detected
Details
Reason
project is not fuzzed
Details
Reason
SAST tool is not run on all commits -- score normalized to 0
Details
Reason
94 existing vulnerabilities detected
Details
Score
Last Scanned on 2025-07-07
The Open Source Security Foundation is a cross-industry collaboration to improve the security of open source software (OSS). The Scorecard provides security health metrics for open source projects.
Learn More