Annotations in Playwright

Tutorial 5​​ ​​ Assertions (contd…)​​ and Annotations​​ 

What you will Learn​​ in this​​ blog:

  • Recap of ‘toContainText()’ assertion

  • Assertion to match 100% text

  • Assert the count

  • Annotations​​ Introduction

  • Skip annotation

Recap of ‘toContainText()’ assertion​​ 

In one of our previous tutorials​​ https://www.way2automation.com/write-and-execute-your-first-playwright-test/​​ we have already seen how to verify presence of an element on a page by using ‘toContainText()’ method, a quick snapshot is shown below.​​ 

However, if you carefully observe, this method uses the word ‘Contain.​​ What this means is that, this method would verify if the pageHeader​​ h1​​ contains​​ some portion​​ of the text from within ‘LIFETIME MEMBERSHIP CLUB’.​​ 

This method​​ does NOT match​​ 100% of​​ the entire ‘LIFETIME MEMBERSHIP CLUB’ text​​ ​​ 

​​ 

Let us now see an assertion that will match 100% text.

Assertion to match​​ 100% text

Launch​​ http://zero.webappsecurity.com/login.html​​ and inspect the header text​​ 

To validate whether the text ‘Log in to ZeroBank’ is 100% matching with the text captured in ‘elem’ variable, we can use ‘toHaveText​​ method

Save and execute test, notice that assertion got passed

Let us introduce an error in the expected text

Save and execute. Notice the error

Assert the count

Let us now assert how many​​ h3​​ elements​​ are displayed on the page.​​ But before we do that, let us first use ChroPath to count the number of ‘h3’ elements on the page.​​ 

Notice below that there is only 1 element matching​​ 

There is a ‘toHaveCount()’ method that we can use here, see line#27. We know that the count of​​ h3​​ is 1, but let us mention count as 2​​ as shown below

Save and run.​​ 

Notice the error​​ (self-explanatory)


Let us now correct the count to 1

Save and execute.​​ 

Notice that the test got​​ passed​​ this time

Similarly, there are many more assertions that you can explore and practice

Annotations​​ Introduction

Launch the official playwright page for annotations​​ https://playwright.dev/docs/test-annotations

Notice below that the annotations help us in dealing with skipping a test execution, grouping the tests etc​​ 

Let us see annotations​​ one by one.

Skip annotation

Skip annotation allows us to select and mark tests that will be ignored during the test execution.​​ 

We had​​ earlier commented one test so that playwright does not execute it, see below

Let us uncomment the above test

Next, let us write test.skip​​ (see line#4​​ below)

So basically, we have used​​ ‘skip’ annotation​​ here to skip the​​ test​​ ‘working with forms’

Save the file.​​ 

So right now we have 2 tests in this file:​​ 
‘working with forms’ and ‘working with assertions’

Execute the test.

Notice below that​​ the test ‘working with forms’ got skipped​​ (marked as blue)​​ and the other​​ test​​ got passed

We will see more annotations in next blog.

Thank you for reading!​​ 

Share On

Share on facebook
Share on twitter
Share on linkedin
Share on whatsapp
Share on tumblr
Share on email

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top

Lifetime Membership Club

LIFETIME MEMBERSHIP BIG SALE - ALL LIVE COURES JUST - 10000 RS/149 USD
Attend All Live courses in just 10000 rs / $149 - offer ends 31st May 2024