Gathering detailed insights and metrics for testcafe-react-patched
Gathering detailed insights and metrics for testcafe-react-patched
Gathering detailed insights and metrics for testcafe-react-patched
Gathering detailed insights and metrics for testcafe-react-patched
npm install testcafe-react-patched
Typescript
Module System
Node Version
NPM Version
JavaScript (71.4%)
Mustache (22.67%)
HTML (5.93%)
Total Downloads
0
Last Day
0
Last Week
0
Last Month
0
Last Year
0
MIT License
203 Stars
339 Commits
42 Forks
20 Watchers
6 Branches
21 Contributors
Updated on Mar 13, 2025
Latest Version
5.0.3
Package Id
testcafe-react-patched@5.0.3
Unpacked Size
53.32 kB
Size
12.05 kB
File Count
5
NPM Version
6.14.13
Node Version
14.17.0
Published on
Jun 20, 2023
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
This plugin provides selector extensions that make it easier to test ReactJS components with TestCafe. These extensions allow you to select page elements in a way that is native to React.
$ npm install testcafe-react-selectors
To wait until the React's component tree is loaded, add the waitForReact
method to fixture's beforeEach
hook.
1import { waitForReact } from 'testcafe-react-selectors'; 2 3fixture `App tests` 4 .page('http://react-app-url') 5 .beforeEach(async () => { 6 await waitForReact(); 7 });
The default timeout for waitForReact
is 10000
ms. You can specify a custom timeout value:
1await waitForReact(5000);
If you need to call a selector from a Node.js callback, pass the current test controller as the second argument in the waitForReact
function:
1import { waitForReact } from 'testcafe-react-selectors'; 2 3fixture `App tests` 4 .page('http://react-app-url') 5 .beforeEach(async t => { 6 await waitForReact(5000, t); 7 });
The test controller will be assigned to the boundTestRun function's option. Otherwise, TestCafe would throw the following error: ClientFunction cannot implicitly resolve the test run in context of which it should be executed
. See the TestCafe documentation for further details.
ReactSelector
allows you to select page elements by the name of the component class or the nested component element.
Suppose you have the following JSX.
1<TodoApp className="todo-app"> 2 <TodoInput /> 3 <TodoList> 4 <TodoItem priority="High" key="HighPriority">Item 1</TodoItem> 5 <TodoItem priority="Low" key="LowPriority">Item 2</TodoItem> 6 </TodoList> 7 8 <div className="items-count">Items count: <span>{this.state.itemCount}</span></div> 9</TodoApp>
To get a root DOM element for a component, pass the component name to the ReactSelector
constructor.
1import { ReactSelector } from 'testcafe-react-selectors'; 2 3const todoInput = ReactSelector('TodoInput');
To obtain a nested component or DOM element, you can use a combined selector or add DOM element's tag name.
1import { ReactSelector } from 'testcafe-react-selectors'; 2 3const TodoList = ReactSelector('TodoApp TodoList'); 4const itemsCountStatus = ReactSelector('TodoApp div'); 5const itemsCount = ReactSelector('TodoApp div span');
Warning: if you specify a DOM element's tag name, React selectors search for the element among the component's children without looking into nested components. For instance, for the JSX above the ReactSelector('TodoApp div')
selector will be equal to Selector('.todo-app > div')
.
To obtain a component by its key, use the withKey
method.
1import { ReactSelector } from 'testcafe-react-selectors'; 2 3const item = ReactSelector('TodoItem').withKey('HighPriority');
You can select elements by the component's displayName.
For instance, consider the TodoList
component whose displayName
class property is specified as follows:
1class TodoList extends React.Component { 2 // ... 3} 4 5TodoList.displayName = 'TodoList';
In this instance, you can use todo-list-display-name
to identify TodoList
.
1import { ReactSelector } from 'testcafe-react-selectors'; 2 3const list = ReactSelector('todo-list-display-name');
React selectors allow you to select elements that have a specific property value. To do this, use the withProps
method. You can pass the property and its value as two parameters or an object.
1import { ReactSelector } from 'testcafe-react-selectors'; 2 3const item1 = ReactSelector('TodoApp').withProps('priority', 'High'); 4const item2 = ReactSelector('TodoApp').withProps({ priority: 'Low' });
You can also search for elements by multiple properties.
1import { ReactSelector } from 'testcafe-react-selectors'; 2 3const element = ReactSelector('componentName').withProps({ 4 propName: 'value', 5 anotherPropName: 'differentValue' 6});
React selectors allow you to filter components by properties whose values are objects.
When the withProps
function filters properties, it determines whether the objects (property values) are strictly or partially equal.
The following example illustrates strict and partial equality.
1object1 = { 2 field1: 1 3} 4object2 = { 5 field1: 1 6} 7object3 = { 8 field1: 1 9 field2: 2 10} 11object4 = { 12 field1: 3 13 field2: 2 14}
object1
strictly equals object2
object2
partially equals object3
object2
does not equal object4
object3
does not equal object4
Prior to version 3.0.0, withProps
checked if objects are strictly equal when comparing them. Since 3.0.0, withProps
checks for partial equality. To test objects for strict equality, specify the exactObjectMatch
option.
The following example returns the componentName
component because the objProp
property values are strictly equal and exactObjectMatch
is set to true.
1// props = { 2// simpleProp: 'value', 3// objProp: { 4// field1: 'value', 5// field2: 'value' 6// } 7// } 8 9const element = ReactSelector('componentName').withProps({ 10 simpleProp: 'value', 11 objProp: { 12 field1: 'value', 13 field2: 'value' 14 } 15}, { exactObjectMatch: true })
Note that the partial equality check works for objects of any depth.
1// props = { 2// simpleProp: 'value', 3// objProp: { 4// field1: 'value', 5// field2: 'value', 6// nested1: { 7// someField: 'someValue', 8// nested2: { 9// someField: 'someValue', 10// nested3: { 11// field: 'value', 12// someField: 'someValue' 13// } 14// } 15// } 16// } 17// } 18 19 20const element = ReactSelector('componentName').withProps({ 21 simpleProp: 'value', 22 objProp: { 23 field1: 'value', 24 nested1: { 25 nested2: { 26 nested3: { 27 field: 'value' 28 } 29 } 30 } 31 } 32}, { exactObjectMatch: false })
You can search for a desired subcomponent or DOM element among the component's children using the .findReact(element)
method. The method takes the subcomponent name or tag name as a parameter.
Suppose you have the following JSX.
1<TodoApp className="todo-app"> 2 <div> 3 <TodoList> 4 <TodoItem priority="High">Item 1</TodoItem> 5 <TodoItem priority="Low">Item 2</TodoItem> 6 </TodoList> 7 </div> 8</TodoApp>
The following sample demonstrates how to obtain the TodoItem
subcomponent.
1import { ReactSelector } from 'testcafe-react-selectors'; 2 3const component = ReactSelector('TodoApp'); 4const div = component.findReact('div'); 5const subComponent = div.findReact('TodoItem');
You can call the .findReact
method in a chain, for example:
1import { ReactSelector } from 'testcafe-react-selectors'; 2 3const subComponent = ReactSelector('TodoApp').findReact('div').findReact('TodoItem');
You can also combine .findReact
with regular selectors and other) methods like .find or .withText, for example:
1import { ReactSelector } from 'testcafe-react-selectors'; 2 3const subComponent = ReactSelector('TodoApp').find('div').findReact('TodoItem');
Selectors returned by the ReactSelector
constructor are recognized as TestCafe selectors. You can combine them with regular selectors and filter with .withText, .nth, .find and other functions. To search for elements within a component, you can use the following combined approach.
1import { ReactSelector } from 'testcafe-react-selectors'; 2 3var itemsCount = ReactSelector('TodoApp').find('.items-count span');
Example
Let's use the API described above to add a task to a Todo list and check that the number of items changed.
1import { ReactSelector } from 'testcafe-react-selectors'; 2 3fixture `TODO list test` 4 .page('http://localhost:1337'); 5 6test('Add new task', async t => { 7 const todoTextInput = ReactSelector('TodoInput'); 8 const todoItem = ReactSelector('TodoList TodoItem'); 9 10 await t 11 .typeText(todoTextInput, 'My Item') 12 .pressKey('enter') 13 .expect(todoItem.count).eql(3); 14});
As an alternative to testcafe snapshot properties, you can obtain state
, props
or key
of a ReactJS component.
To obtain component's properties, state and key, use the React selector's .getReact()
method.
The .getReact()
method returns a client function. This function resolves to an object that contains component's properties (excluding properties of its children
), state and key.
1const reactComponent = ReactSelector('MyComponent'); 2const reactComponentState = await reactComponent.getReact(); 3 4// >> reactComponentState 5// 6// { 7// props: <component_props>, 8// state: <component_state>, 9// key: <component_key> 10// }
The returned client function can be passed to assertions activating the Smart Assertion Query mechanism.
Example
1import { ReactSelector } from 'testcafe-react-selectors'; 2 3fixture `TODO list test` 4 .page('http://localhost:1337'); 5 6test('Check list item', async t => { 7 const el = ReactSelector('TodoList'); 8 const component = await el.getReact(); 9 10 await t.expect(component.props.priority).eql('High'); 11 await t.expect(component.state.isActive).eql(false); 12 await t.expect(component.key).eql('componentID'); 13});
As an alternative, the .getReact()
method can take a function that returns the required property, state or key. This function acts as a filter. Its argument is an object returned by .getReact()
, i.e. { props: ..., state: ..., key: ...}
.
1ReactSelector('Component').getReact(({ props, state, key }) => {...})
Example
1import { ReactSelector } from 'testcafe-react-selectors'; 2 3fixture `TODO list test` 4 .page('http://localhost:1337'); 5 6test('Check list item', async t => { 7 const el = ReactSelector('TodoList'); 8 9 await t 10 .expect(el.getReact(({ props }) => props.priority)).eql('High') 11 .expect(el.getReact(({ state }) => state.isActive)).eql(false) 12 .expect(el.getReact(({ key }) => key)).eql('componentID'); 13});
The .getReact()
method can be called for the ReactSelector
or the snapshot this selector returns.
Use the generic ReactComponent
type to create scalable selectors in TypeScript.
Pass the props
object as the type argument to ReactComponent
to introduce a type for a specific component.
1type TodoItem = ReactComponent<{ id: string }>;
You can then pass the created TodoItem
type to the withProps
and getReact
generic methods.
1const component = ReactSelector('TodoItem'); 2type TodoItem = ReactComponent<{ id: string }>; 3 4const item1 = component.withProps<TodoItem>('id', 'tdi-1'); 5const itemId = component.getReact<TodoItem>(({ props }) => props.id);
Example
1import { ReactSelector, ReactComponent } from 'testcafe-react-selectors'; 2 3fixture`typescript support` 4 .page('http://react-page-example.com') 5 6test('ReactComponent', async t => { 7 const todoList = ReactSelector('TodoList'); 8 type TodoListComponent = ReactComponent<{ id: string }>; 9 10 const todoListId = todoList.getReact<TodoListComponent>(({ props }) => props.id); 11 12 await t.expect(todoListId).eql('ul-item'); 13});
If a component's props and state include other composite types, you can create your own type definitions for them. Then pass these definitions to ReactComponent
as type arguments.
The following example shows custom Props
and State
type definitions. The State
type uses another composite type - Option
.
1import { ReactComponent } from 'testcafe-react-selectors'; 2 3interface Props { 4 id: string; 5 text: string; 6} 7 8interface Option { 9 id: number; 10 title: string; 11 description: string; 12} 13 14interface State { 15 optionsCount: number; 16 options: Option[]; 17} 18 19export type OptionReactComponent = ReactComponent<Props, State>;
testcafe-react-selectors
support ReactJS starting with version 16. To check if a component can be found, use the react-dev-tools extension.
Search for a component starts from the root React component, so selectors like ReactSelector('body MyComponent')
will return null
.
ReactSelectors need class names to select components on the page. Code minification usually does not keep the original class names. So you should either use non-minified code or configure the minificator to keep class names.
For babel-minify
, add the following options to the configuration:
1{ keepClassName: true, keepFnName: true }
In UglifyJS, use the following configuration:
1{ 2 compress: { 3 keep_fnames: true 4 }, 5 6 mangle: { 7 keep_fnames: true 8 } 9}
No vulnerabilities found.
Reason
no dangerous workflow patterns detected
Reason
no binaries found in the repo
Reason
license file detected
Details
Reason
Found 6/13 approved changesets -- score normalized to 4
Reason
SAST tool is not run on all commits -- score normalized to 2
Details
Reason
dependency not pinned by hash detected -- score normalized to 1
Details
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
detected GitHub workflow tokens with excessive permissions
Details
Reason
security policy file not detected
Details
Reason
project is not fuzzed
Details
Reason
18 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