{"id":317,"date":"2024-10-10T14:39:58","date_gmt":"2024-10-10T14:39:58","guid":{"rendered":"https:\/\/www.369usa.com\/blog\/?p=317"},"modified":"2024-10-10T15:34:20","modified_gmt":"2024-10-10T15:34:20","slug":"codegen-playwright","status":"publish","type":"post","link":"https:\/\/www.369usa.com\/blog\/codegen-playwright\/","title":{"rendered":"Codegen &amp; Playwright"},"content":{"rendered":"\n<p>Resources:<\/p>\n\n\n\n<p><a href=\"https:\/\/playwright.dev\/docs\/codegen\">https:\/\/playwright.dev\/docs\/codegen<\/a><\/p>\n\n\n\n<p>Installation:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>npm init playwright@latest<\/code><\/pre>\n\n\n\n<p>During installation, you&#8217;ll be prompted with several options. Here\u2019s an example of what I selected:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>Initializing project in '.'\nDo you want to use TypeScript or JavaScript? \u00b7 JavaScript\nWhere to put your end-to-end tests? \u00b7 tests\nAdd a GitHub Actions workflow? (y\/N) \u00b7 false\nInstall Playwright browsers (can be done manually via 'npx playwright install')? (Y\/n) \u00b7 true<\/code><\/pre>\n\n\n\n<p>Once installed, you can use the following commands from your project\u2019s root directory:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>npx playwright test\n    Runs the end-to-end tests.\n\nnpx playwright test --ui\n    Starts the interactive UI mode.\n\nnpx playwright test --project=chromium\n    Runs the tests only on Desktop Chrome.\n\nnpx playwright test example\n    Runs the tests in a specific file.\n\nnpx playwright test --debug\n    Runs the tests in debug mode.\n\nnpx playwright codegen\n    Auto generate tests with Codegen.<\/code><\/pre>\n\n\n\n<p>The installation also comes with an example folder, <code>tests-examples<\/code>, which is really handy for getting a quick grasp of how to use this tool. Running the command <code>npx playwright test example<\/code> will execute example test cases, and you\u2019ll see something like 6\/6 tests passing.<\/p>\n\n\n\n<p>The command <code>npx playwright codegen<\/code> opens up an empty Codegen browser, but you can also specify a URL, like this:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>npx playwright codegen \"http:\/\/study.369usa.com\/demo.html\"<\/code><\/pre>\n\n\n\n<p>After playing around in the Codegen browser, it generates a list of actions in clean JavaScript for me:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>import { test, expect } from '@playwright\/test';\n\ntest('test', async ({ page }) => {\n  await page.goto('http:\/\/study.369usa.com\/demo.html');\n  await page.getByLabel('Number A:').click();\n  await page.getByLabel('Number A:').fill('1');\n  await page.getByLabel('Number B:').click();\n  await page.getByLabel('Number B:').fill('2');\n  await page.getByRole('button', { name: 'Add Numbers' }).click();\n  await expect(page.getByTestId('result')).toContainText('3');\n});<\/code><\/pre>\n\n\n\n<p>A special mention for the assertion parts: at the top of the Playwright Inspector, you&#8217;ll find three buttons\u2014<strong>Assert Visibility<\/strong>, <strong>Assert Text<\/strong>, and <strong>Assert Value<\/strong>\u2014to help with adding assertions without the need of writing a single line of your own code.<\/p>\n\n\n\n<p>Once the test is successfully generated, simply copy and paste it into a test file, name it <code>xxxx.spec.js<\/code>, and run the command<code> npx playwright test<\/code>. As expected, the single test I just created passed!<\/p>\n\n\n\n<p>Additionally, by running:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>npx playwright test --ui<\/code><\/pre>\n\n\n\n<p>You can even see the test steps play out one by one. You can control the steps, go back and forth, and interact with the simulated browser.<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"765\" src=\"https:\/\/www.369usa.com\/blog\/wp-content\/uploads\/2024\/10\/Screen-Shot-2024-10-10-at-11.22.17-AM-1024x765.png\" alt=\"\" class=\"wp-image-337\" srcset=\"https:\/\/www.369usa.com\/blog\/wp-content\/uploads\/2024\/10\/Screen-Shot-2024-10-10-at-11.22.17-AM-1024x765.png 1024w, https:\/\/www.369usa.com\/blog\/wp-content\/uploads\/2024\/10\/Screen-Shot-2024-10-10-at-11.22.17-AM-300x224.png 300w, https:\/\/www.369usa.com\/blog\/wp-content\/uploads\/2024\/10\/Screen-Shot-2024-10-10-at-11.22.17-AM-768x574.png 768w, https:\/\/www.369usa.com\/blog\/wp-content\/uploads\/2024\/10\/Screen-Shot-2024-10-10-at-11.22.17-AM-1536x1148.png 1536w, https:\/\/www.369usa.com\/blog\/wp-content\/uploads\/2024\/10\/Screen-Shot-2024-10-10-at-11.22.17-AM-1200x897.png 1200w, https:\/\/www.369usa.com\/blog\/wp-content\/uploads\/2024\/10\/Screen-Shot-2024-10-10-at-11.22.17-AM.png 2044w\" sizes=\"(max-width: 709px) 85vw, (max-width: 909px) 67vw, (max-width: 1362px) 62vw, 840px\" \/><\/figure>\n\n\n\n<p><\/p>\n\n\n\n<p>I don\u2019t know about you, but I\u2019ve definitely fallen in love with it! <\/p>\n\n\n\n<figure class=\"wp-block-image is-resized\"><img decoding=\"async\" src=\"https:\/\/emoji.slack-edge.com\/T033GCPL1\/imposibru\/15df8203026bda84.png\" alt=\":imposibru:\" style=\"width:87px;height:auto\"\/><\/figure>\n\n\n\n<p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Resources: https:\/\/playwright.dev\/docs\/codegen Installation: During installation, you&#8217;ll be prompted with several options. Here\u2019s an example of what I selected: Once installed, you can use the following commands from your project\u2019s root directory: The installation also comes with an example folder, tests-examples, which is really handy for getting a quick grasp of how to use this tool. &hellip; <a href=\"https:\/\/www.369usa.com\/blog\/codegen-playwright\/\" class=\"more-link\">Continue reading<span class=\"screen-reader-text\"> &#8220;Codegen &amp; Playwright&#8221;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-317","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v23.5 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>Codegen &amp; Playwright - \u7ebd\u7ea6\u7f51\u7ad9\u8bbe\u8ba1\u535a\u5ba2<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.369usa.com\/blog\/codegen-playwright\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Codegen &amp; Playwright - \u7ebd\u7ea6\u7f51\u7ad9\u8bbe\u8ba1\u535a\u5ba2\" \/>\n<meta property=\"og:description\" content=\"Resources: https:\/\/playwright.dev\/docs\/codegen Installation: During installation, you&#8217;ll be prompted with several options. Here\u2019s an example of what I selected: Once installed, you can use the following commands from your project\u2019s root directory: The installation also comes with an example folder, tests-examples, which is really handy for getting a quick grasp of how to use this tool. &hellip; Continue reading &quot;Codegen &amp; Playwright&quot;\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.369usa.com\/blog\/codegen-playwright\/\" \/>\n<meta property=\"og:site_name\" content=\"\u7ebd\u7ea6\u7f51\u7ad9\u8bbe\u8ba1\u535a\u5ba2\" \/>\n<meta property=\"article:published_time\" content=\"2024-10-10T14:39:58+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2024-10-10T15:34:20+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/www.369usa.com\/blog\/wp-content\/uploads\/2024\/10\/Screen-Shot-2024-10-10-at-11.22.17-AM-1024x765.png\" \/>\n<meta name=\"author\" content=\"darkersss\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"darkersss\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"2 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"WebPage\",\"@id\":\"https:\/\/www.369usa.com\/blog\/codegen-playwright\/\",\"url\":\"https:\/\/www.369usa.com\/blog\/codegen-playwright\/\",\"name\":\"Codegen &amp; Playwright - \u7ebd\u7ea6\u7f51\u7ad9\u8bbe\u8ba1\u535a\u5ba2\",\"isPartOf\":{\"@id\":\"https:\/\/www.369usa.com\/blog\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\/\/www.369usa.com\/blog\/codegen-playwright\/#primaryimage\"},\"image\":{\"@id\":\"https:\/\/www.369usa.com\/blog\/codegen-playwright\/#primaryimage\"},\"thumbnailUrl\":\"https:\/\/www.369usa.com\/blog\/wp-content\/uploads\/2024\/10\/Screen-Shot-2024-10-10-at-11.22.17-AM-1024x765.png\",\"datePublished\":\"2024-10-10T14:39:58+00:00\",\"dateModified\":\"2024-10-10T15:34:20+00:00\",\"author\":{\"@id\":\"https:\/\/www.369usa.com\/blog\/#\/schema\/person\/1c260c42af4aadda7f89a0081f6591b6\"},\"breadcrumb\":{\"@id\":\"https:\/\/www.369usa.com\/blog\/codegen-playwright\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/www.369usa.com\/blog\/codegen-playwright\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/www.369usa.com\/blog\/codegen-playwright\/#primaryimage\",\"url\":\"https:\/\/www.369usa.com\/blog\/wp-content\/uploads\/2024\/10\/Screen-Shot-2024-10-10-at-11.22.17-AM.png\",\"contentUrl\":\"https:\/\/www.369usa.com\/blog\/wp-content\/uploads\/2024\/10\/Screen-Shot-2024-10-10-at-11.22.17-AM.png\",\"width\":2044,\"height\":1528},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/www.369usa.com\/blog\/codegen-playwright\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/www.369usa.com\/blog\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Codegen &amp; Playwright\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/www.369usa.com\/blog\/#website\",\"url\":\"https:\/\/www.369usa.com\/blog\/\",\"name\":\"369\u7ebd\u7ea6\u7f51\u7ad9\u8bbe\u8ba1\",\"description\":\"\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/www.369usa.com\/blog\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Person\",\"@id\":\"https:\/\/www.369usa.com\/blog\/#\/schema\/person\/1c260c42af4aadda7f89a0081f6591b6\",\"name\":\"darkersss\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/www.369usa.com\/blog\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/secure.gravatar.com\/avatar\/2c538d84b753eed6f6f714becf9b3955?s=96&d=mm&r=g\",\"contentUrl\":\"https:\/\/secure.gravatar.com\/avatar\/2c538d84b753eed6f6f714becf9b3955?s=96&d=mm&r=g\",\"caption\":\"darkersss\"}}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Codegen &amp; Playwright - \u7ebd\u7ea6\u7f51\u7ad9\u8bbe\u8ba1\u535a\u5ba2","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/www.369usa.com\/blog\/codegen-playwright\/","og_locale":"en_US","og_type":"article","og_title":"Codegen &amp; Playwright - \u7ebd\u7ea6\u7f51\u7ad9\u8bbe\u8ba1\u535a\u5ba2","og_description":"Resources: https:\/\/playwright.dev\/docs\/codegen Installation: During installation, you&#8217;ll be prompted with several options. Here\u2019s an example of what I selected: Once installed, you can use the following commands from your project\u2019s root directory: The installation also comes with an example folder, tests-examples, which is really handy for getting a quick grasp of how to use this tool. &hellip; Continue reading \"Codegen &amp; Playwright\"","og_url":"https:\/\/www.369usa.com\/blog\/codegen-playwright\/","og_site_name":"\u7ebd\u7ea6\u7f51\u7ad9\u8bbe\u8ba1\u535a\u5ba2","article_published_time":"2024-10-10T14:39:58+00:00","article_modified_time":"2024-10-10T15:34:20+00:00","og_image":[{"url":"https:\/\/www.369usa.com\/blog\/wp-content\/uploads\/2024\/10\/Screen-Shot-2024-10-10-at-11.22.17-AM-1024x765.png"}],"author":"darkersss","twitter_card":"summary_large_image","twitter_misc":{"Written by":"darkersss","Est. reading time":"2 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"WebPage","@id":"https:\/\/www.369usa.com\/blog\/codegen-playwright\/","url":"https:\/\/www.369usa.com\/blog\/codegen-playwright\/","name":"Codegen &amp; Playwright - \u7ebd\u7ea6\u7f51\u7ad9\u8bbe\u8ba1\u535a\u5ba2","isPartOf":{"@id":"https:\/\/www.369usa.com\/blog\/#website"},"primaryImageOfPage":{"@id":"https:\/\/www.369usa.com\/blog\/codegen-playwright\/#primaryimage"},"image":{"@id":"https:\/\/www.369usa.com\/blog\/codegen-playwright\/#primaryimage"},"thumbnailUrl":"https:\/\/www.369usa.com\/blog\/wp-content\/uploads\/2024\/10\/Screen-Shot-2024-10-10-at-11.22.17-AM-1024x765.png","datePublished":"2024-10-10T14:39:58+00:00","dateModified":"2024-10-10T15:34:20+00:00","author":{"@id":"https:\/\/www.369usa.com\/blog\/#\/schema\/person\/1c260c42af4aadda7f89a0081f6591b6"},"breadcrumb":{"@id":"https:\/\/www.369usa.com\/blog\/codegen-playwright\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.369usa.com\/blog\/codegen-playwright\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.369usa.com\/blog\/codegen-playwright\/#primaryimage","url":"https:\/\/www.369usa.com\/blog\/wp-content\/uploads\/2024\/10\/Screen-Shot-2024-10-10-at-11.22.17-AM.png","contentUrl":"https:\/\/www.369usa.com\/blog\/wp-content\/uploads\/2024\/10\/Screen-Shot-2024-10-10-at-11.22.17-AM.png","width":2044,"height":1528},{"@type":"BreadcrumbList","@id":"https:\/\/www.369usa.com\/blog\/codegen-playwright\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/www.369usa.com\/blog\/"},{"@type":"ListItem","position":2,"name":"Codegen &amp; Playwright"}]},{"@type":"WebSite","@id":"https:\/\/www.369usa.com\/blog\/#website","url":"https:\/\/www.369usa.com\/blog\/","name":"369\u7ebd\u7ea6\u7f51\u7ad9\u8bbe\u8ba1","description":"","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.369usa.com\/blog\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Person","@id":"https:\/\/www.369usa.com\/blog\/#\/schema\/person\/1c260c42af4aadda7f89a0081f6591b6","name":"darkersss","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.369usa.com\/blog\/#\/schema\/person\/image\/","url":"https:\/\/secure.gravatar.com\/avatar\/2c538d84b753eed6f6f714becf9b3955?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/2c538d84b753eed6f6f714becf9b3955?s=96&d=mm&r=g","caption":"darkersss"}}]}},"_links":{"self":[{"href":"https:\/\/www.369usa.com\/blog\/wp-json\/wp\/v2\/posts\/317"}],"collection":[{"href":"https:\/\/www.369usa.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.369usa.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.369usa.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.369usa.com\/blog\/wp-json\/wp\/v2\/comments?post=317"}],"version-history":[{"count":18,"href":"https:\/\/www.369usa.com\/blog\/wp-json\/wp\/v2\/posts\/317\/revisions"}],"predecessor-version":[{"id":348,"href":"https:\/\/www.369usa.com\/blog\/wp-json\/wp\/v2\/posts\/317\/revisions\/348"}],"wp:attachment":[{"href":"https:\/\/www.369usa.com\/blog\/wp-json\/wp\/v2\/media?parent=317"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.369usa.com\/blog\/wp-json\/wp\/v2\/categories?post=317"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.369usa.com\/blog\/wp-json\/wp\/v2\/tags?post=317"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}