Reuse Playwright Code across Files and Tests with Fixtures

  Рет қаралды 27,451

Checkly

Checkly

Күн бұрын

Learn how to leverage Playwright test fixtures to DRY your code and reuse it across tests and spec files.
Playwright docs for fixtures: playwright.dev/docs/test-fixt...

Пікірлер: 81
@3VAudioVideo
@3VAudioVideo 4 ай бұрын
Wow! This was great! So hard to find good Playwright videos like there are for Selenium (Java, C#, Python). A few videos are out there for creating setup and teardown, but they are in the same file as the testcases are. Your video went at a good face pace and while watching it I started to notice it was about to end. You didn't yet show how to put the code in a separate file so I thought it wasn't going to be covered , but you covered it! Thanks so much!
@ChecklyHQ
@ChecklyHQ 4 ай бұрын
Thank you for the kind words. Happy the video has been valuable. 💙
@owcaandroid
@owcaandroid 6 ай бұрын
My God this is what I was looking for! That's funny because I didn't find this clip at once, I stumbled on the clip about parallelism because google pointed out me that topic for my question. Thank you very much, your examples are very clear and straightforward
@ChecklyHQ
@ChecklyHQ 6 ай бұрын
Happy you found it after all. :)
@OlgaTheKey
@OlgaTheKey 4 ай бұрын
omg that is the one I needed so hard, cool explanation, because I literally bored with all setups and imports due creating student's project. ty
@ChecklyHQ
@ChecklyHQ 4 ай бұрын
We're very happy that this video has been helpful! 💙
@rossfisher5543
@rossfisher5543 Жыл бұрын
It would be great to see a video that matches with your article on the performance api. Great video!
@stefanjudis
@stefanjudis Жыл бұрын
Thanks Ross. I'll put it on the list for the future videos. :)
@luke1023
@luke1023 10 ай бұрын
Love the videos, they’re very helpful and love your energy!
@ChecklyHQ
@ChecklyHQ 10 ай бұрын
Thanks!
@robertrobles7698
@robertrobles7698 3 ай бұрын
Straightforward and useful explanation. Thank you for sharing!
@ChecklyHQ
@ChecklyHQ 3 ай бұрын
Glad it was helpful! 💙 Thanks for watching!
@user-en4pd9rd7s
@user-en4pd9rd7s Жыл бұрын
Great example, helped me a lot, other videos from people have a lot of useless info. But your example is super!
@ChecklyHQ
@ChecklyHQ Жыл бұрын
Thanks! Happy it's valuable! 🦝
@gruttewibe76
@gruttewibe76 6 ай бұрын
Very good explanation on the topic.
@ChecklyHQ
@ChecklyHQ 6 ай бұрын
Glad it was helpful!
@Cowglow
@Cowglow Жыл бұрын
Such a beautiful feature!
@ChecklyHQ
@ChecklyHQ Жыл бұрын
We think so, too! 💯
@dand4485
@dand4485 Жыл бұрын
Great presentation.
@ChecklyHQ
@ChecklyHQ Жыл бұрын
Thanks! Happy it's useful. :)
@justaskiekas
@justaskiekas Жыл бұрын
Awesome video
@stefanjudis
@stefanjudis Жыл бұрын
Thanks! Happy it was useful! 😊
@takeshikriang
@takeshikriang 11 ай бұрын
Very helpful, thanks.
@ChecklyHQ
@ChecklyHQ 11 ай бұрын
Glad it was helpful!
@QaAutomationAlchemist
@QaAutomationAlchemist 5 ай бұрын
Great video🎉
@ChecklyHQ
@ChecklyHQ 5 ай бұрын
Glad you enjoyed it. Thanks!
@oyerindedaniel7977
@oyerindedaniel7977 3 ай бұрын
nice video
@thiagofragoso6471
@thiagofragoso6471 7 ай бұрын
Thanks for sharing this content. quick and assetive. May I ask you whats the theme are you using on this video? thank you
@ChecklyHQ
@ChecklyHQ 7 ай бұрын
The color theme is Yi Dark and if you're after the custom font it's described in this article 👉 www.stefanjudis.com/blog/how-to-enable-beautiful-cursive-fonts-in-your-vs-code-theme/
@aleksandrdobrev6584
@aleksandrdobrev6584 Жыл бұрын
Very nice channel with good and simple explanations! How does this compare to a Page Object method? Is it better to use one over the other or maybe in combination?
@stefanjudis
@stefanjudis Жыл бұрын
Thanks Aleksandar. I don't think it's an either-or decision. Fixtures play nicely with POMs. I see their main advantage in being to use "native PW functionality" and the ability to have setups / teardowns next to the code running in your tests. So if you're POMs have some sort of required clean-up steps, i'd def go for a fixture setup. Hope this helps!
@HelloWorld-xg6nw
@HelloWorld-xg6nw Жыл бұрын
@@stefanjudis I am using fixtures to create POM objects, e.g. myAccountPage: async ({ page }, use) => { await use(new MyAccountPage(page)); }, so that I don't repear this useless code every time in test script
@karthikeyan.ganapathy
@karthikeyan.ganapathy Жыл бұрын
Thanks team for quick video, you guys are great
@powertester5596
@powertester5596 9 ай бұрын
Hi, I have started exploring playwright very recently and found your videos on the topic . Kudos for creating these :-). I have a question. If there are more than one reusable functions to use inside the test, how do you do it then?
@ChecklyHQ
@ChecklyHQ 9 ай бұрын
Custom fixtures aren't bound to a single one. You could add more if you wanted to. :) Add more properties to the `extend` object and you're good to go. :)
@powertester5596
@powertester5596 9 ай бұрын
@@ChecklyHQ cool. Thanks for the reply :-).
@guillaumebrunerie
@guillaumebrunerie Жыл бұрын
What’s the advantage of using a fixture as opposed to a regular function? You could simply define const login = async (page) => { // Do all the stuff } and then simply call `await login(page)` at the beginning of the test.
@stefanjudis
@stefanjudis Жыл бұрын
That's right Guillaume and a great question. In this simple example scenario, a helper method definitely works. 👍 Fixtures enable you to follow the PW conventions and group helper/utility code like the framework does. I consider this to be a great advantage. Additionally, and considering your example: if you'd want to perform a teardown action (maybe you want to log out after) you'd already have two methods to pull in every test case and the number of util functions grows. With a fixture-based approach, you could put setup (before the 'use' call), the action and teardowns (after the 'use' call) into a single place. Hope this helps. :)
@karthikeyan.ganapathy
@karthikeyan.ganapathy Жыл бұрын
is there a way we can enable and disable the fixture based on run time values like flag to enable or disable? Scenario: if the login/SSO is required for higher env's and not for lower env
@stefanjudis
@stefanjudis Жыл бұрын
Enabling and disabling fixtures doesn't feel right to me. I'd rather make the fixture behave differently depending on run time flags. :) Hope this helps.
@CostaGiannakopoulos
@CostaGiannakopoulos Жыл бұрын
Great video, Is it possible to create a base fixture that is common across all your tests but then extend the base fixture with something more specific that can target a subset of tests?
@ChecklyHQ
@ChecklyHQ Жыл бұрын
Great question, Costa. Yes, this is possible with `test.use()`. playwright.dev/docs/api/class-test#test-use
@CostaGiannakopoulos
@CostaGiannakopoulos Жыл бұрын
@@ChecklyHQ Thanks I found a solution where I create a baseFixture that holds everything that is common to all my tests and then I have fixtures that extend the base fixture that targets a subset of tests and its working perfectly. I did try the test.use() but in my use case it did not work
@ChecklyHQ
@ChecklyHQ Жыл бұрын
@@CostaGiannakopoulos That's great to hear? What were the issues with test.use()?
@CostaGiannakopoulos
@CostaGiannakopoulos Жыл бұрын
@@ChecklyHQ I was getting conflicts in the api request where 1 of the 2 requests was bad as they conflicted, so iv separated the conflicts into 2 new fixtures that both extend from the base so i dont have to duplicate the common stuff
@ChecklyHQ
@ChecklyHQ Жыл бұрын
@@CostaGiannakopoulos Great. Thanks for explaining!
@arti-draws
@arti-draws 2 ай бұрын
Very nice and informative video. Did you put your codes anywhere in Github? could you please give its link to us?
@ChecklyHQ
@ChecklyHQ 16 күн бұрын
Unfortunately we don't have to code available for this video any more, but I'll try to do better in the future. But generally the Playwright docs are pretty good here: playwright.dev/docs/test-fixtures#creating-a-fixture
@chandanchoudhary8630
@chandanchoudhary8630 7 ай бұрын
Great stuff , Thanks , but how can we utilize this is page object model and maintain the same page state as page instance will be different hence different instances will be launched , can u plz show , thanks
@ChecklyHQ
@ChecklyHQ 6 ай бұрын
That should definitely be possible. I'll put it on the list for future videos. :)
Жыл бұрын
which extension are you using to see the await text on handwriting font ?
@ChecklyHQ
@ChecklyHQ 11 ай бұрын
The editor setup is described here: www.stefanjudis.com/blog/how-to-enable-beautiful-cursive-fonts-in-your-vs-code-theme/
11 ай бұрын
@@ChecklyHQ thanks so much
@bgd_surf
@bgd_surf 3 ай бұрын
@@ChecklyHQ thanks u rock!
@Danielo515
@Danielo515 2 ай бұрын
How is this different from a function that takes the page and executes the same operations than the fixture ?
@ChecklyHQ
@ChecklyHQ Ай бұрын
Nothing wrong with writing your own abstraction. 👍 Fixture are just the more native way of doing things. :)
@TristanBailey
@TristanBailey 3 ай бұрын
How would you see that as different from just using .before that will already run before every test. (Or login session is better put in config where playwright has options for putting it per project/browser or for default of all. Saving in a json file. )
@ChecklyHQ
@ChecklyHQ 3 ай бұрын
It depends on the case I'd say. If you have a single `spec` file before/after does the trick for sure. And also, when it comes to saving the login logic, you also make a valid case buy saving the browser state. All that said, I often prefer fixtures, because they "hide" functionality from my spec files. I don't have to worry about fiddling with before/after or anything. Additionally, I can also configure them with fixture options. Implementing fixtures just feel like a "native Playwright approach". And I like this a lot. But, of course and as always, it's a classical "it depends" and a matter of taste. ;)
@danielkovacs3664
@danielkovacs3664 Жыл бұрын
thanks the videos Is there possible that we use different storagestate inside one test between test.steps? e.g.: test.step.use({storageState: '1.json '})... test.step.use({storageState: '2.json '})?
@stefanjudis
@stefanjudis Жыл бұрын
Hey Dániel, I just had a look and I don't think that's possible. But if you're testing your sites with two different states (such as a regular and admin one) I'd also favor fixtures. `test('something', ({page, AdminPage})` Hope this helps. :)
@danielkovacs3664
@danielkovacs3664 Жыл бұрын
@@stefanjudis thanks, i am doing this now, my test login admin... logout admin login editor... logout editor login admin, that i can delete the item what do u suggest how could i avoid so much login,logut?
@ChecklyHQ
@ChecklyHQ Жыл бұрын
@@danielkovacs3664 Hey Daniel, in this case I'd login admin and editor so that you don't have to switch and log in/out all the time. Similar to this: `test('something', ({page, AdminPage})` That said though, here at checkly we have a good experience with cleaning up created resources on the API level. blog.checklyhq.com/how-low-level-api-calls-stabilize-your-end-to-end-tests/
@sxsrin2
@sxsrin2 Жыл бұрын
I am trying to login with api and then pass that state to the page object and so far struggling with it. Can you make a video on that please ?
@ChecklyHQ
@ChecklyHQ Жыл бұрын
Great call. We'll put it on the list. :)
@salikhanan
@salikhanan 10 ай бұрын
Hello, How can you return something form Webapp fixure? I am trying to copy text from text field on clipboard and then eceluate it and return it to my test,
@ChecklyHQ
@ChecklyHQ 10 ай бұрын
This doesn't sound like a fixture case for me. I'd instead extract the logic into a separate function for the case of evaluating text in the clipboard. That said, you could always change the fixture to be a nested object or a function. It's up to you what to call `use` with. :)
@ripperx444
@ripperx444 4 ай бұрын
What if you want to have two fixtures in differnt files... you can't import them as they are extend test
@ChecklyHQ
@ChecklyHQ 4 ай бұрын
You could always structure your project to use different fixture with different exported `test` object. And depending on the fixture differences you could also make fixtures configurable with fixture options. It's a bit tough to give advice without knowing specific. :)
@imnutty7797
@imnutty7797 4 ай бұрын
What is font name do you use?
@ChecklyHQ
@ChecklyHQ 4 ай бұрын
It's FiraCode iScript and if you're looking for the same editor setup it's described on my blog: www.stefanjudis.com/blog/how-to-enable-beautiful-cursive-fonts-in-your-vs-code-theme/
@salikhanan
@salikhanan 11 ай бұрын
Hello, my API test creates an access token and I want to reuse that access token in other tests, How do you do that?
@ChecklyHQ
@ChecklyHQ 11 ай бұрын
You can create a similar fixture called "accessToken", do whatever needs to happen to access the token and then pass it to your tests via "use(token)". Hope this helps. :)
@salikhanan
@salikhanan 11 ай бұрын
@@ChecklyHQ ok, Thanks, That was a quick help. at the same time another question I have json(playload) stored in a file and I want one Field (RefNumber) has a random value every time How can I provide a random value every time to the RefNumber in json? for exmple in follwing workOrderPayload is the work load and I was to provide RefNumber as a random to the Json everytime const response = await request.post(workOrderUrl, { headers:{ "Accept" : "application/json, "Content-Type" : "application/json "Authorization" : `Bearer ${process.env.TOKEN}` }, data: workOrderPayload })
@ChecklyHQ
@ChecklyHQ 11 ай бұрын
@@salikhanan I'm not sure I understand. Could you use `Math.random()` and alter `workOrder` to include a different `RefNumber`? That said though, let's stay focused on this video here. :) For questions around synthetic monitoring with Playwright and Checkly we're always happy to help at checklyhq.com/slack/. :)
@og4789
@og4789 8 ай бұрын
Try native Authentication solution from Playwright well described in the docs
Now THIS is entertainment! 🤣
00:59
America's Got Talent
Рет қаралды 39 МЛН
Slow motion boy #shorts by Tsuriki Show
00:14
Tsuriki Show
Рет қаралды 8 МЛН
A teacher captured the cutest moment at the nursery #shorts
00:33
Fabiosa Stories
Рет қаралды 13 МЛН
New model rc bird unboxing and testing
00:10
Ruhul Shorts
Рет қаралды 24 МЛН
You don't need a frontend framework
15:45
Andrew Schmelyun
Рет қаралды 115 М.
Writing e2e tests isn’t hard, so do it
6:41
Web Dev Cody
Рет қаралды 16 М.
CrowdStrike IT Outage Explained by a Windows Developer
13:40
Dave's Garage
Рет қаралды 2 МЛН
e2e testing with Playwright | OD111
15:52
Microsoft Developer
Рет қаралды 21 М.
КАЧЕЛИ ИЗ АРБУЗА #юмор#cat  #топ
0:24
Лайки Like
Рет қаралды 1,4 МЛН
настоящий чемпион
0:59
QorabogTV
Рет қаралды 6 МЛН
La emociones de Bluey #歌ってみた #bluey #burrikiki
0:13
Bluey y BurriKiKi
Рет қаралды 25 МЛН