{"id":3426,"date":"2025-01-16T14:00:24","date_gmt":"2025-01-16T14:00:24","guid":{"rendered":"https:\/\/rahulshettyacademy.com\/blog\/?p=3426"},"modified":"2025-01-17T14:15:06","modified_gmt":"2025-01-17T14:15:06","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><em>In this blog we will be utilizing Playwright Java for the execution of Data Driven Testing with ease.<\/em><\/p>\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 &nbsp;<\/strong><\/p>\n<p>On certain occasions, we might have the need to perform a Test using various sets of data.&nbsp;<\/p>\n<p>To understand this, navigate to<\/p>\n<p><a href=\"http:\/\/zero.webappsecurity.com\/login.html\" target=\"_blank\" rel=\"noopener\">http:\/\/zero.webappsecurity.com\/login.html<\/a>&nbsp;<\/p>\n<p><span><img decoding=\"async\" alt=\"\" data-id=\"3429\" width=\"523\" data-init-width=\"1198\" height=\"223\" data-init-height=\"510\" title=\"Screenshot 2025-01-16 at 7.31.24\u202fPM\" loading=\"lazy\" src=\"https:\/\/rahulshettyacademy.com\/blog\/wp-content\/uploads\/2025\/01\/Screenshot-2025-01-16-at-7.31.24\u202fPM.png\" data-width=\"523\" data-height=\"223\" style=\"aspect-ratio: auto 1198 \/ 510;\"><\/span><\/p>\n<p>&nbsp;It 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;.&nbsp;<\/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.&nbsp;<\/p>\n<p>Let us see how to perform data driven testing.&nbsp;<\/p>\n<p>Inspect \u2018Login\u2019 field&nbsp;<\/p>\n<p><span><img decoding=\"async\" alt=\"\" data-id=\"3431\" width=\"548\" data-init-width=\"1198\" height=\"204\" data-init-height=\"446\" title=\"Screenshot 2025-01-16 at 7.32.21\u202fPM\" loading=\"lazy\" src=\"https:\/\/rahulshettyacademy.com\/blog\/wp-content\/uploads\/2025\/01\/Screenshot-2025-01-16-at-7.32.21\u202fPM.png\" data-width=\"548\" data-height=\"204\" style=\"aspect-ratio: auto 1198 \/ 446;\"><\/span><\/p>\n<p>&nbsp;The idea is to use <em>@DataProvider<\/em> TestNg annotation that will contain an \u2018Object\u2019 array holding 3 test user names<\/p>\n<p><span><img decoding=\"async\" alt=\"\" data-id=\"3432\" width=\"487\" data-init-width=\"1198\" height=\"237\" data-init-height=\"584\" title=\"Screenshot 2025-01-16 at 7.33.27\u202fPM\" loading=\"lazy\" src=\"https:\/\/rahulshettyacademy.com\/blog\/wp-content\/uploads\/2025\/01\/Screenshot-2025-01-16-at-7.33.27\u202fPM.png\" data-width=\"487\" data-height=\"237\" style=\"aspect-ratio: auto 1198 \/ 584;\"><\/span><\/p>\n<p>&nbsp;This \u2018dataProvider\u2019 can than be used in our @Test method (\u2018username\u2019 will capture multiple user ids during runtime)<\/p>\n<p><span><img decoding=\"async\" alt=\"\" data-id=\"3433\" width=\"532\" data-init-width=\"1198\" height=\"312\" data-init-height=\"702\" title=\"Screenshot 2025-01-16 at 7.34.07\u202fPM\" loading=\"lazy\" src=\"https:\/\/rahulshettyacademy.com\/blog\/wp-content\/uploads\/2025\/01\/Screenshot-2025-01-16-at-7.34.07\u202fPM.png\" data-width=\"532\" data-height=\"312\" style=\"aspect-ratio: auto 1198 \/ 702;\"><\/span><\/p>\n<p>&nbsp;Complete code shown below<\/p>\n<p><span><img decoding=\"async\" alt=\"\" data-id=\"3434\" width=\"531\" data-init-width=\"1198\" height=\"432\" data-init-height=\"974\" title=\"Screenshot 2025-01-16 at 7.34.44\u202fPM\" loading=\"lazy\" src=\"https:\/\/rahulshettyacademy.com\/blog\/wp-content\/uploads\/2025\/01\/Screenshot-2025-01-16-at-7.34.44\u202fPM.png\" data-width=\"531\" data-height=\"432\" style=\"aspect-ratio: auto 1198 \/ 974;\"><\/span><\/p>\n<p>&nbsp;Execute.<\/p>\n<p>Test executes 3 times (once for each test id)&nbsp;<\/p>\n<p><span><img decoding=\"async\" alt=\"\" data-id=\"3435\" width=\"540\" data-init-width=\"1198\" height=\"539\" data-init-height=\"1196\" title=\"Screenshot 2025-01-16 at 7.35.30\u202fPM\" loading=\"lazy\" src=\"https:\/\/rahulshettyacademy.com\/blog\/wp-content\/uploads\/2025\/01\/Screenshot-2025-01-16-at-7.35.30\u202fPM.png\" data-width=\"540\" data-height=\"539\" style=\"aspect-ratio: auto 1198 \/ 1196;\"><\/span><span><img decoding=\"async\" alt=\"\" data-id=\"3436\" width=\"532\" data-init-width=\"1198\" height=\"676\" data-init-height=\"1522\" title=\"Screenshot 2025-01-16 at 7.36.20\u202fPM\" loading=\"lazy\" src=\"https:\/\/rahulshettyacademy.com\/blog\/wp-content\/uploads\/2025\/01\/Screenshot-2025-01-16-at-7.36.20\u202fPM.png\" data-width=\"532\" data-height=\"676\" style=\"aspect-ratio: auto 1198 \/ 1522;\"><\/span><\/p>\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<p><span><img decoding=\"async\" alt=\"\" data-id=\"3437\" width=\"507\" data-init-width=\"1198\" height=\"359\" data-init-height=\"848\" title=\"Screenshot 2025-01-16 at 7.37.05\u202fPM\" loading=\"lazy\" src=\"https:\/\/rahulshettyacademy.com\/blog\/wp-content\/uploads\/2025\/01\/Screenshot-2025-01-16-at-7.37.05\u202fPM.png\" data-width=\"507\" data-height=\"359\" style=\"aspect-ratio: auto 1198 \/ 848;\"><\/span><\/p>\n<p>&nbsp;Comment the below piece of code from previous code<\/p>\n<p><span><img decoding=\"async\" alt=\"\" data-id=\"3438\" width=\"476\" data-init-width=\"1198\" height=\"555\" data-init-height=\"1398\" title=\"Screenshot 2025-01-16 at 7.37.38\u202fPM\" loading=\"lazy\" src=\"https:\/\/rahulshettyacademy.com\/blog\/wp-content\/uploads\/2025\/01\/Screenshot-2025-01-16-at-7.37.38\u202fPM.png\" data-width=\"476\" data-height=\"555\" style=\"aspect-ratio: auto 1198 \/ 1398;\"><\/span><\/p>\n<p>Execute.&nbsp;<\/p>\n<p>Test gets executed 3 times<\/p>\n<p><span><img decoding=\"async\" alt=\"\" data-id=\"3439\" width=\"558\" data-init-width=\"1198\" height=\"588\" data-init-height=\"1262\" title=\"Screenshot 2025-01-16 at 7.38.22\u202fPM\" loading=\"lazy\" src=\"https:\/\/rahulshettyacademy.com\/blog\/wp-content\/uploads\/2025\/01\/Screenshot-2025-01-16-at-7.38.22\u202fPM.png\" data-width=\"558\" data-height=\"588\" style=\"aspect-ratio: auto 1198 \/ 1262;\"><\/span><span><img decoding=\"async\" alt=\"\" data-id=\"3440\" width=\"552\" data-init-width=\"1198\" height=\"294\" data-init-height=\"638\" title=\"Screenshot 2025-01-16 at 7.38.55\u202fPM\" loading=\"lazy\" src=\"https:\/\/rahulshettyacademy.com\/blog\/wp-content\/uploads\/2025\/01\/Screenshot-2025-01-16-at-7.38.55\u202fPM.png\" data-width=\"552\" data-height=\"294\" style=\"aspect-ratio: auto 1198 \/ 638;\"><\/span><\/p>\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>\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><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>\n<p>@BeforeMethod<\/p>\n<p><strong>public<\/strong><strong>void<\/strong> setUp() {<\/p>\n<p>Playwright pw = Playwright.<em>create<\/em>();&nbsp;<\/p>\n<p>browser = pw.chromium().launch(<strong>new<\/strong> BrowserType.LaunchOptions().setHeadless(<strong>false<\/strong>));<\/p>\n<p>browserContext = browser.newContext();&nbsp;<\/p>\n<p>page = browserContext.newPage();<\/p>\n<p>}<\/p>\n<\/p>\n<p>@Test(dataProvider = &#8220;TestData&#8221;)<\/p>\n<p><strong>public<\/strong><strong>void<\/strong> test1(String username) {<\/p>\n<p>page.navigate(&#8220;http:\/\/zero.webappsecurity.com\/login.html&#8221;);&nbsp;<\/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><strong>void<\/strong> tearDown() {<\/p>\n<p>\/\/browser.close();<\/p>\n<p>\/\/page.close();<\/p>\n<p>}<\/p>\n<\/p>\n<p>}<\/p>\n<\/p>\n<p><strong>Source code (dataProvider in different file)<\/strong><\/p>\n<p><strong>package<\/strong> com.rsa.playwrightjava;<\/p>\n<\/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><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><strong>void<\/strong> setUp() {<\/p>\n<p>Playwright pw = Playwright.<em>create<\/em>();&nbsp;<\/p>\n<p>browser = pw.chromium().launch(<strong>new<\/strong> BrowserType.LaunchOptions().setHeadless(<strong>false<\/strong>));<\/p>\n<p>browserContext = browser.newContext();&nbsp;<\/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><strong>void<\/strong> test1(String username) {<\/p>\n<p>page.navigate(&#8220;http:\/\/zero.webappsecurity.com\/login.html&#8221;);&nbsp;<\/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><strong>void<\/strong> tearDown() {<\/p>\n<p>\/\/browser.close();<\/p>\n<p>\/\/page.close();<\/p>\n<p>}<\/p>\n<\/p>\n<p>}<\/p>\n<\/p>\n<p><strong>Source code (external dataProvider)<\/strong><\/p>\n<p><strong>package<\/strong> com.rsa.playwrightjava;<\/p>\n<\/p>\n<p><strong>import<\/strong> org.testng.annotations.DataProvider;<\/p>\n<\/p>\n<p><strong>public<\/strong><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>\n<p>}<\/p>\n<\/p>\n<p>Thanks.<\/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":750,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[41],"tags":[],"class_list":["post-3426","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","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":6,"href":"https:\/\/rahulshettyacademy.com\/blog\/index.php\/wp-json\/wp\/v2\/posts\/3426\/revisions"}],"predecessor-version":[{"id":3861,"href":"https:\/\/rahulshettyacademy.com\/blog\/index.php\/wp-json\/wp\/v2\/posts\/3426\/revisions\/3861"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/rahulshettyacademy.com\/blog\/index.php\/wp-json\/wp\/v2\/media\/750"}],"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}]}}