<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Let’s Playwright - Medium]]></title>
        <description><![CDATA[用 30 天的時間，探索 Playwright 的奧妙之處 - Medium]]></description>
        <link>https://medium.com/lets-playwright?source=rss----a1615354cbdd---4</link>
        <image>
            <url>https://cdn-images-1.medium.com/proxy/1*TGH72Nnw24QL3iV9IOm4VA.png</url>
            <title>Let’s Playwright - Medium</title>
            <link>https://medium.com/lets-playwright?source=rss----a1615354cbdd---4</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Fri, 04 Dec 2026 22:04:30 GMT</lastBuildDate>
        <atom:link href="https://medium.com/feed/lets-playwright" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[Let’s Playwright: Day 03 — Playwright Configuration]]></title>
            <link>https://medium.com/lets-playwright/lets-playwright-day-03-playwright-configuration-fcab5aec5a79?source=rss----a1615354cbdd---4</link>
            <guid isPermaLink="false">https://medium.com/p/fcab5aec5a79</guid>
            <category><![CDATA[playwright-test]]></category>
            <category><![CDATA[e2e-testing]]></category>
            <dc:creator><![CDATA[Lala's programming, parenting and life.]]></dc:creator>
            <pubDate>Mon, 07 Nov 2022 02:27:23 GMT</pubDate>
            <atom:updated>2022-11-07T02:27:23.802Z</atom:updated>
            <content:encoded><![CDATA[<h3>Let’s Playwright: Day 03 — Playwright Configuration</h3><h3>Intro</h3><p>透過設定組態(Configuration)的方式，可用以提供測試運行對應適合的環境，也可以為測試錄製 Video 或擷取 Spanshot。</p><p>你可以使用 configuration file （組態設定檔 ) 做<strong>全域</strong>的設定，也可以在test file (測試程式碼)中做<strong>區域</strong>的設定。</p><h3>Global configuration</h3><ul><li>建立一個檔名固定為 playwright.config.js 的檔案，即可在檔案中設定 config。</li></ul><pre>// @ts-check</pre><pre>/** @type {import(&#39;@playwright/test&#39;).PlaywrightTestConfig} */<br>const config = {<br>  use: {<br>    headless: false,<br>    viewport: { width: 1280, height: 720 },<br>    ignoreHTTPSErrors: true,<br>    video: &#39;on-first-retry&#39;,<br>  },<br>};</pre><pre>module.exports = config;</pre><ul><li>運行測試 — 不指定 configuration file 的位置</li></ul><p>預設會抓取 playwright.config.js</p><pre>npx playwright test</pre><ul><li>運行測試 — 使用 --config 來指定configuration file 的位置</li></ul><pre>npx playwright test --config=tests/my.config.js</pre><h3>Local configuration</h3><p>使用 test.use 在區域的組態設定。</p><ul><li>設定在 test file 中，做<strong>檔案</strong>「區域」的組態設定</li></ul><pre>// example.spec.js<br>const { test, expect } = require(&#39;@playwright/test&#39;);<br>// Run tests in this file with portrait-like viewport.<br>test.use({ viewport: { width: 600, height: 900 } });<br>test(&#39;my portrait test&#39;, async ({ page }) =&gt; {<br>  // ...<br>});</pre><ul><li>設定在 test.describe 中，做<strong>測試案例群組</strong>「區域」的組態設定</li></ul><pre>// example.spec.js<br>const { test, expect } = require(&#39;@playwright/test&#39;);<br>test.describe(&#39;locale block&#39;, () =&gt; {<br>  // Run tests in this describe block with portrait-like viewport.<br>  test.use({ viewport: { width: 600, height: 900 } });<br>  test(&#39;my portrait test&#39;, async ({ page }) =&gt; {<br>    // ...<br>  });<br>});</pre><h3>TestOptions</h3><p>以下是各種情境的常用設定選項，通常會設定在 configuration file 做全域的設定。</p><p>這些設定通常會與 Browser 或 BrowserContext 有關，所以都會使用 use 來做設定。</p><ul><li>actionTimeout - 設定 Playwright 的 Timeout(以毫秒為單位)。默認為 0。</li><li>baseURL - 用於瀏覽頁面的基本 URL。設定完成後，就允許使用部分的路徑進行導航，例如 page.goto(&#39;/settings&#39;)</li><li>browserName - 要運行測試的瀏覽器名稱 - chromium、firefox或webkit。</li><li>bypassCSP - 切換繞過內容安全策略。</li><li>channel - 要使用的瀏覽器 Channel。eg. 你可以指定 chromium 是要運行 Google Chrome (chrome) 還是 Microsoft Edge (msedge)。</li><li>headless - 是否以headless運行 (背景執行網頁，不開啟瀏覽器)。</li><li>viewport - 用於設定網頁的可視區塊。</li><li>colorScheme - 模擬 prefers-colors-scheme 媒體功能，可用的值為light、dark、no-preference。</li><li>locale - 模擬用戶語系，例如en-GB,de-DE等。</li><li>permissions - 授予頁面的權限列表。</li><li>timezoneId - 更改時區。</li></ul><pre>// @ts-check</pre><pre>/** @type {import(&#39;@playwright/test&#39;).PlaywrightTestConfig} */<br>const config = {<br>  use: {<br>    baseURL: &#39;http://localhost:3000&#39;,<br>    browserName: &#39;firefox&#39;,<br>    headless: true,<br>  },<br>};</pre><pre>module.exports = config;</pre><h3>Multiple browsers — Projects Config</h3><p>當需要一次運行多個瀏覽器時，可以使用 projects 的設定。以下的範例，會在 Chromium、Firefox 和 WebKit 中運行每個測試。</p><pre>// playwright.config.js<br>// @ts-check<br>const { devices } = require(&#39;@playwright/test&#39;);</pre><pre>/** @type {import(&#39;@playwright/test&#39;).PlaywrightTestConfig} */<br>const config = {<br>  projects: [<br>    {<br>      name: &#39;chromium&#39;,<br>      use: { ...devices[&#39;Desktop Chrome&#39;] },<br>    },<br>    {<br>      name: &#39;firefox&#39;,<br>      use: { ...devices[&#39;Desktop Firefox&#39;] },<br>    },<br>    {<br>      name: &#39;webkit&#39;,<br>      use: { ...devices[&#39;Desktop Safari&#39;] },<br>    },<br>  ],<br>};</pre><pre>module.exports = config;</pre><ul><li>不指定--project，預設會運行所以在 projects 中設定瀏覧器。</li></ul><pre>npx playwright test</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/948/0*Hk4l9BN4wJME8Wgg.png" /></figure><ul><li>指定--project，就會只運行指定的瀏覽器。</li></ul><pre>npx playwright test --project=firefox</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/948/0*wFeGzHXb2tTp7DCM.png" /></figure><h3>Network Config</h3><p>以下是可以用來做網路設定的選項，通常會設定在 configuration file 做全域的設定。</p><ul><li>acceptDownloads - 是否自動下載所有附件，默認為true.</li><li>extraHTTPHeaders - 每次 Request 跟著一起發送的additional HTTP headers。所有 Header value 都必須是 String。</li><li>httpCredentials - HTTP 身份驗證的憑證。</li><li>ignoreHTTPSErrors - 是否忽略 HTTPS 錯誤。</li><li>offline - 是否模擬網路離線。</li><li>proxy - 用於測試中所有頁面的 Proxy Setting。</li></ul><h4>Network mocking</h4><p>針對 Network 的 Request 狀況模擬，則不需要使用 Config 做設定，只要直接在測試程式碼中，在 Test Case 或是 Test Hooks 中使用 Route 做 Network Request 的操作模擬。</p><ul><li>使用在 Test Hooks — eg. 在每次測試案例執行前阻止任何的 CSS Request</li></ul><pre>// example.spec.js<br>const { test, expect } = require(&#39;@playwright/test&#39;);</pre><pre>test.beforeEach(async ({ context }) =&gt; {<br>  // Block any css requests for each test in this file.<br>  await context.route(/.css/, route =&gt; route.abort());<br>});</pre><pre>test(&#39;loads page without css&#39;, async ({ page }) =&gt; {<br>  await page.goto(&#39;https://playwright.dev&#39;);<br>  // ... test goes here<br>});</pre><ul><li>使用在 Test Case — eg. 在測試案例中阻止 png/jpeg 的 Request</li></ul><pre>// example.spec.js<br>const { test, expect } = require(&#39;@playwright/test&#39;);</pre><pre>test(&#39;loads page without images&#39;, async ({ page }) =&gt; {<br>  // Block png and jpeg images.<br>  await page.route(/(png|jpeg)$/, route =&gt; route.abort());</pre><pre>  await page.goto(&#39;https://playwright.dev&#39;);<br>  // ... test goes here<br>});</pre><h3>Automatic Screenshots</h3><p>你可以設定 screenshot，控制 Playwright 擷取 Screenshots，預設 screentshot 的設定是 off。</p><ul><li>off - 不須擷圖</li><li>on - 在每個測試案例結束後擷圖</li><li>only-on-failure - 在每個測試案例結束且失敗後擷圖</li></ul><p>Screenshots 會出現在測試輸出目錄裡，通常是 test-results</p><pre>// @ts-check</pre><pre>/** @type {import(&#39;@playwright/test&#39;).PlaywrightTestConfig} */<br>const config = {<br>  use: {<br>    screenshot: &#39;only-on-failure&#39;,<br>  },<br>};</pre><pre>module.exports = config;</pre><h3>Record Video</h3><p>你可以設定 video，控制 Playwright 側錄測試操作影片，預設 video 的設定是 off。</p><ul><li>off - 不須錄影</li><li>on - 在每個測試案例執行時錄影</li><li>retain-on-failure - 在每個測試案例執行時錄影，但執行成功的影片會被移除，只保留失敗的。</li><li>on-first-retry - 僅在第一次重試測試時錄影。</li></ul><p>video 會出現在測試輸出目錄裡，通常是 test-results</p><pre>// @ts-check</pre><pre>/** @type {import(&#39;@playwright/test&#39;).PlaywrightTestConfig} */<br>const config = {<br>  use: {<br>    video: &#39;on-first-retry&#39;,<br>  },<br>};</pre><pre>module.exports = config;</pre><h3>Record test trace</h3><p>Playwright Test 可以在運行測試時產生測試軌跡，然後透過 Trace Viewer 可查看，且獲取執行當下的詳細資訊。預設 trace 的設定是 off。</p><ul><li>off - 不記錄測試軌跡。</li><li>on - 記錄每次測試的軌跡。</li><li>retain-on-failure - 記錄每次測試的軌跡，但執行成功的軌跡會被移除，只保留失敗的。</li><li>on-first-retry - 僅在第一次重試測試時記錄測試軌跡。</li></ul><p>Trace files 會出現在測試輸出目錄裡，通常是 test-results</p><pre>// @ts-check</pre><pre>/** @type {import(&#39;@playwright/test&#39;).PlaywrightTestConfig} */<br>const config = {<br>  use: {<br>    trace: &#39;retain-on-failure&#39;,<br>  },<br>};</pre><pre>module.exports = config;</pre><h3>TestConfig</h3><p>跟測試本身運行有關的設定，通常是 top-level 的 option，不需要把它放在 use 裡。</p><ul><li>forbidOnly - 當測試被標記成test.only或test.describe.only時，當錯誤發生時是否退出測試。對 CI 很有用。</li><li>retries - 每次測試的最大重試次數。</li><li>testDir - 包含測試檔案的目錄。</li><li>testIgnore - 匹配到字串或正則式的檔案，不會做為測試文件執行。例如，‘<strong>/test-assets/</strong>’ 將忽略 test-assets 目錄中的任何文件。</li><li>testMatch - 只有匹配到字串或正則式的檔案，才會做為測試文件執行。預設的情況下，Playwright Test 會查找運行.*(test|spec).(js|ts|mjs)</li><li>timeout - 每次測試的時間（以毫秒為單位）。</li><li>workers - 用於設定執行 parallelizing tests 時，最大可運行的 concurrent worker 數量。也可以設置為 CPU Core 的百分比，例如 50%。</li></ul><pre>// playwright.config.js<br>// @ts-check</pre><pre>/** @type {import(&#39;@playwright/test&#39;).PlaywrightTestConfig} */<br>const config = {<br>  // Look for test files in the &quot;tests&quot; directory, relative to this configuration file<br>  testDir: &#39;tests&#39;,</pre><pre>  // Each test is given 30 seconds<br>  timeout: 30000,</pre><pre>  // Forbid test.only on CI<br>  forbidOnly: !!process.env.CI,</pre><pre>  // Two retries for each test<br>  retries: 2,</pre><pre>  // Limit the number of workers on CI, use default locally<br>  workers: process.env.CI ? 2 : undefined,</pre><pre>  use: {<br>    // Configure browser and context here<br>  },<br>};</pre><pre>module.exports = config;</pre><h3>Next</h3><p>當運行測試的環境組態檔案設定完成，Playwright 就會按照設定的方式去運行測試。接下來就要更進一步學習，如何寫出適合的 Playwright 的測試案例。</p><h3>Reference</h3><p><a href="https://playwright.dev/docs/test-configuration">https://playwright.dev/docs/test-configuration</a></p><p><a href="https://playwright.dev/docs/api/class-testoptions">https://playwright.dev/docs/api/class-testoptions</a></p><p><a href="https://playwright.dev/docs/api/class-testconfig">https://playwright.dev/docs/api/class-testconfig</a></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=fcab5aec5a79" width="1" height="1" alt=""><hr><p><a href="https://medium.com/lets-playwright/lets-playwright-day-03-playwright-configuration-fcab5aec5a79">Let’s Playwright: Day 03 — Playwright Configuration</a> was originally published in <a href="https://medium.com/lets-playwright">Let’s Playwright</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Let’s Playwright: Day 02 — Getting started — use Command]]></title>
            <link>https://medium.com/lets-playwright/lets-playwright-day-02-getting-started-use-command-87cdd0d0b595?source=rss----a1615354cbdd---4</link>
            <guid isPermaLink="false">https://medium.com/p/87cdd0d0b595</guid>
            <category><![CDATA[playwright-test]]></category>
            <category><![CDATA[e2e-testing]]></category>
            <dc:creator><![CDATA[Lala's programming, parenting and life.]]></dc:creator>
            <pubDate>Wed, 02 Nov 2022 02:22:04 GMT</pubDate>
            <atom:updated>2022-11-02T02:22:04.149Z</atom:updated>
            <content:encoded><![CDATA[<h3>Let’s Playwright: Day 02 — Getting started — use Command</h3><h3>Installation</h3><h4>安裝指令</h4><p>為你的專案安裝 playwright，可以使用 npm 或 yarn 來安裝。</p><pre>npm init playwright@latest</pre><pre>or</pre><pre>yarn create playwright</pre><p>執行以上指令後，並選擇你需要的配置</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/960/0*xcUeL38GriIONEMJ.gif" /></figure><ul><li>選擇要使用 TypeScript 還是 JavaScript (預設是 TypeScript)</li><li>選擇放置測試程式碼檔案的路徑 (預設是 tests or e2e)</li><li>增加 GitHub Actions Workflow (預設是 false)</li><li>是否安裝 Playwright browsers (預設是 true)</li></ul><h4>安裝後檔案結構</h4><figure><img alt="" src="https://cdn-images-1.medium.com/max/804/1*-Wcr6QTFz_dMSEP-cpQAuQ.png" /><figcaption>Playwright 安裝後檔案結構</figcaption></figure><ul><li>playwright.config.js 是可以為 Playwright 做配置設定的地方</li><li>tests 資料夾，會有一個寫好的 DEMO 測試，讓你試著執行看看環境是否安裝運行成功。</li><li>tests-examples 資料夾，則是 Playwright 提供的 TODO 頁面應用測試，你可以參考裡面的寫法，在初始的時候學習如何操作 Playwright，如果不需要也可以直接移除。</li></ul><h4>運行測試</h4><p>在 playwright.config.js 的 testDir，指定好要運行測試的檔案目錄，就可以執行以下指令來運行測試。</p><pre>npx playwright test</pre><p>預設是使用 headless (不打開瀏覽器) 的方式來執行，測試過程及結果都會被顯示在 Terminal 上。</p><h4>HTML Test Report</h4><p>一旦測試執行完成，會建置一個 HTML Reprot，用以顯示完整的測試報告。</p><p>你可以使用測試報告的上方工具列，查詢你要找的測試案例名稱，或是用分類標籤去檢視 — 通過的測試、失敗的測試、不穩定的(Flaky)測試和跳過的測試。</p><p>你可以點擊每個測試項目，檢視其步驟或是發生的錯誤。在預設的情況下，測試失敗的時候，測試報告會自動打開。</p><pre>npx playwright show-report</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*FrHqplpZyIs4E7yP.png" /></figure><h3>Writing Tests</h3><p>觀察安裝 Playwright 時自動產生的範例測試檔案，可以發現一個測試主要會包含三種要素 — assertions、locators 和 selectors</p><pre>// @ts-check<br>const { test, expect } = require(&#39;@playwright/test&#39;);</pre><pre>test(&#39;homepage has Playwright in title and get started link linking to the intro page&#39;, async ({ page }) =&gt; {<br>  await page.goto(&#39;https://playwright.dev/&#39;);</pre><pre>// Expect a title &quot;to contain&quot; a substring.<br>  await expect(page).toHaveTitle(/Playwright/);</pre><pre>// create a locator<br>  const getStarted = page.getByText(&#39;Get Started&#39;);</pre><pre>// Expect an attribute &quot;to be strictly equal&quot; to the value.<br>  await expect(getStarted).toHaveAttribute(&#39;href&#39;, &#39;/docs/intro&#39;);</pre><pre>// Click the get started link.<br>  await getStarted.click();</pre><pre>// Expects the URL to contain intro.<br>  await expect(page).toHaveURL(/.*intro/);<br>});</pre><blockquote><em>在 VS Code 中使用 JavaScript 時，可在每個測試文件的開頭添加 </em><em>// @ts-check 以獲取自動類型檢查。</em></blockquote><h4>Assertions</h4><p>斷言（Assertion）指的是程式進行到某個時間點，斷定其必然是某種狀態。在寫測試的時候，經常需要檢查值是否滿足指定的條件，目的是為了標示與驗證程式開發者預期的結果－當程式執行到斷言的位置時，對應的斷言應該為真。若斷言不為真時，程式會中止執行，並給出錯誤訊息。</p><p>Playwright 使用並擴展 Jest expect函式庫，做為其測試程式碼的斷言機制。</p><pre>await expect(page).toHaveTitle(/Playwright/);</pre><p>之後會有對 Assertions 更詳細的介紹。</p><h4>Locators</h4><p>定位點 (Locators) 代表的是在頁面上找到的元素，取得定位點後，可以用它來判斷該定位點的內容是否符合預期，也可以用它來做頁面操作。</p><p>Playwright 提供一系列可用的 Locators 語法。如果要自訂，Locators，可使用 page.locator(selector [,options])。</p><p>之後會有對 Locators 更詳細的介紹。</p><pre>const getStarted = page.getByText(&#39;Get Started&#39;);</pre><pre>await expect(getStarted).toHaveAttribute(&#39;href&#39;, &#39;/docs/installation&#39;);<br>await getStarted.click();</pre><h4>Selectors</h4><p>選擇器 (Selectors) 是用以找到 Locators 的一組字串，它可以是 Text、CSS、XPath 等等。(在實務開發上比較常使用 CSS Selectors)</p><p>之後會有對 Selectors 更詳細的介紹。</p><pre>await expect(page.getByText(&#39;Installation&#39;)).toBeVisible();</pre><h4>Test Isolation</h4><p>Test Fixture 用以建置每個測試的執行環境，Playwright 的測試有一組內建的 Test Fixture — page、context、browser 及 browserName。，每個測試案例的 Test Fixture 都是獨立分開的。</p><pre>test(&#39;basic test&#39;, async ({ page }) =&gt; {<br>...</pre><p>之後會有對 Test Fixture 更詳細的介紹。</p><h4>Using Test Hooks</h4><p>Playwright 提供了多樣化的 Test Hooks，如下</p><ul><li>test.describe - 用以群組化測試案例</li><li>test.beforeEach、test.afterEach - 在每次測試案例前 or 後執行</li><li>test.beforeAll、test.afterAll- 在所有測試案例前 or 後執行</li></ul><p>之後會有對 Test Hooks 更詳細的介紹。</p><pre>// @ts-check<br>const { test, expect } = require(&quot;@playwright/test&quot;);</pre><pre>test.describe(&quot;navigation&quot;, () =&gt; {<br>  test.beforeEach(async ({ page }) =&gt; {<br>    // Go to the starting url before each test.<br>    await page.goto(&quot;https://playwright.dev/&quot;);<br>  });</pre><pre>test(&quot;main navigation&quot;, async ({ page }) =&gt; {<br>    // Assertions use the expect API.<br>    await expect(page).toHaveURL(&quot;https://playwright.dev/&quot;);<br>  });<br>});</pre><h4>test V.S. test.describe</h4><p>當測試需求越來越複雜時，可針對需求來分類測試案例，將相關的測試放在同一個群組區塊內，此時就會用到describe和test區塊。</p><ul><li>test 區塊：即測試案例 (test case)，使用 Assertions 驗證程式執行結果是否符合預期</li><li>describe 區塊：將多個相關的 test 區塊放在一起，便於組織測試案例</li></ul><h3>Running Tests</h3><h4>使用指令測試</h4><p>使用指令執行單個測試、一組測試或所有測試。</p><ul><li>執行所有測試</li></ul><pre>npx playwright test</pre><ul><li>執行單個測試檔案</li></ul><pre>npx playwright test landing-page.spec.js</pre><ul><li>執行測試檔案子目錄</li></ul><pre>npx playwright test tests/todo-page/ tests/landing-page/</pre><ul><li>執行測試檔案檔名含有指定字串</li></ul><pre>npx playwright test landing login</pre><ul><li>執行測試案列的 title 為指定字串</li></ul><pre>npx playwright test -g &quot;add a todo item&quot;</pre><h4>使用指令測試並啟用中斷點 Debug</h4><ul><li>偵錯所有測試</li></ul><pre>npx playwright test --debug</pre><ul><li>偵錯單個測試檔案</li></ul><pre>npx playwright test example.spec.js --debug</pre><h3>Test Generator</h3><p>Playwright 具有開箱即用生成測試的能力，是快速開始測試的好方法。</p><p>它將打開二個視窗，一個是瀏覽器視窗，您可以在此視窗與您希望測試的網站進行頁面元素互動操作，另一個是 Playwright Inspector 視窗，您可以在其中錄製您的測試、複製自動生成的測試、清除或更改測試的代碼。</p><h4>Running Codegen</h4><ul><li>語法</li></ul><pre>npx playwright codegen your-test-site-url</pre><ul><li>使用 Codegen 為 playwright.dev 自動生成測試</li></ul><pre>npx playwright codegen playwright.dev</pre><ul><li>開始操作 Playwright Inspector 錄製自動生成的測試</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/960/0*Oi7Vq9jf_FImnUUz.gif" /></figure><h3>Next</h3><p>在看過本篇跟前篇介紹的 Playwright 測試初學入門後，接下來就要再更深入介紹 Playwright 的測試開發細節。</p><h3>Reference</h3><p><a href="https://playwright.dev/docs/intro">https://playwright.dev/docs/intro</a></p><p><a href="https://playwright.dev/docs/writing-tests">https://playwright.dev/docs/writing-tests</a></p><p><a href="https://playwright.dev/docs/running-tests">https://playwright.dev/docs/running-tests</a></p><p><a href="https://playwright.dev/docs/codegen-intro">https://playwright.dev/docs/codegen-intro</a></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=87cdd0d0b595" width="1" height="1" alt=""><hr><p><a href="https://medium.com/lets-playwright/lets-playwright-day-02-getting-started-use-command-87cdd0d0b595">Let’s Playwright: Day 02 — Getting started — use Command</a> was originally published in <a href="https://medium.com/lets-playwright">Let’s Playwright</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Let’s Playwright: Day 01 — Getting started — VS Code]]></title>
            <link>https://medium.com/lets-playwright/lets-playwright-day-01-getting-started-vs-code-8094a66c220?source=rss----a1615354cbdd---4</link>
            <guid isPermaLink="false">https://medium.com/p/8094a66c220</guid>
            <category><![CDATA[low-code]]></category>
            <category><![CDATA[playwright-test]]></category>
            <category><![CDATA[e2e-testing]]></category>
            <category><![CDATA[vscode-extension]]></category>
            <dc:creator><![CDATA[Lala's programming, parenting and life.]]></dc:creator>
            <pubDate>Tue, 01 Nov 2022 01:37:52 GMT</pubDate>
            <atom:updated>2022-11-01T02:38:49.096Z</atom:updated>
            <content:encoded><![CDATA[<h3>Let’s Playwright: Day 01 — Getting started — VS Code</h3><h3>Playwright Test for VSCode</h3><p>就算不使用 Playwright 來為網頁做自動化的端到端測試，平常也可以使用 Playwright 做為自動化操作瀏覽器的工具。</p><p>假設現行的案子，臨時需要增加需求，這個需求有很多操作步驟，例如，需要填寫完客戶資料後，才可以刷卡購買，這時候就可以使用 Playwright 協助快速的把前面填寫資料的部分自動化填入，簡化我們每次開發測試的操作。</p><p>微軟提供了 <a href="https://marketplace.visualstudio.com/items?itemName=ms-playwright.playwright">VSCode Extension — Playwright Test for VSCode</a>，可以將 Playwright 整合到您的 VS Code 開發工作流程中。 它為開發者提供了不離開 VSCode Editor 的「沉浸式」開發體驗。</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/922/1*6cLL4FfKYD-NsXmqApZjIA.png" /><figcaption><a href="https://marketplace.visualstudio.com/items?itemName=ms-playwright.playwright">VSCode Extension — Playwright Test for VSCode</a></figcaption></figure><p>這個 Extension 同時也提供了 low-code 的開發體驗方式。在以前寫測試案例的痛點，就是「需求單位大多不知道怎麼寫測試；IT人員則不清楚實際要能滿足需求的測試結果。」他們必須與 IT 人員來回溝通、彼此間有不切實際的期待而容易產生落差。如今透過 low-code 工具做視覺化的指引、簡單的點選/拖拉功能等，讓沒有專業程式背景、但熟知產業知識的專案成員，也可以協助做到測試案例開發，提升整體的開發生產力。</p><h3>Installation</h3><p>VSCode Extension 安裝完成後，使用 cmd + shift + p，叫出命名選擇區如下，輸入以下指令</p><pre>&gt;Install Playwright</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/754/0*PUZRndusczCs2MQ4.png" /></figure><p>選擇想使用的瀏覽器，稍後可以在playwright.config.js，更改配置使用。</p><blockquote><em>若專案的CI使用 GitHub Actions，還可以選擇添加 GitHub Actions，以便在每次拉取請求或推送時運行。</em></blockquote><figure><img alt="" src="https://cdn-images-1.medium.com/max/721/0*5xxlXqhjfMqxDmh8.png" /></figure><p>Extension 會自動檢測是否安裝了 Playwright 套件及載入要運行的瀏覧器。它會選擇第一個勾選的瀏覧器做為預設。在 VS Code 的 Task Runner，可以更改預設瀏覧器，用以在多個或單個不同瀏覽器中運行測試。</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/960/0*0wmnuvtGGYxmMvMv.gif" /></figure><h3>安裝完成 Playwright 後的檔案結構</h3><pre>playwright.config.ts<br>package.json<br>package-lock.json<br>tests/<br>  example.spec.ts<br>tests-examples/<br>  demo-todo-app.spec.ts</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/923/0*KL9xf_KFVKgG1kZl.png" /></figure><h4>playwright.config.js</h4><p>運行 Playwright 的設定檔案</p><h4>**/*.spec.js</h4><p>要運行測試案例程式碼的檔案</p><p>觀察安裝完 Playwright 後的檔案結構，你會發現有二個可以運行測試的目錄</p><ul><li>tests - 這裡提供一個運行測試案例檔案的範本。你可以修改此檔案內容，或是在此目錄下新增 *.spec.js。</li><li>tests-examples - 這裡提供一個完整運行https://demo.playwright.dev/todomvc 此網站的測試案例檔案的範本。當你還不熟悉 Playwrights 時，你可以留存用以參考測試案例的寫法，若不需要的話，也可以刪除該目錄。</li></ul><h3>Playwright 設定檔，可指定運行測試的目錄</h3><pre>testDir: ./你要運行測試的目錄</pre><ul><li>這裡示範切換運行測試的目錄，使用的是預設自動產生的測試案例檔案做為示範 — test or tests-examples，你也可以依照需求更改運行測試的目錄名稱 eg.e2e，然後在 e2e 目錄下新增 *.spec.js 檔案。</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/960/0*ZBYrcV48X_tdi5wC.gif" /></figure><h3>Running Tests</h3><p>你可以在測試程式碼的test 旁邊，按下綠色的三角執行按鈕。(Run Single Test)，或是在 Testing Sidebar 上，按下灰色的三角執行按鈕。(Run Single/Multiple Tests)</p><h3>Run Single Test</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/960/0*ejej4HeTo742HHy6.gif" /></figure><h3>Run Multiple Tests</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/960/0*wB8cJsn8W4JZYdHb.gif" /></figure><h3>Show browsers</h3><p>如果你希望運行測試時，能夠同時看到瀏覽器的執行步驟，那就在 Testing Sidebar 下方勾選 Show browser。</p><p>執行完成後，可以勾選 Close all browsers，來關閉瀏覧器。</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/960/0*vFHXw-Gc7rUrO1Rv.gif" /></figure><h3>Pick Selectors</h3><p>當你在撰寫測試程式碼，但你可能是既有專案的新加入開發者，在尚未了解整體專案架構前，你可以使用 Pick Selectors 功能，快速的取得網頁上元素的選取器。</p><p>在 Testing Sidebar 下方點選 Pick Selecotors，再到運行測試的網頁做元素的點選，你會看到灰色的 Tips 顯示該元素的選取器，點擊它就會在 VSCode 的命名選擇區，顯示選取器的內容，你可以直接按下 Enter，就會幫你複製到剪貼簿用以接下來貼上到程式碼中，或者你也可以按下 Esc 放棄該次選取內容。</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/960/0*KAb8CSm39jlN0yCg.gif" /></figure><h3>Debugging Tests</h3><p>在撰寫 Playwright 的測試程式碼時，搭配 VSCode Extension 對應的中斷點除錯方法，會更有效率的幫助做測試程式碼開發。</p><p>在程式行數的部分，先設置好要監看的中斷點 (breakpoint)，然後右鍵選單選擇「對測試偵錯 (Debug Test)」，你會發現測試程式會執行到中斷點後做暫停，此時畫面會停留在中斷點程式碼要操作的畫面元素上且會被突顯出來。</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/960/0*eKdktjy0hyy37Wxp.gif" /></figure><h3>Tune Selectors</h3><p>當測試程式碼對應的選取器 (Selector) 不理想時，你也可以使用 Debug 的功能來 Tune 你的 Selector，選取器改變的結果也會被突顯出來。</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/960/0*Ajk-EZTMOPKSImw6.gif" /></figure><h3>Auto Generating Tests</h3><p>這個 Extension 也可以幫我們輕鬆快速的自動產生測試程式碼。</p><p>在 Testing Sidebar 下方點選 Record tests，他會產生一個新的測試檔案後，開啟瀏覧器，然後你可以在網址列輸入URL，導頁至你想要測試的頁面，並且在畫面上做你想要被錄製的操作，所有的操作都會被產生對應的測試程式碼。</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/960/0*LhEtDz_nnZ32jtY7.gif" /></figure><h3>Next</h3><p>如果不想使用 VSCode Extension 來操作 Playwright，也可以使用指令的方式來完成相對應的功能。接下來就會介紹如何使用指令的方式來幫助開發 Playwright 測試程式碼。</p><h3>Reference</h3><p><a href="https://github.com/microsoft/playwright-vscode">https://github.com/microsoft/playwright-vscode</a></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=8094a66c220" width="1" height="1" alt=""><hr><p><a href="https://medium.com/lets-playwright/lets-playwright-day-01-getting-started-vs-code-8094a66c220">Let’s Playwright: Day 01 — Getting started — VS Code</a> was originally published in <a href="https://medium.com/lets-playwright">Let’s Playwright</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Let’s Playwright: Day 00  瀏覽器自動化工具]]></title>
            <link>https://medium.com/lets-playwright/lets-playwright-day-00-%E7%80%8F%E8%A6%BD%E5%99%A8%E8%87%AA%E5%8B%95%E5%8C%96%E5%B7%A5%E5%85%B7-ee48cd361984?source=rss----a1615354cbdd---4</link>
            <guid isPermaLink="false">https://medium.com/p/ee48cd361984</guid>
            <category><![CDATA[playwright-test]]></category>
            <dc:creator><![CDATA[Lala's programming, parenting and life.]]></dc:creator>
            <pubDate>Fri, 28 Oct 2022 01:18:09 GMT</pubDate>
            <atom:updated>2022-10-28T01:28:03.931Z</atom:updated>
            <content:encoded><![CDATA[<h3>Let’s Playwright: Day 00 — 瀏覽器自動化工具</h3><h3>簡介</h3><p>Playwright 是微軟開發的一個 Open Souce 的瀏覽器自動化工具，可以選擇 Chromium、Firefox、WebKit 瀏覽器，目前有 Python、JavaScript、TypeScript、.NET、Java 的 API 可供使用。</p><p>它擁有以下幾點特性</p><h3>跨平台</h3><p>MacOS、Linux、Windows 皆可用。</p><h3>跨瀏覽器</h3><p>可操控 WebKit、Firefox、Chromium 三大瀏覽器。</p><h3>跨語言</h3><p>可使用 TypeScript, JavaScript, Python, .NET, Java 等語言，用相似的方式開發 API。</p><h3>Headless and Headed</h3><p>支援 Headless (不開啟瀏覽器) 和 Headed (開啟瀏覧器) 的測試模式。Headed 適合用於偵錯，Headless 啟動快速，適合用於 CI/Cloud 執行。</p><h3>開發穩定活躍</h3><p>2020年5年6日，Playwright 發布第一個正式版本。初期 Commit 次數 50～100次 / 每日，目前 Commit 次數是 30～100 次 / 每週。Github Fork/Start 數已經超過 2K/40K，正處於穩定成長中。</p><p><a href="https://github.com/microsoft/playwright/graphs/commit-activity">https://github.com/microsoft/playwright/graphs/commit-activity</a></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/700/0*-bTCHnJN2MLJLqVi.png" /><figcaption><a href="https://github.com/microsoft/playwright/graphs/commit-activity">https://github.com/microsoft/playwright/graphs/commit-activity</a></figcaption></figure><h3>與 VSCode 完美支援</h3><p>VSCode 已成為地表最強的程式開發編輯器，目前官方提供的 Playwright Extension 目前支援使用 JS/TS 的語法，在 VSCode 中做沈浸式的開發體驗。</p><h3>可錄製網頁操作，產生網頁自動化瀏覽的語法</h3><p>操作 Playwright 的人員，不一定要是熟悉該網站架構的開發者，它提供了簡易的操作，可以幫我們把操作網頁的過程用指定的程式語言錄製下來，快速的產生網頁自動化瀏覽的語法。</p><h3>Next</h3><p>接下來的 30 天，就讓我們一起來探索 Playwright 的奧妙之處吧！</p><h3>Reference</h3><p><a href="https://playwright.dev/">https://playwright.dev/</a></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=ee48cd361984" width="1" height="1" alt=""><hr><p><a href="https://medium.com/lets-playwright/lets-playwright-day-00-%E7%80%8F%E8%A6%BD%E5%99%A8%E8%87%AA%E5%8B%95%E5%8C%96%E5%B7%A5%E5%85%B7-ee48cd361984">Let’s Playwright: Day 00  瀏覽器自動化工具</a> was originally published in <a href="https://medium.com/lets-playwright">Let’s Playwright</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
    </channel>
</rss>