{"id":9606,"date":"2025-01-26T19:19:19","date_gmt":"2025-01-26T19:19:19","guid":{"rendered":"http:\/\/putridparrot.com\/blog\/?p=9606"},"modified":"2025-01-26T19:19:19","modified_gmt":"2025-01-26T19:19:19","slug":"adding-playwright-to-a-react-web-app","status":"publish","type":"post","link":"https:\/\/putridparrot.com\/blog\/adding-playwright-to-a-react-web-app\/","title":{"rendered":"Adding Playwright to a React web app."},"content":{"rendered":"<p>Playwright is an automation testing framework for the web. Let&#8217;s add it to our React app. and demonstrate how to use it<\/p>\n<p><strong>Installation<\/strong><\/p>\n<ul>\n<li>Install Playwright\n<pre class=\"brush: plain; title: ; notranslate\" title=\"\">\r\nyarn create playwright\r\n<\/pre>\n<\/li>\n<li>You&#8217;ll be asked where to put your end-to-end tests, default is <em>e2e<\/em> so let&#8217;s stick with that<\/li>\n<li>Next, you&#8217;ll be asked whether to add a GitHub actions workflow, default is <em>N<<\/em> but I want them, so selected <em>Y<\/em><\/li>\n<li>Now you&#8217;re asked whether to install Playwright browsers, default is <em>Y<\/em> so let&#8217;s stick with that<\/li>\n<li>Now Playwright is downloaded and installed<\/l>\n<\/ul>\n<p><strong>Writing Tests<\/strong><\/p>\n<p>Within the folder we set for our tests, I used the default <em>e2e<\/em> we can start adding our *.spec.ts test files, for example here&#8217;s a simple example test just to check the title on my web app.<\/p>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\r\nimport { test, expect } from &#039;@playwright\/test&#039;;\r\n\r\ntest.beforeEach(async ({ page }) =&gt; {\r\n  \/\/ obviously needs changing to your deployed web app, but\r\n  \/\/ fine for local testing\r\n  await page.goto(&#039;http:\/\/localhost:3000\/&#039;);\r\n});\r\n\r\ntest(&#039;Ensure title is as expected&#039;, async ({ page }) =&gt; {\r\n\r\n  await expect(page).toHaveTitle(\/My Web App\/);\r\n  await page.getByText(&#039;End Sat Dec 31 2022&#039;).click();\r\n});\r\n<\/pre>\n<p>In the above we simple create a <em>test<\/em> and using playwright we automate testing of the web app.<\/p>\n<p>Now to run this, as I&#8217;m using React, add to package.json the following to the scripts section<\/p>\n<pre class=\"brush: plain; title: ; notranslate\" title=\"\">\r\n&quot;playwright_test&quot;: &quot;playwright test&quot;,\r\n&quot;playwright_report&quot;: &quot;playwright show-report&quot;,\r\n<\/pre>\n<p>Now we can run <em>yarn playwright_test<\/em> to run the tests within <em>e2e<\/em> or whatever your test folder was named. <\/p>\n","protected":false},"excerpt":{"rendered":"<p>Playwright is an automation testing framework for the web. Let&#8217;s add it to our React app. and demonstrate how to use it Installation Install Playwright yarn create playwright You&#8217;ll be asked where to put your end-to-end tests, default is e2e so let&#8217;s stick with that Next, you&#8217;ll be asked whether to add a GitHub actions [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[340],"tags":[],"class_list":["post-9606","post","type-post","status-publish","format-standard","hentry","category-playwright"],"jetpack_sharing_enabled":true,"jetpack_featured_media_url":"","_links":{"self":[{"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/posts\/9606","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/comments?post=9606"}],"version-history":[{"count":5,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/posts\/9606\/revisions"}],"predecessor-version":[{"id":11264,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/posts\/9606\/revisions\/11264"}],"wp:attachment":[{"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/media?parent=9606"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/categories?post=9606"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/tags?post=9606"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}