Schema Assertion | Bondar Academy
Course: Playwright API Testing with TypeScript
Module: Schema Validation
Instructor: Artem Bondar
Lesson Summary
In this lesson, we focused on converting the validate schema method into a custom expect assertion called shouldMatchSchema . Here’s a breakdown of the process: Steps to Create Custom Assertion Start by copying an existing assertion template. Rename it to shouldMatchSchema . Replace the base expect method with the validateSchema method. Import the validateSchema from the schema validator. Make the shouldMatchSchema function asynchronous. Handling Parameters Pass the response object from the expect method as the first argument. The expected arguments for the function are: directoryName fileName Error Handling In case of validation failure, the error is caught and the message is logged. The message is simplified to indicate whether the schema validation passed or failed. Testing the Assertion Finally, we tested the assertion by calling expect(response).shouldMatchSchema(directoryName, fileName) . The tests passed successfully, confirming that the schema validation works as intended. Summary We created a custom assertion shouldMatchSchema that validates the schema asynchronously, using the validateSchema method. The assertion captures the response and logs any errors encountered during validation.
Video Transcript
Okay, guys, we are moving forward, and in this lesson, we will convert the validate schema method into a custom expect assertion, all right? Let's jump into it. So this is our project so far. We have this await validate schema to validate the get tags schema. So let's convert it into the custom expect assertion to follow the same format that we have for the assertions before. For example, expect response should match schema, and then schema details that we expect, all right? How about that? So for that template, let me copy this assertion, and I will copy and paste it over here. It's going to be my starting point. I will rename it into should match schema, will be my assertion. Then let's simplify. So instead of base expect receive to equal, I will replace it with the method that we called right here in the test, Ctrl C, and replace it over here, Ctrl V. Now we need to fix some of those things. First of all, it cannot find validate schema. Quick fix, we need an import from schema validator, okay? Import was added, it's fine. The next thing, await express only allowed in asynchronous function. So this match schema should be async like this, and this one fixed as well. The next step, response, where we get the response. So response, we will pass from the expect method. So the first argument right here in the received is the actual response object that is coming back from our API. So all we need to do is just take this received and replace right here. And then we need to figure out with this expected any. So instead of expected any, we want to get the actual arguments for the function, which is directory name and file name, these two guys. So custom expect, and I am expecting the directory name and the file name as the argument to should match schema method. And then I pass the directory name over here, and the file name over here, like this. And remove the single quote, and I think we fixed all the squigglies and all that stuff. Now we need to add the should match schema into the namespace over here. So I copy this, paste it here, get the name, paste it here. What are the expected arguments? So the directory name and the file string, like this. And the return type, instead of r, it have to be a promise r. Why promise? Should match schema is the asynchronous function or asynchronous method. So I type promise, promise, okay, like this. And type of r. All right, this one is ready. Now let's fix the content. So what it's gonna do? We validate the schema, if everything is went okay, pass is true. And we will not have a negative type of the validation, like should not match schema or something. So I will remove this from here. If the validation schema did not work, it will throw the error. This error will be caught by the catch block right here, then the pass is false. Then what we want to do, let's create a constant of the logs and remove the constant from here. So const the logs, and we will print some message with the logs. So if the assertion pass, let's create a message, let's do like this. So let message of type string. I just want to simplify it a little bit, because right now we're returning this big message. This message, we technically don't need the huge message like this. So I will temporarily comment this out. So the message, if the assertion passed, right here. I will assign a message equals to schema validation passed, something like this. And if assertion failed, I will assign a message as just reading the error message from here, which is e.message. So the e is represented like error that will be caught from this method after the execution, and then I catch this error and I want to print the message. The message that will be printed is this one, which is thrown by our validate schema function, okay? So doing this, and let's maybe append recent API activity. So I do dash n dash n, and I will add this recent API activity as well. So in total, we'll be printing the error message from the function, and just additionally, we will have a recent API activity. And that's it, we don't need all this stuff. We don't need this is not, because we're not gonna use, should not have schema or something like this. We need only a positive test case, so we can return, remove this message. Yeah, I believe, yeah, that's it, let's try it. So now, going back to the test, and now I call expect, then response.shouldMatchSchema, and provide the arguments. The directory name is tags, and the file name is get underscore tags, like this. And I can comment this line temporarily, and just see if it is working. Running the test, and test, pass successfully. Okay, let's see if it's actually working. So I change this to, for example, to a string, to modify the schema running the test, and yay, validation. Schema validation failed, perfect. So let's look into the report. Open in default browser, awesome. So we have a message printed, schema validation failed. The error message must be a string, then the actual response, and then recent API activity was pulled from the log. Everything is working as designed, so sounds good. We're removing this, and now we formatted everything, and using the same format for the validation, along with our custom assertions, like should equal, should be less than or equal. And now we have a new method that is doing a schema validation, should match the schema that's doing stuff for us. Let me revert this back, array, run it this one more time, and it is working. All right, so let's quickly summarize what we did in this lesson. So we created a custom assertion which we called should match schema, and we call the validate schema method inside of this custom assertion. This custom assertion should be asynchronous, because the validate schema has a weight, and the function validate schema is asynchronous as well. So should match schema have to be asynchronous. That's why the interface type over here have to be promise R, a little bit the confusion, but it is what it is. The first argument inside of the received schema is the object that is coming from our test as a argument inside of the expect, and the rest of two arguments is directory name, file name, we pass into validate schema, it returns us error message, and the recent API logs after the execution. All right, that's it guys, and see you in the next lesson.