Parameterization (Data Driven Testing) using Playwright Python

Tutorial 20​​ ​​ Parameterization (Data Driven Testing)​​ using​​ Playwright​​ Python​​ 

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

  • Parameterize a Test

  • Code snippet

Parameterize a Test​​ ​​ 

Sometimes​​ we may want to execute a Test with multiple sets of data.​​ 

As an example, let us launch

http://zero.webappsecurity.com/login.html​​ 

We have a ‘Login’ field here. We may want to login with different user ids for​​ different tests. It does not make sense to automate the same login functionality again and again for different user ids (since the code logic would be same).

So instead, what we do is that, we write a piece of code for login functionality and then parametrize it for different user ids.​​ 

Before we practically see this, let us first inspect the above ‘Login’ field

Make a note of ‘id’, we would be using it in our script to locate this field.

To begin with, we will first create a​​ @pytest.mark.parametrize​​ annotation that will​​ hold 3 test user names​​ (TestUser1, TesUser2 etc)​​ during runtime

We than pass this ‘username’ to our function as shown below

Save and execute

Notice below that the test gets executed 3 times (for the 3 users respectively)

 

The test passes

This is how we can perform data driven testing.

Code snippet

import​​ time

import​​ pytest
from​​ playwright.sync_api​​ import​​ Page,​​ expect

@pytest.mark.parametrize("username",​​ [("TestUser1"),("TestUser2"),("TestUser3")])
def​​ test_example(page: Page,​​ username) ->​​ None:
 ​​ ​​ ​​​​ page.goto(
"http://zero.webappsecurity.com/login.html")

 ​​ ​​ ​​​​ page.locator(
"#user_login").fill(username)

 ​​ ​​ ​​​​ time.sleep(
6)

 

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 10th Nov 2024