{"id":3426,"date":"2025-01-16T14:00:24","date_gmt":"2025-01-16T14:00:24","guid":{"rendered":"https:\/\/rahulshettyacademy.com\/blog\/index.php\/2025\/01\/16\/data-driven-testing-using-playwright-java\/"},"modified":"2025-01-16T14:00:24","modified_gmt":"2025-01-16T14:00:24","slug":"data-driven-testing-using-playwright-java","status":"publish","type":"post","link":"https:\/\/rahulshettyacademy.com\/blog\/index.php\/2025\/01\/16\/data-driven-testing-using-playwright-java\/","title":{"rendered":"Data Driven Testing using Playwright Java"},"content":{"rendered":"<p><html><body><\/p>\n<section class=\"tcb-post-content tcb-shortcode thrv_wrapper\" data-css=\"tve-u-16ec5d248bb\">\n<div class=\"tve_flt tcb-style-wrap\" id=\"tve_flt\">\n<div class=\"tve_shortcode_editor tar-main-content\" data-post-id=\"3426\" id=\"tve_editor\">\n<div class=\"thrv_wrapper thrv_text_element\">\n<p><em>In this blog we will be utilizing Playwright Java for the execution of Data Driven Testing with ease.<\/em><\/p>\n<div class=\"code-block code-block-2\" style=\"margin: 8px auto; text-align: center; display: block; clear: both;\">\n<p><!-- horizontal responsive 2 --><br \/>\n<ins class=\"adsbygoogle\" data-ad-client=\"ca-pub-3231318496783163\" data-ad-format=\"auto\" data-ad-slot=\"2788468763\" data-full-width-responsive=\"true\" style=\"display:block\"><\/ins>\n<\/div>\n<p><strong>Topics that we will cover:<\/strong><\/p>\n<ul>\n<li>Parameterize Test using dataProvider in same file<\/li>\n<li>Parameterize Test using dataProvider in separate file<\/li>\n<li>Source code (dataProvider in same file)<\/li>\n<li>Source code (dataProvider in different file)<\/li>\n<li>Source code (external dataProvider)<\/li>\n<\/ul>\n<p><strong>Parameterize Test using dataProvider in same file \u00a0<\/strong><\/p>\n<p>On certain occasions, we might have the need to perform a Test using various sets of data.\u00a0<\/p>\n<p>To understand this, navigate to<\/p>\n<p><a href=\"http:\/\/zero.webappsecurity.com\/login.html\" rel=\"noopener\" target=\"_blank\">http:\/\/zero.webappsecurity.com\/login.html<\/a>\u00a0<\/p>\n<\/div>\n<div class=\"thrv_wrapper tve_image_caption\" data-css=\"tve-u-1946f6c2b5a\" style=\"\"><span class=\"tve_image_frame\"><img alt=\"\" class=\"tve_image wp-image-3429\" data-css=\"tve-u-1946f6c4aa8\" data-height=\"223\" data-id=\"3429\" data-init-height=\"510\" data-init-width=\"1198\" data-width=\"523\" decoding=\"async\" height=\"223\" loading=\"lazy\" src=\"\/blog\/wp-content\/uploads\/2025\/01\/Screenshot-2025-01-16-at-7.31.24\u202fPM.png\" style=\"aspect-ratio: auto 1198 \/ 510;\" title=\"Screenshot 2025-01-16 at 7.31.24\u202fPM\" width=\"523\"\/><\/span><\/div>\n<div class=\"thrv_wrapper thrv_text_element\">\n<p>\u00a0It may be necessary to use various user IDs when logging in for different tests. For example, in first test we may want to login with user id \u201cTestUser1\u201d, in the second test we may want to login as \u201cTestUser2\u201d and so on&#8230;.\u00a0<\/p>\n<p>Automating repetitive login functionality for various user ids is redundant, as the code logic remains constant while only the login id varies. It is illogical to repeatedly automate the same process.<\/p>\n<p>Instead, our approach would be to write code for login functionality and then parameterize the code for multiple user ids.\u00a0<\/p>\n<p>Let us see how to perform data driven testing.\u00a0<\/p>\n<p>Inspect \u2018Login\u2019 field\u00a0<\/p>\n<\/div>\n<div class=\"thrv_wrapper tve_image_caption\" data-css=\"tve-u-1946f6d6fdd\" style=\"\"><span class=\"tve_image_frame\"><img alt=\"\" class=\"tve_image wp-image-3431\" data-css=\"tve-u-1946f6d8710\" data-height=\"204\" data-id=\"3431\" data-init-height=\"446\" data-init-width=\"1198\" data-width=\"548\" decoding=\"async\" height=\"204\" loading=\"lazy\" src=\"\/blog\/wp-content\/uploads\/2025\/01\/Screenshot-2025-01-16-at-7.32.21\u202fPM.png\" style=\"aspect-ratio: auto 1198 \/ 446;\" title=\"Screenshot 2025-01-16 at 7.32.21\u202fPM\" width=\"548\"\/><\/span><\/div>\n<div class=\"thrv_wrapper thrv_text_element\">\n<p>\u00a0The idea is to use <em>@DataProvider<\/em> TestNg annotation that will contain an \u2018Object\u2019 array holding 3 test user names<\/p>\n<\/div>\n<div class=\"thrv_wrapper tve_image_caption\" data-css=\"tve-u-1946f6e0cbd\" style=\"\"><span class=\"tve_image_frame\"><img alt=\"\" class=\"tve_image wp-image-3432\" data-css=\"tve-u-1946f6e209d\" data-height=\"237\" data-id=\"3432\" data-init-height=\"584\" data-init-width=\"1198\" data-width=\"487\" decoding=\"async\" height=\"237\" loading=\"lazy\" src=\"\/blog\/wp-content\/uploads\/2025\/01\/Screenshot-2025-01-16-at-7.33.27\u202fPM.png\" style=\"aspect-ratio: auto 1198 \/ 584;\" title=\"Screenshot 2025-01-16 at 7.33.27\u202fPM\" width=\"487\"\/><\/span><\/div>\n<div class=\"thrv_wrapper thrv_text_element\">\n<p>\u00a0This \u2018dataProvider\u2019 can than be used in our @Test method (\u2018username\u2019 will capture multiple user ids during runtime)<\/p>\n<\/div>\n<div class=\"thrv_wrapper tve_image_caption\" data-css=\"tve-u-1946f6eaca3\" style=\"\"><span class=\"tve_image_frame\"><img alt=\"\" class=\"tve_image wp-image-3433\" data-css=\"tve-u-1946f6ebc34\" data-height=\"312\" data-id=\"3433\" data-init-height=\"702\" data-init-width=\"1198\" data-width=\"532\" decoding=\"async\" height=\"312\" loading=\"lazy\" src=\"\/blog\/wp-content\/uploads\/2025\/01\/Screenshot-2025-01-16-at-7.34.07\u202fPM.png\" style=\"aspect-ratio: auto 1198 \/ 702;\" title=\"Screenshot 2025-01-16 at 7.34.07\u202fPM\" width=\"532\"\/><\/span><\/div>\n<div class=\"thrv_wrapper thrv_text_element\">\n<p>\u00a0Complete code shown below<\/p>\n<\/div>\n<div class=\"thrv_wrapper tve_image_caption\" data-css=\"tve-u-1946f6f4901\" style=\"\"><span class=\"tve_image_frame\"><img alt=\"\" class=\"tve_image wp-image-3434\" data-css=\"tve-u-1946f6f60f9\" data-height=\"432\" data-id=\"3434\" data-init-height=\"974\" data-init-width=\"1198\" data-width=\"531\" decoding=\"async\" height=\"432\" loading=\"lazy\" src=\"\/blog\/wp-content\/uploads\/2025\/01\/Screenshot-2025-01-16-at-7.34.44\u202fPM.png\" style=\"aspect-ratio: auto 1198 \/ 974;\" title=\"Screenshot 2025-01-16 at 7.34.44\u202fPM\" width=\"531\"\/><\/span><\/div>\n<div class=\"thrv_wrapper thrv_text_element\">\n<p>\u00a0Execute.<\/p>\n<p>Test executes 3 times (once for each test id)\u00a0<\/p>\n<\/div>\n<div class=\"thrv_wrapper tve_image_caption\" data-css=\"tve-u-1946f6fe194\" style=\"\"><span class=\"tve_image_frame\"><img alt=\"\" class=\"tve_image wp-image-3435\" data-css=\"tve-u-1946f6ff966\" data-height=\"539\" data-id=\"3435\" data-init-height=\"1196\" data-init-width=\"1198\" data-width=\"540\" decoding=\"async\" height=\"539\" loading=\"lazy\" src=\"\/blog\/wp-content\/uploads\/2025\/01\/Screenshot-2025-01-16-at-7.35.30\u202fPM.png\" style=\"aspect-ratio: auto 1198 \/ 1196;\" title=\"Screenshot 2025-01-16 at 7.35.30\u202fPM\" width=\"540\"\/><\/span><\/div>\n<div class=\"thrv_wrapper tve_image_caption\" data-css=\"tve-u-1946f70ba7f\" style=\"\"><span class=\"tve_image_frame\"><img alt=\"\" class=\"tve_image wp-image-3436\" data-css=\"tve-u-1946f70cc7f\" data-height=\"676\" data-id=\"3436\" data-init-height=\"1522\" data-init-width=\"1198\" data-width=\"532\" decoding=\"async\" height=\"676\" loading=\"lazy\" src=\"\/blog\/wp-content\/uploads\/2025\/01\/Screenshot-2025-01-16-at-7.36.20\u202fPM.png\" style=\"aspect-ratio: auto 1198 \/ 1522;\" title=\"Screenshot 2025-01-16 at 7.36.20\u202fPM\" width=\"532\"\/><\/span><\/div>\n<div class=\"thrv_wrapper thrv_text_element\">\n<p>So this way we can perform data driven testing using playwright.<\/p>\n<p><strong>Parameterize Test using dataProvider in separate file<\/strong><\/p>\n<p>If you want, the test data can also be kept in a separate file<\/p>\n<\/div>\n<div class=\"thrv_wrapper tve_image_caption\" data-css=\"tve-u-1946f7149d8\" style=\"\"><span class=\"tve_image_frame\"><img alt=\"\" class=\"tve_image wp-image-3437\" data-css=\"tve-u-1946f715fc0\" data-height=\"359\" data-id=\"3437\" data-init-height=\"848\" data-init-width=\"1198\" data-width=\"507\" decoding=\"async\" height=\"359\" loading=\"lazy\" src=\"\/blog\/wp-content\/uploads\/2025\/01\/Screenshot-2025-01-16-at-7.37.05\u202fPM.png\" style=\"aspect-ratio: auto 1198 \/ 848;\" title=\"Screenshot 2025-01-16 at 7.37.05\u202fPM\" width=\"507\"\/><\/span><\/div>\n<div class=\"thrv_wrapper thrv_text_element\">\n<p>\u00a0Comment the below piece of code from previous code<\/p>\n<\/div>\n<div class=\"thrv_wrapper tve_image_caption\" data-css=\"tve-u-1946f71e4fd\" style=\"\"><span class=\"tve_image_frame\"><img alt=\"\" class=\"tve_image wp-image-3438\" data-css=\"tve-u-1946f71fcfb\" data-height=\"555\" data-id=\"3438\" data-init-height=\"1398\" data-init-width=\"1198\" data-width=\"476\" decoding=\"async\" height=\"555\" loading=\"lazy\" src=\"\/blog\/wp-content\/uploads\/2025\/01\/Screenshot-2025-01-16-at-7.37.38\u202fPM.png\" style=\"aspect-ratio: auto 1198 \/ 1398;\" title=\"Screenshot 2025-01-16 at 7.37.38\u202fPM\" width=\"476\"\/><\/span><\/div>\n<div class=\"thrv_wrapper thrv_text_element\">\n<p>Execute.\u00a0<\/p>\n<p>Test gets executed 3 times<\/p>\n<\/div>\n<div class=\"thrv_wrapper tve_image_caption\" data-css=\"tve-u-1946f727d4a\" style=\"\"><span class=\"tve_image_frame\"><img alt=\"\" class=\"tve_image wp-image-3439\" data-css=\"tve-u-1946f7298e9\" data-height=\"588\" data-id=\"3439\" data-init-height=\"1262\" data-init-width=\"1198\" data-width=\"558\" decoding=\"async\" height=\"588\" loading=\"lazy\" src=\"\/blog\/wp-content\/uploads\/2025\/01\/Screenshot-2025-01-16-at-7.38.22\u202fPM.png\" style=\"aspect-ratio: auto 1198 \/ 1262;\" title=\"Screenshot 2025-01-16 at 7.38.22\u202fPM\" width=\"558\"\/><\/span><\/div>\n<div class=\"thrv_wrapper tve_image_caption\" data-css=\"tve-u-1946f72f52c\" style=\"\"><span class=\"tve_image_frame\"><img alt=\"\" class=\"tve_image wp-image-3440\" data-css=\"tve-u-1946f730bed\" data-height=\"294\" data-id=\"3440\" data-init-height=\"638\" data-init-width=\"1198\" data-width=\"552\" decoding=\"async\" height=\"294\" loading=\"lazy\" src=\"\/blog\/wp-content\/uploads\/2025\/01\/Screenshot-2025-01-16-at-7.38.55\u202fPM.png\" style=\"aspect-ratio: auto 1198 \/ 638;\" title=\"Screenshot 2025-01-16 at 7.38.55\u202fPM\" width=\"552\"\/><\/span><\/div>\n<div class=\"thrv_wrapper thrv_text_element\">\n<p>This is how data driven testing can be performed.<\/p>\n<p><strong>Source code (dataProvider in same file)<\/strong><\/p>\n<p><strong>package<\/strong> com.rsa.playwrightjava;<\/p>\n<p><\/p>\n<p><strong>import<\/strong> org.testng.annotations.AfterMethod;<\/p>\n<p><strong>import<\/strong> org.testng.annotations.BeforeMethod;<\/p>\n<p><strong>import<\/strong> org.testng.annotations.DataProvider;<\/p>\n<p><strong>import<\/strong> org.testng.annotations.Test;<\/p>\n<p><strong>import<\/strong> com.microsoft.playwright.Browser;<\/p>\n<p><strong>import<\/strong> com.microsoft.playwright.BrowserContext;<\/p>\n<p><strong>import<\/strong> com.microsoft.playwright.BrowserType;<\/p>\n<p><strong>import<\/strong> com.microsoft.playwright.Page;<\/p>\n<p><strong>import<\/strong> com.microsoft.playwright.Playwright;<\/p>\n<p><strong>public<\/strong><br \/>\n<strong>class<\/strong> Blog24_dataProviderSameFile {<\/p>\n<p><strong>private<\/strong> Browser browser;<\/p>\n<p><strong>private<\/strong> BrowserContext browserContext;<\/p>\n<p><strong>private<\/strong> Page page;<\/p>\n<p><\/p>\n<p>@BeforeMethod<\/p>\n<p><strong>public<\/strong><br \/>\n<strong>void<\/strong> setUp() {<\/p>\n<p>Playwright pw = Playwright.<em>create<\/em>();\u00a0<\/p>\n<p>browser = pw.chromium().launch(<strong>new<\/strong> BrowserType.LaunchOptions().setHeadless(<strong>false<\/strong>));<\/p>\n<p>browserContext = browser.newContext();\u00a0<\/p>\n<p>page = browserContext.newPage();<\/p>\n<p>}<\/p>\n<p><\/p>\n<p>@Test(dataProvider = &#8220;TestData&#8221;)<\/p>\n<p><strong>public<\/strong><br \/>\n<strong>void<\/strong> test1(String username) {<\/p>\n<p>page.navigate(&#8220;http:\/\/zero.webappsecurity.com\/login.html&#8221;);\u00a0<\/p>\n<p>page.locator(&#8220;#user_login&#8221;).fill(username);<\/p>\n<p>}<\/p>\n<p>@DataProvider(name=&#8221;TestData&#8221;)<\/p>\n<p><strong>public<\/strong> Object[] getData(){<\/p>\n<p>Object[][] obj = <strong>new<\/strong> Object[][] {<\/p>\n<p>{&#8220;TestUser1&#8221;},<\/p>\n<p>{&#8220;TestUser2&#8221;},<\/p>\n<p>{&#8220;TestUser3&#8221;}<\/p>\n<p>};<\/p>\n<p><strong>return<\/strong> obj;<\/p>\n<p>}<\/p>\n<p>@AfterMethod<\/p>\n<p><strong>public<\/strong><br \/>\n<strong>void<\/strong> tearDown() {<\/p>\n<p>\/\/browser.close();<\/p>\n<p>\/\/page.close();<\/p>\n<p>}<\/p>\n<p><\/p>\n<p>}<\/p>\n<p><\/p>\n<p><strong>Source code (dataProvider in different file)<\/strong><\/p>\n<p><strong>package<\/strong> com.rsa.playwrightjava;<\/p>\n<p><\/p>\n<p><strong>import<\/strong> org.testng.annotations.AfterMethod;<\/p>\n<p><strong>import<\/strong> org.testng.annotations.BeforeMethod;<\/p>\n<p><strong>import<\/strong> org.testng.annotations.DataProvider;<\/p>\n<p><strong>import<\/strong> org.testng.annotations.Test;<\/p>\n<p><strong>import<\/strong> com.microsoft.playwright.Browser;<\/p>\n<p><strong>import<\/strong> com.microsoft.playwright.BrowserContext;<\/p>\n<p><strong>import<\/strong> com.microsoft.playwright.BrowserType;<\/p>\n<p><strong>import<\/strong> com.microsoft.playwright.Page;<\/p>\n<p><strong>import<\/strong> com.microsoft.playwright.Playwright;<\/p>\n<p><strong>public<\/strong><br \/>\n<strong>class<\/strong> Blog24_dataProviderExternalFile {<\/p>\n<p><strong>private<\/strong> Browser browser;<\/p>\n<p><strong>private<\/strong> BrowserContext browserContext;<\/p>\n<p><strong>private<\/strong> Page page;<\/p>\n<p>@BeforeMethod<\/p>\n<p><strong>public<\/strong><br \/>\n<strong>void<\/strong> setUp() {<\/p>\n<p>Playwright pw = Playwright.<em>create<\/em>();\u00a0<\/p>\n<p>browser = pw.chromium().launch(<strong>new<\/strong> BrowserType.LaunchOptions().setHeadless(<strong>false<\/strong>));<\/p>\n<p>browserContext = browser.newContext();\u00a0<\/p>\n<p>page = browserContext.newPage();<\/p>\n<p>}<\/p>\n<p>@Test(dataProvider = &#8220;TestData&#8221;, dataProviderClass = Blog24_DataProvider.<strong>class<\/strong>)<\/p>\n<p><strong>public<\/strong><br \/>\n<strong>void<\/strong> test1(String username) {<\/p>\n<p>page.navigate(&#8220;http:\/\/zero.webappsecurity.com\/login.html&#8221;);\u00a0<\/p>\n<p>page.locator(&#8220;#user_login&#8221;).fill(username);<\/p>\n<p>}<\/p>\n<p>\/*<\/p>\n<p>@DataProvider(name=&#8221;TestData&#8221;)<\/p>\n<p>public Object[] getData(){<\/p>\n<p>Object[][] obj = new Object[][] {<\/p>\n<p>{&#8220;TestUser1&#8221;},<\/p>\n<p>{&#8220;TestUser2&#8221;},<\/p>\n<p>{&#8220;TestUser3&#8221;}<\/p>\n<p>};<\/p>\n<p>return obj;<\/p>\n<p>}<\/p>\n<p>*\/<\/p>\n<p>@AfterMethod<\/p>\n<p><strong>public<\/strong><br \/>\n<strong>void<\/strong> tearDown() {<\/p>\n<p>\/\/browser.close();<\/p>\n<p>\/\/page.close();<\/p>\n<p>}<\/p>\n<p><\/p>\n<p>}<\/p>\n<p><\/p>\n<p><strong>Source code (external dataProvider)<\/strong><\/p>\n<p><strong>package<\/strong> com.rsa.playwrightjava;<\/p>\n<p><\/p>\n<p><strong>import<\/strong> org.testng.annotations.DataProvider;<\/p>\n<p><\/p>\n<p><strong>public<\/strong><br \/>\n<strong>class<\/strong> Blog24_DataProvider {<\/p>\n<p>@DataProvider(name=&#8221;TestData&#8221;)<\/p>\n<p><strong>public<\/strong> Object[] getData(){<\/p>\n<p>Object[][] obj = <strong>new<\/strong> Object[][] {<\/p>\n<p>{&#8220;TestUser1&#8221;},<\/p>\n<p>{&#8220;TestUser2&#8221;},<\/p>\n<p>{&#8220;TestUser3&#8221;}<\/p>\n<p>};<\/p>\n<p><strong>return<\/strong> obj;<\/p>\n<p>}<\/p>\n<p><\/p>\n<p>}<\/p>\n<p><\/p>\n<p>Thanks.<\/p>\n<\/div>\n<\/div>\n<\/div>\n<div class=\"tcb_flag\" style=\"display: none\"><\/div>\n<\/section>\n<p><\/body><\/html><\/p>\n","protected":false},"excerpt":{"rendered":"<p>In this blog we will be utilizing Playwright Java for the execution of Data Driven Testing with ease. Topics that we will cover: Parameterize Test using dataProvider in same file Parameterize Test using dataProvider in separate file Source code (dataProvider in same file) Source code (dataProvider in different file) Source code (external dataProvider) Parameterize Test [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[5,9],"tags":[],"class_list":["post-3426","post","type-post","status-publish","format-standard","hentry","category-java","category-playwright","post-wrapper","thrv_wrapper"],"_links":{"self":[{"href":"https:\/\/rahulshettyacademy.com\/blog\/index.php\/wp-json\/wp\/v2\/posts\/3426","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/rahulshettyacademy.com\/blog\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/rahulshettyacademy.com\/blog\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/rahulshettyacademy.com\/blog\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/rahulshettyacademy.com\/blog\/index.php\/wp-json\/wp\/v2\/comments?post=3426"}],"version-history":[{"count":0,"href":"https:\/\/rahulshettyacademy.com\/blog\/index.php\/wp-json\/wp\/v2\/posts\/3426\/revisions"}],"wp:attachment":[{"href":"https:\/\/rahulshettyacademy.com\/blog\/index.php\/wp-json\/wp\/v2\/media?parent=3426"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/rahulshettyacademy.com\/blog\/index.php\/wp-json\/wp\/v2\/categories?post=3426"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/rahulshettyacademy.com\/blog\/index.php\/wp-json\/wp\/v2\/tags?post=3426"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}