Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use ReactDOM Test Selector API in DevTools e2e tests #22978

Merged
merged 1 commit into from
Dec 21, 2021

Conversation

bvaughn
Copy link
Contributor

@bvaughn bvaughn commented Dec 16, 2021

Builds on top of the existing Playwright tests to plug in the test selector API.

My goals in doing this are to...

  1. Experiment with the new API to see what works and what doesn't.
  2. Add some test selector attributes (and remove DOM-structure based selectors).
  3. Focus the tests on DevTools itself (rather than the test app).

I also took this opportunity to add a few new test cases– like named hooks, editable props, component search, and profiling- just to play around more with the Playwright API.

Relates to #22646


The thing I'm least happy with about this PR is that the selectors lean pretty heavily on data-testname and innerText attributes (which is something we could do with other testing frameworks). The createComponentSelector selector would be particularly interesting to use, but how do we access the (same) function component type rendered by React? Probably worth refactoring and exploring this further with a future PR, but at least this one gives us some starting point?

@facebook-github-bot facebook-github-bot added CLA Signed React Core Team Opened by a member of the React Core Team labels Dec 16, 2021
@@ -132,9 +132,10 @@ export default function Element({data, index, style}: Props) {
className={className}
onMouseEnter={handleMouseEnter}
onMouseLeave={handleMouseLeave}
onMouseDown={handleMouseDown}
onClick={handleClick}
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This change made it so e2e tests could select a row (element) and then click on it to inspect props.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can e2e tests not simulate mouse down?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I can simulate a click by calling element.click() but to simulate a mouse-down I'd have to make a fake event and dispatch it etc. Seems dirtier :D

@bvaughn
Copy link
Contributor Author

bvaughn commented Dec 16, 2021

Note that some things are more awkward than I wish they were.

For example, the test selector API lets us get a handle on the Components search input, but programmatically setting its value doesn't cause a change event to fire, and Playwright/Puppeteer don't provide any helper methods for typing that are element-based. (They're all selector based.) So the only way (I can think of) to make the two play together would be to do something hacky like:

  1. Select the element using ReactDOM
  2. Add a unique ID
  3. Pass that ID to Playwright's fill method to type into the field

This seems like a pretty rough edge though.

@akgupta0777
Copy link
Contributor

I tried playwright experimental react selectors to achieve filling and clicking options on react components. React selectors just need component name.
Example if app contains input component.
Then you can do page.fill('_react=input',SOME_VALUE)

But unfortunately this feature of playwright didn't work on apps with iframes.

Copy link

@Amir-Alipour Amir-Alipour left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

great

Copy link
Contributor

@akgupta0777 akgupta0777 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Seems like I have to read the whole gist properly to understand how it works. I have left some questions and thoughts for you to answer😄

@bvaughn bvaughn force-pushed the devtools-e2e-tests branch 5 times, most recently from 5ffeb8d to 2970efc Compare December 20, 2021 14:11
Builds on top of the existing Playwright tests to plug in the test selector API.

My goals in doing this are to...
1. Experiment with the new API to see what works and what doesn't.
2. Add some test selector attributes (and remove DOM-structure based selectors).
3. Focus the tests on DevTools itself (rather than the test app).

I also took this opportunity to add a few new test types like named hooks, component search, and profiler– just to play around with the Playwright API.
* @flow
*/

import * as React from 'react';
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No new JSX transform? 🥺

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should update the shell for this! Wanna do it in a follow up PR?

@@ -132,9 +132,10 @@ export default function Element({data, index, style}: Props) {
className={className}
onMouseEnter={handleMouseEnter}
onMouseLeave={handleMouseLeave}
onMouseDown={handleMouseDown}
onClick={handleClick}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can e2e tests not simulate mouse down?

@bvaughn bvaughn merged commit a4ead70 into facebook:main Dec 21, 2021
@bvaughn bvaughn deleted the devtools-e2e-tests branch December 21, 2021 16:58
zhengjitf pushed a commit to zhengjitf/react that referenced this pull request Apr 15, 2022
Builds on top of the existing Playwright tests to plug in the test selector API: https://gist.github.com/bvaughn/d3c8b8842faf2ac2439bb11773a19cec

My goals in doing this are to...
1. Experiment with the new API to see what works and what doesn't.
2. Add some test selector attributes (and remove DOM-structure based selectors).
3. Focus the tests on DevTools itself (rather than the test app).

I also took this opportunity to add a few new test cases– like named hooks, editable props, component search, and profiling- just to play around more with the Playwright API.

Relates to issue facebook#22646
nevilm-lt pushed a commit to nevilm-lt/react that referenced this pull request Apr 22, 2022
Builds on top of the existing Playwright tests to plug in the test selector API: https://gist.github.com/bvaughn/d3c8b8842faf2ac2439bb11773a19cec

My goals in doing this are to...
1. Experiment with the new API to see what works and what doesn't.
2. Add some test selector attributes (and remove DOM-structure based selectors).
3. Focus the tests on DevTools itself (rather than the test app).

I also took this opportunity to add a few new test cases– like named hooks, editable props, component search, and profiling- just to play around more with the Playwright API.

Relates to issue facebook#22646
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
CLA Signed React Core Team Opened by a member of the React Core Team
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants