Reuse Playwright Code across Files and Tests with Fixtures

แชร์
ฝัง
  • เผยแพร่เมื่อ 28 พ.ย. 2024

ความคิดเห็น • 94

  • @OlgaTheKey
    @OlgaTheKey 8 หลายเดือนก่อน +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  8 หลายเดือนก่อน +1

      We're very happy that this video has been helpful! 💙

  • @3VAudioVideo
    @3VAudioVideo 8 หลายเดือนก่อน +2

    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  8 หลายเดือนก่อน +1

      Thank you for the kind words. Happy the video has been valuable. 💙

  • @owcaandroid
    @owcaandroid 10 หลายเดือนก่อน +1

    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  10 หลายเดือนก่อน

      Happy you found it after all. :)

  • @alexalexander4326
    @alexalexander4326 18 วันที่ผ่านมา

    Custom fixture is designed to extend basic fixtures. In your example you misuse it and normally this isolated repeated set of calls should go into a separate class (POM)

    • @ChecklyHQ
      @ChecklyHQ  18 วันที่ผ่านมา

      Thanks for your comment.
      "Custom fixture is designed to extend basic fixtures." -> Can you point me to where you have this from? I'm happy to be convinced but I don't think this is true. Fixtures can be used for much more than extending the included base fixtures.
      "This isolated repeated set of calls should go into a separate class (POM)" -> I can understand this opinion, but the question of "to POM or not to POM" isn't more than a personal preference. If POMs work great for you, wonderful, but I wouldn't (and in fact won't) recommend using POMs for everything. Using POMs often only increases complexity, which is also the case for this quick demo example. 🤷
      This video aims to be an intro to Playwright fixtures, and I think it does a decent job at this. :)

  • @robertrobles7698
    @robertrobles7698 8 หลายเดือนก่อน +1

    Straightforward and useful explanation. Thank you for sharing!

    • @ChecklyHQ
      @ChecklyHQ  8 หลายเดือนก่อน

      Glad it was helpful! 💙 Thanks for watching!

  • @rossfisher5543
    @rossfisher5543 ปีที่แล้ว +1

    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. :)

  • @NikoNiko-q1i
    @NikoNiko-q1i ปีที่แล้ว

    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! 🦝

  • @thiagofragoso6471
    @thiagofragoso6471 11 หลายเดือนก่อน +1

    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  11 หลายเดือนก่อน

      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/

  • @kundansharma6536
    @kundansharma6536 หลายเดือนก่อน

    Thanks for such a great explanation. Just wanted to know why page is getting cleared if any of the test cases in a describe block failed. Is there any way to handle it?

    • @ChecklyHQ
      @ChecklyHQ  หลายเดือนก่อน +1

      Thanks. :) I don't understand. What is cleared? Please provide as much context and code as possible (GitHub gists are great) because otherwise it's very tough to help or answer questions. :)

  • @powertester5596
    @powertester5596 ปีที่แล้ว +3

    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  ปีที่แล้ว +2

      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 ปีที่แล้ว +1

      @@ChecklyHQ cool. Thanks for the reply :-).

  • @guillaumebrunerie
    @guillaumebrunerie ปีที่แล้ว +3

    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 ปีที่แล้ว +4

      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. :)

  • @glebmirosnikovs6722
    @glebmirosnikovs6722 2 หลายเดือนก่อน +1

    Nice tutorial, thank you.
    Have one question. Currently on project we have POM and UtilClass for common methods we use. And usually I just create method in POM and then I can re use it in my test files.
    Looks like creating custom fixtures is more or less the same as storing method in Page Object file and then execute it if needed.
    Or are there any other advantages of using custom fixtures?
    Thanks

    • @ChecklyHQ
      @ChecklyHQ  2 หลายเดือนก่อน

      Yes, this is correct. There's nothing particularly wrong with importing utils/poms within your test files. But I found that by relying on fixtures a project becomes more structure and does things "the playwright way". One advantage is that if you're using classes that need to be initialized you can hide this and don't neet to care about it. :) 👇
      th-cam.com/video/k488kAtT-Pw/w-d-xo.html

    • @glebmirosnikovs6722
      @glebmirosnikovs6722 2 หลายเดือนก่อน

      @@ChecklyHQ Thank you for your reply, appreciate it! Speaking about initializing classes... we already hide them with pageManager. There is a function, that returns class object, so we call that function in test file.
      I agree about "the playwright way". Looks like there are few ways to achieve same result. Agree, that fixtures look nice.
      And thanks for sharing your tips and playwright insides. Before fixtures were unclear to me... but now I understand them a bit better. Thanks for your effort!

    • @ChecklyHQ
      @ChecklyHQ  2 หลายเดือนก่อน +1

      @@glebmirosnikovs6722 Oh for sure - there are plenty of ways to abstract things. :) Playwright is also just JS/TS at the end.
      Happy to hear that the videos have been valuable. If you have more questions or comments, I'm always looking for new ideas. ;)

  • @luke1023
    @luke1023 ปีที่แล้ว

    Love the videos, they’re very helpful and love your energy!

  • @gruttewibe76
    @gruttewibe76 10 หลายเดือนก่อน

    Very good explanation on the topic.

    • @ChecklyHQ
      @ChecklyHQ  10 หลายเดือนก่อน

      Glad it was helpful!

  • @romag5605
    @romag5605 2 หลายเดือนก่อน +2

    how can I use parameterized fixtures ? for example I want to make a login fixture but to pass username and the password to this fixture . can I ?

    • @ChecklyHQ
      @ChecklyHQ  2 หลายเดือนก่อน +1

      This is a great question and I think I'll record the answer today. :)

    • @ChecklyHQ
      @ChecklyHQ  2 หลายเดือนก่อน +1

      And here we go: th-cam.com/video/rRmfYu8hlbw/w-d-xo.html 😊

    • @romag5605
      @romag5605 2 หลายเดือนก่อน +1

      @@ChecklyHQ thanks, before I saw it , my solution for this was global params like: process.env.userName

  • @gdd546
    @gdd546 2 หลายเดือนก่อน

    Do you have a course on playwright framework design?

    • @ChecklyHQ
      @ChecklyHQ  2 หลายเดือนก่อน

      No, unfortunately we don't have and are not planning to create a course on that. :/

  • @karthikeyan.ganapathy
    @karthikeyan.ganapathy ปีที่แล้ว +1

    Thanks team for quick video, you guys are great

  •  ปีที่แล้ว +2

    which extension are you using to see the await text on handwriting font ?

    • @ChecklyHQ
      @ChecklyHQ  ปีที่แล้ว +1

      The editor setup is described here: www.stefanjudis.com/blog/how-to-enable-beautiful-cursive-fonts-in-your-vs-code-theme/

    •  ปีที่แล้ว

      @@ChecklyHQ thanks so much

    • @bgd_surf
      @bgd_surf 7 หลายเดือนก่อน

      @@ChecklyHQ thanks u rock!

  • @Cowglow
    @Cowglow ปีที่แล้ว +1

    Such a beautiful feature!

    • @ChecklyHQ
      @ChecklyHQ  ปีที่แล้ว

      We think so, too! 💯

  • @chandanchoudhary8630
    @chandanchoudhary8630 11 หลายเดือนก่อน

    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  11 หลายเดือนก่อน

      That should definitely be possible. I'll put it on the list for future videos. :)

  • @Danielo515
    @Danielo515 6 หลายเดือนก่อน +1

    How is this different from a function that takes the page and executes the same operations than the fixture ?

    • @ChecklyHQ
      @ChecklyHQ  5 หลายเดือนก่อน

      Nothing wrong with writing your own abstraction. 👍 Fixture are just the more native way of doing things. :)

  • @TristanBailey
    @TristanBailey 8 หลายเดือนก่อน

    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  8 หลายเดือนก่อน

      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. ;)

  • @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 ปีที่แล้ว +2

      @@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

  • @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  ปีที่แล้ว +1

      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 ปีที่แล้ว +1

      @@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  ปีที่แล้ว +1

      @@CostaGiannakopoulos Great. Thanks for explaining!

  • @ripperx444
    @ripperx444 8 หลายเดือนก่อน

    What if you want to have two fixtures in differnt files... you can't import them as they are extend test

    • @ChecklyHQ
      @ChecklyHQ  8 หลายเดือนก่อน

      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. :)

  • @justaskiekas
    @justaskiekas ปีที่แล้ว +1

    Awesome video

    • @stefanjudis
      @stefanjudis ปีที่แล้ว

      Thanks! Happy it was useful! 😊

  • @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 ปีที่แล้ว +1

      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.

  • @salikhanan
    @salikhanan ปีที่แล้ว

    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  ปีที่แล้ว

      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. :)

  • @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. :)

  • @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 ปีที่แล้ว +1

      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/

  • @imnutty7797
    @imnutty7797 9 หลายเดือนก่อน

    What is font name do you use?

    • @ChecklyHQ
      @ChecklyHQ  9 หลายเดือนก่อน +1

      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/

  • @dand4485
    @dand4485 ปีที่แล้ว

    Great presentation.

    • @ChecklyHQ
      @ChecklyHQ  ปีที่แล้ว +1

      Thanks! Happy it's useful. :)

  • @salikhanan
    @salikhanan ปีที่แล้ว

    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  ปีที่แล้ว

      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 ปีที่แล้ว

      @@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  ปีที่แล้ว

      @@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 ปีที่แล้ว

      Try native Authentication solution from Playwright well described in the docs

  • @takeshikriang
    @takeshikriang ปีที่แล้ว

    Very helpful, thanks.

    • @ChecklyHQ
      @ChecklyHQ  ปีที่แล้ว

      Glad it was helpful!

  • @QaAutomationAlchemist
    @QaAutomationAlchemist 9 หลายเดือนก่อน

    Great video🎉

    • @ChecklyHQ
      @ChecklyHQ  9 หลายเดือนก่อน

      Glad you enjoyed it. Thanks!

  • @oyerindedaniel7977
    @oyerindedaniel7977 7 หลายเดือนก่อน

    nice video