Playwright Assertions | Bondar Academy
Course: Playwright UI Testing with TypeScript
Module: Locators and Assertions
Instructor: Artem Bondar
Lesson Summary
In this lesson, we explore assertions in Playwright, focusing on the differences between three main types: generic assertions , locator assertions , and soft assertions . Types of Assertions Generic Assertions Example syntax: expect(value).toEqual(expectedValue) . Common methods include: toBe toEqual toContain toHaveLength Locator Assertions These assertions interact with web elements and can wait for conditions. Example syntax: await expect(locator).toHaveText('submit') . They include methods like: toBeAttached toBeChecked toHaveText Locator assertions have a timeout of up to five seconds. Soft Assertions Allow tests to continue execution even if an assertion fails. Example syntax: await expect.soft(locator).toHaveText('submit') . Considered less ideal for regular testing practices. Key Takeaways In summary, Playwright offers: Generic Assertions that do not wait for conditions. Locator Assertions that wait for elements and are more interactive. Soft Assertions that allow tests to proceed despite failures. Understanding these differences is crucial for effective testing in Playwright. See you in the next lesson!
Video Transcript
In this lesson, we will talk about assertions more in details. So in the previous lessons, we already created several assertions. But in this lesson, I will show you the difference between two main types of the assertions in Playwright, generic assertions and locator assertions. So what are the syntax rules for those two types of the assertions you should apply? And the third type of the assertions that you can use is a soft assertion. So we will talk about this in this lesson. Let's get into it. So in the previous lessons, we already used the assertions. So here, for example, we validated email input field has test at test.com. And here we made other assertions of the submit, option one, and so on. But you probably noticed the difference that here we used a wait, expect something to have value something. But later, we didn't use a wait keyword for our assertion. So what's the difference? And the difference between different types of the assertions of the Playwright, and I will show you those differences in this lesson. So let me start a new test. So Playwright has two different types of the assertions, general assertions and locator assertions. So let me start first with general assertions. And we begin with very simple example. Let's say I create a new constant. Value equals to five. And then I want to assert that expect value to equal five. And let me run this test real quick. Assertions, run it. Let's open the browser. And here we go. This is the assertions. Expect value to equal five. If I change it to expected result to six, and run it. And it's definitely failed. We see that expected six, but received value five, which is expected. So general assertion logic is very simple. We compare value on the left to the value on the right. And the methods that are available to make a validation are very different. You can look in the list of this method by just typing dot. And here is the list of all possible assertions. For example, to be, to be close to, less than, equal to, to be null, to be truthy, to be I define. To contain is when you have a list of values and you want to make sure that the list has the value that you have, to equal, to have length, and so on. And look at the small annotation next to it. It says generic assertions. So the provided list, we see only generic assertions. Generic assertions are very straightforward. Inside of the expect method, you provide the argument that you want to assert. And on the right, you choose the method and provides your expectation. And that's all it does. So let's make an assertion for, for example, the basic form here. So we make assertion for the submit button. So we can take the previous example, this basic form. Let me paste it here. Locator button. And it will be basic form button, okay? And now let's make assertion that this basic form button has text submit. In order to do that, we need first to get this text from the button equals to basic form button, text content. And then we can make assertion, expect to equal submit. I run this test. And this didn't work. Received empty to expected submit. So let's take a look. And okay, we forgot to put a wait here. So the text content will wait for that web element. Get the text and then run submit. Run it again. And now it's passed successfully, text content to equal submit. So here's the example how easy you can make a mistake, right? So I forgot to put the await and it didn't work. So keep an eye on this thing. And if something doesn't work in your code, always the first thing, check if you put await everywhere where it should. So and it worked. So one more time, we took the button. We took the text from this button, assigned it to the constant and executed the general assertion to validate the text to submit. So now let's do the same using a locator assertion. And what is the difference? For the locator assertion, we also use expect. But instead of providing the exact value inside of the expect, in this example, we use text, which had the exact value. We will provide a locator. Locator is basic form here. And if I click dot, now I have more assertions to be available. For example, to be attached. You can see this is a locator assertion or to be checked. This is also a locator assertion. Or to not to be empty, locator assertion, and so on. So now we have a list of generic assertion to be false and to be focused is a locator assertion. And in the list of these assertions, we can find an assertion that has to have property, to have text. Here we go. This is what we're looking for. To have text and we validate what kind of text we are looking for. And we're looking for text submit. And because this is a locator assertion, we need to provide a weight in front of the expect. To have text method will search for the text inside of this web element. And when it find the expected text, it will make an assertion. And also locator assertions has their own timeout. So when you use the await, expect, and locator assertion, this type of assertion will wait up to five seconds for the element to be available, while the general assertions will not wait any. It will be just executed when it is time in the sequence of the commands of execution. Locator assertions will always wait, and we will talk more about auto-waiting in the next lesson. And another type of the assertion that you can use is soft assertion. Soft assertion. Soft assertion is a kind of the assertion when the test can be continued the execution, even if the assertion has failed. For example, I will type await expect.soft. Then we provide the basic form button, and then to have text, submit. And let's say after this assertion, we want to click on the button. So we will type await basic button.click. With a soft assertion, if we will fail this test, for example, we put submit five, right? So this assertion should fail. But despite that, the test will continue and still will click the button. Let me show you that. So I run this test. We see a waiting of five seconds, and it failed. So we clearly see the failure message that receive the event was submit, but expected was submit five. But at the same time, the playwright click on the submit button as the next text. If I will remove a soft assertion and try to run this test again, it's waiting for five seconds again, and test failed, and it's done. Test did not continue the execution. Playwright did not click on the next step, which is click button for the basic form. So this is the difference between soft assertion and regular assertion. Keep in mind that using a soft assertion considered not a regular assertion, but a soft assertion, and regular assertion. Keep in mind that using a soft assertion considered not a really good practice. But still, if you want to continue running your test after certain validation has failed in order to catch other possible validations within this test run and analyze it, you can use a soft assertions. All right, so let's quickly summarize what we did in this lesson. Playwright has two types of assertion, general assertions and locator assertions. General assertions are very simple. You just provide expect with the value that you want to assert and the desired method with the expectation. General assertions will not wait for any conditions. It simply perform the assertion when it's time to execute this particular line of code. Locator assertion, instead, they're a little bit smarter. They can interact with the web elements and the methods of the locator assertions will wait up to five seconds for the element to be available to make an assertion. To make a soft assertion, you just need to add .soft. In this case, your test will not fail and continue to run if assertion has failed. All right, that's it, guys, and see you in the next lesson.