-
Notifications
You must be signed in to change notification settings - Fork 292
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
feat(test): Playwright testing integration #1250
Merged
Merged
Conversation
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
✅ Deploy Preview for cornerstone-3d-docs canceled.
|
sedghi
changed the title
[WIP] Playwright testing integration
Playwright testing integration
May 16, 2024
IbrahimCSAE
changed the title
Playwright testing integration
feat(test): Playwright testing integration
May 16, 2024
sedghi
approved these changes
May 17, 2024
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Great stuff, thanks
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Writing PlayWright Tests
Our Playwright tests are written using the Playwright test framework. We use these tests to test our examples and ensure that they are working as expected which in turn ensures that our packages are working as expected.
In this guide, we will show you how to write Playwright tests for our examples, create new examples and test against them.
Testing against existing examples
If you would like to use an existing example, you can find the list of examples in the
utils/ExampleRunner/example-info.json
file. You can use theexampleName
property to reference the example you would like to use. for example, if you would like to use theannotationToolModes
example, you can use the following code snippet:Testing against new examples
Our playwright tests run against our examples, if you would like to add a new example, you can add it to the
examples
folder in the root of of the respective package, for example,packages/tools/examples/{your_example_name}/index.ts
, and then add then register it inutils/ExampleRunner/example-info.json
file under it's correct category, for example if its tool related, it can go into the existingtools-basic
category. If you don't find a category that fits your example, you can create a new category and add it to thecategories
object in theexample-info.json
file.Once this is done, you can write a test against the example by using the
visitExample
function in thetests/utils/visitExample.ts
file. For example, if you would like to write a test against theyour_example_name
example, you can use the following code snippet:This will also make your example appear in our docs page, so that users can see how to use the example, so you are adding double value by adding a new example.
Screenshots
A good way to check your tests is working as expected is to capture screenshots at different stages of the test. You can use our
checkForScreenshot
function located intests/utils/checkForScreenshot.ts
to capture screenshots. You should also plan your screenshots in advance, screenshots need to be defined in thetests/utils/screenshotPaths.ts
file. For example, if you would to capture a screenshot after a measurement is added, you can define a screenshot path like this:It's okay if the screenshot doesn't exist yet, this will be dealt with in the next step. Once you have defined your screenshot path, you can use the
checkForScreenshot
function in your test to capture the screenshot. For example, if you would like to capture a screenshot of thecornerstone-canvas
element after a measurement is added, you can use the following code snippet:The test will automatically fail the first time you run it, it will however generate the screenshot for you, you will notice 3 new entries in the
tests/screenshots
folder, underchromium/your-example.spec.js/measurementAdded.png
,firefox/your-example.spec.js/measurementAdded.png
andwebkit/your-example.spec.js/measurementAdded.png
folders. You can now run the test again and it will use those screenshots to compare against the current state of the example. Please verify that the ground truth screenshots are correct before committing them or testing against them.Simulating mouse drags
If you would like to simulate a mouse drag, you can use the
simulateDrag
function located intests/utils/simulateDrag.ts
. You can use this function to simulate a mouse drag on an element. For example, if you would like to simulate a mouse drag on thecornerstone-canvas
element, you can use the following code snippet:Our simulate drag utility can simulate a drag on any element, and avoid going out of bounds. It will calculuate the bounding box of the element and ensure that the drag stays within the bounds of the element. This should be good enough for most tools, and better than providing custom x, and y coordinates which can be error prone and make the code difficult to maintain.
Running the tests
After you have wrote your tests, you can run them by using the following command:
If you want to use headed mode, you can use the following command:
You will see the test results in your terminal, if you want an indepth report, you can use the following command:
Serving the examples manually for development
By default, when you run the tests, it will call the
yarn build-and-serve-static-examples
command to serve the examples first, then run the tests, if you would like to serve the examples manually, you can use the same command. The examples will be available athttp://localhost:3000
. This could speed up your development process since playwright will skip the build and serve step and use the existing server on port 3000.Playwright VSCode Extension and Recording Tests
If you are using VSCode, you can use the Playwright extension to help you write your tests. The extension provides a test runner and many great features such as picking a locator using your mouse, recording a new test, and more. You can install the extension by searching for
Playwright
in the extensions tab in VSCode or by visiting the Playwright extension page.