1

Problem Statement : In test.spec.ts I have a describe block inside which I have a test. In the same file, I have another describe block inside which I have a test that needs to call the test in first describe block and then proceed with it's rest of the steps.

test.spec.ts

test.describe("Describe block 1", async () => {
 test("a test that creates a chart of a given type(assume that you can pass the type from an environment variable) and validate if it is created correctly", async()=>{
  step1
  step2
....
})
})

test.describe("Describe block 2, async () => {
 test("a test that firstly need a new chart to proceed with it's other steps", async()=> {
  call the test in first describe block and get a chart created and then proceed with other steps
  step2
  step3
....
})
})

Why I would need to do such a thing - There is a valid use case where in the first describe block there is a test that creates a chart of a given type and validates if the chart is created correctly. In one of the test in second describe block I need to again create a chart and then do some other validations basis the type of the chart. This will ensure that I am not touching any existing chart and have a valid sample to work with.

Is there a way to achieve the same in playwright efficiently. What other options do I have?

Thank you in advance!

utkarsh-k
  • 836
  • 8
  • 17

1 Answers1

1

Re-use function, not test. Remember a test can fail for unexpected behavior but not an function unless there are coding errors.

Create a re-usable method in your page object to create charts.

Pass all the variable data inputs to it as parameters and use it anywhere as a reusable function to create different kind of charts based on its parameters.

Note: Don't create test dependencies(like calling one test from another or use data created in one test in another test after) its a bad practice as for the simple reason if one test fails the dependent tests will also fail in chain reaction.

Keep your tests isolated to each other as much as possible.

Vishal Aggarwal
  • 1,929
  • 1
  • 13
  • 23
  • thanks. Is there a way to avoid such dependencies. I don't want to use existing charts. I need a newly created chart for my test every time to have a consistent sample every time I run my test validations. – utkarsh-k Aug 22 '23 at 04:34
  • Yes , as I mentioned create a method to create chart in your page object and call in tests wherever required. – Vishal Aggarwal Aug 22 '23 at 10:11
  • can you please suggest me some repos or example of page object model that scales very well for a large app. I am writing down the tests for a large application and following page object model. I have been following up best practices that I have learnt in playwright but I want to see other projects as well that I can refer to see if my direction of writing the tests are right and will scale well. Thank you. – utkarsh-k Aug 23 '23 at 13:24
  • My suggestion would be don't get in to premature optimization and just focus on keeping your code DRY. – Vishal Aggarwal Aug 23 '23 at 14:42
  • https://en.m.wikipedia.org/wiki/Don't_repeat_yourself – Vishal Aggarwal Aug 23 '23 at 14:42
  • Moving the code to a new function will keep my code DRY but I noticed that essentially what I have done is moved the code related to a test to a function and calling it wherever required in my spec file. This is kind of dependency because the code that I have moved "creates a chart of a given type" and there is a test where I create a chart of a given type say type A and validates if it get created where I call this function. Also there is another test where I need a chart of type A, I call this function. Hence this latter test will become dependent on creation test case. – utkarsh-k Aug 28 '23 at 09:38
  • Both tests are dependent on an function but not on each other, there is an difference. – Vishal Aggarwal Aug 28 '23 at 11:22
  • Function does not create a chart of a single type only . Various kind of charts can be created using parameters with same function. – Vishal Aggarwal Aug 28 '23 at 11:23
  • Even if a chart creation process becomes lengthy, it can be broken down into sub functions who do their small bit in the process. – Vishal Aggarwal Aug 28 '23 at 11:25