Retries, Pause and Multiple Browser Pages usage in Playwright

Tutorial 9​​ ​​ Pause, Retries, Multiple browser​​ tabs​​ in Playwright​​ 

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

  • Usage of ‘Pause’​​ and Playwright Inspector

  • Usage of ‘Retries’​​ 

  • Multiple Browser Pages/Tabs using single browser instance

  • Code snippet

Usage of ‘Pause’ in PW​​ and Playwright Inspector​​ ​​ 

Sometimes​​ we may want to pause our test so that we can either debug or wait for an even to occur etc.​​ 

.pause()​​ method comes handy here, see the description below​​ 

The below script will launch the browser and then playwright will pause the script

Execute the script, notice below that ‘Playwright Inspector’ is launced the script pauses. The browser does not close automatically

Click the ‘Resume’ button to continue the script and the browser closes, test gets passed​​ 

Usage of ‘Retries’

Sometimes we may want to automatically execute a failed test multiple times (without manually executing the test). We might also want to do this if the tests are flaky for any reason.

To demonstrate this, let us intentionally remove .com from the domainname as seen below

Execute the command with​​ --retries=2​​ flag (you can put any number though).​​ 

Notice below that the test executed 3 times (one is default execution and rest 2 are due to retries)

The error below points us that something is not right in line#22

Let us correct the script

Execute the script with​​ --retries=2​​ flag.​​ 

Notice that the test executes only once this time, since the test passed in first instance itself. So basically, the ‘retries’ option will not be called if the test passes​​ 

Multiple Browser​​ Page/Tabs using single browser instance

So far, we have been using the ‘page’ object to create ‘page’ instance for each test

Let us now use the ‘browser’ object.​​ 

The same browser instance can be used between all the tests

Let us see how.

Let us first create a new browser context. As can be seen in the description, the browser context will not share cache with other browser contexts

So​​ let us create​​ a ‘context’ variable holding the browser context

Using this context, we can now create a new browse page

So we have

Let us catch this new page in a const var page1

Similarly let us create 2 more pages using the same browser context

Next we can launch 3 websites on 3 different pages

Save and execute.

Notice below that 3 tabs open

Code snippet

import​​ {test,expect}​​ from​​ '@playwright/test'

 

test('retries',​​ async​​ ({​​ page​​ })​​ =>​​ {

   ​​ await​​ page.goto('http://zero.webappsecurity.com/online-banking.html')

   ​​ //await page.pause();          

})

 

test.only('Multiple browser pages',​​ async​​ ({​​ browser​​ })​​ =>​​ {

   ​​ const​​ context​​ =​​ await​​ browser.newContext()

   ​​ const​​ page1​​   =​​ await​​ context.newPage()

   ​​ const​​ page2​​   =​​ await​​ context.newPage()

   ​​ const​​ page3​​   =​​ await​​ context.newPage()

   ​​ await​​ page1.goto('https://www.bing.com/')    ​​ 

   ​​ await​​ page2.goto('https://www.bing.com/')    ​​ 

   ​​ await​​ page3.goto('https://www.bing.com/')              

})

 

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 - 7000 RS/ 99 USD
Attend All Live courses in just 7000 rs / $99 - offer ends 31st DEC 2024