Authorization Helper | Bondar Academy
Course: Playwright API Testing with TypeScript
Module: Building a Framework
Instructor: Artem Bondar
Lesson Summary
In this lesson, we focus on improving the authorization process by creating a helper function for token generation instead of using the beforeEach hook. Creating a Helper Function We will create a new folder called helpers to store various functions needed for repetitive tasks or preconditions in tests. Inside this folder, we will create a file named createToken.ts . Simple Approach Create an async function named createToken that takes parameters for email and password . Replace the current token generation logic with a call to createToken in the test file, passing the necessary parameters. Independent Approach To create a more independent function: Remove the dependency on the API request handler. Import and instantiate the request handler and logger within the function. Create a new request context using request.newContext() . Implement error handling with a try-catch block to capture errors and stack traces. Ensure to call context.dispose() to properly close the context and free memory. Summary We explored two methods for creating helper functions: The simpler method relies on passing API fixtures as dependencies. The more complex method creates an independent function that can be executed outside of tests. By following these steps, the createToken function becomes fully independent and reusable across different contexts.
Video Transcript
In this lesson, we will make improvements for authorization process. So currently, we create token in the before each hook, but instead we will create a helper function that will be responsible for this step. All right, so let's get into it. So this is the before all hook and this is the way how we create the token right now. So let's create a helper function and I will show you two ways how you can create a helper functions in the playwright, the easier way and more complex independent way. So I will create a new folder, which I call helpers, helpers. And in this folder, we can keep different kind of functions that are needed to create some kind of repetitive data or maybe set preconditions for our tests and something like this. So inside of it, I create a new file, createToken.ts. And let me create a new function, exportAsyncFunction and the function name would be createToken. Okay, so which parameters do we need over here? The simpler way of creating this function is to create dependency on the API request handler like this. And then we will add other parameters that we will need for our request. So going back to the smoke test.spec.ts and I just copy the whole thing right here and copying over here inside. Then we have a dependency on these two fields, email and password. So let's create those properties. Email is a string and password is a string, like this. And we will replace email over here and password over here. And then instead of auth token, I will tell that we want to return this value. That's it, so simple. And now inside of the test, going back to the smoke test, we have a function createToken. It's imported automatically from the createToken. And we need to pass required parameters, which is API. Then config user email and then config user password. Like this, and then I remove this. And this is also a wait because this is a sync function. Yeah, that's it. Let's try to run it, and it worked as expected. The function is working correctly. So this is the most simplest way how you can create reusable function. The only kind of disadvantage of this approach is that we have a dependency on the API fixture that we need to pass into the reusable function. But if you use these functions inside of the test, then it's totally fine because API fixture is available. But if you want to create a function that will be completely independent from the test that you can run from anywhere, even outside of the scope of the test, then we need to choose a different approach. And for the createToken specifically, I would like to follow this different approach and let me show you what's the difference. So instead of using API request handler as a dependency, so first of all, let me comment this out temporarily. So I will remove this dependency completely. But I will need in this case to import and create the instance of the request handler and the logger by myself within this function. So let me do that. So I can take these dependencies from the fixtures. I will need API request handler. So where is that createToken? I will need to add it into this function, request handler. And also I will need a logger, logger right here, going back over here. Okay, logger is here. We have complaints on this configuration. So we need to import also config file. Import config from API config. Okay, we'll fix this. Then we need to make an import of API logger, a quick on quick fix, and add import from utils logger. This one is fixed as well. And the last thing is to fix this request object. So request object is not available for us within just independent function. Because remember, request fixture available only inside of the playwright fixture and inside of the test. But we can create a new context based on that. So I will need import the request from playwright test, but not the object request like this. And then const context equals to request.newContext. And we create a new request context independent within this function, okay? Then I take this context and pass it right here. And the red squigglies, and yeah, I am missing await over here. So await request new context, we create a new context, pass it here. Logger, everything is ready. And this guy, we can rename to just API. And finally, everything is ready. Now we have API is created. So this function for us right now is completely independent. And the only one thing left is to make a proper error handling in case this function will fail. So we will know where exactly the issue happens. So I will create a try-catch block. Try-catch like this. And I will put this inside, Ctrl-X, Ctrl-V. Then if the error happens, I want to catch this error and capture stack trace. Remember, we did this before with the error and the name of the function createToken. And then I want to throw this error like this. And finally, what I need to do is the very final step is the context.dispose. So what does this mean? And I need to put await, not delete, dispose. Dispose, here we go, this is the correct method. So when this method will be completely executed, and even if it will not throw any error, we need to properly close or dispose the context which we created over here in the line number eight. Just to save the memory and not create unneeded objects in the memory during the test run. So right now, this function is completely independent, completely isolated. It creates own API request context using our logger and request handler. Create API request, return the token that we need. And finally, it's close the context at the end of the execution. So now let's try to use this function over here. So I call this authToken, and I need to just remove this API thing. And run this test one more time. And everything is working, function working as expected. All right, so let's quickly summarize what we did in this lesson. When you need some repetitive kinds of operation that create a precondition for your test, creating a helper functions is a good way to do. So there are two ways how you can do it. So option number one, which is easier option, is to create a function that has a dependency on the API fixture. So you pass this fixture inside of this function as a dependency, and then execute the API requests, and then provide the standard return. But if you want to create more independent function, you need to create a new request context like this. And then you can pass this request context into the request handler, and API logger is also have to be imported inside over here. Then using try-catch block, you create an API request, and then finally, you need to dispose the context that you created to properly close the separation. The createToken function becomes completely independent, and you can run it even outside of the test if you need it. All right, that's it guys, and see you in the next lesson.