<?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[Stories by Anuchit Charoenkeskit on Medium]]></title>
        <description><![CDATA[Stories by Anuchit Charoenkeskit on Medium]]></description>
        <link>https://medium.com/@anuchit_79065?source=rss-0b1d00f7035f------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*QR7g0i5XAFNGSZsam_EbXw.jpeg</url>
            <title>Stories by Anuchit Charoenkeskit on Medium</title>
            <link>https://medium.com/@anuchit_79065?source=rss-0b1d00f7035f------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Mon, 16 Nov 2026 20:26:45 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@anuchit_79065/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[มาเขียน Page Object Model ใน Playwright ให้สะอาดมากขึ้นด้วย Playwright Fixtures]]></title>
            <link>https://medium.com/@anuchit_79065/%E0%B8%A1%E0%B8%B2%E0%B9%80%E0%B8%82%E0%B8%B5%E0%B8%A2%E0%B8%99-page-object-model-%E0%B9%83%E0%B8%99-playwright-%E0%B9%83%E0%B8%AB%E0%B9%89%E0%B8%AA%E0%B8%B0%E0%B8%AD%E0%B8%B2%E0%B8%94%E0%B8%A1%E0%B8%B2%E0%B8%81%E0%B8%82%E0%B8%B6%E0%B9%89%E0%B8%99%E0%B8%94%E0%B9%89%E0%B8%A7%E0%B8%A2-playwright-fixtures-6997fcbb5063?source=rss-0b1d00f7035f------2</link>
            <guid isPermaLink="false">https://medium.com/p/6997fcbb5063</guid>
            <category><![CDATA[automation-testing]]></category>
            <category><![CDATA[software-testing]]></category>
            <category><![CDATA[playwrights]]></category>
            <category><![CDATA[software-development]]></category>
            <category><![CDATA[testing]]></category>
            <dc:creator><![CDATA[Anuchit Charoenkeskit]]></dc:creator>
            <pubDate>Thu, 19 Jun 2025 10:00:57 GMT</pubDate>
            <atom:updated>2025-06-19T13:47:24.432Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*3aBWcpupoJ8ZmP1l97uMGQ.jpeg" /></figure><p>สวัสดีครับ สำหรับเพื่อนๆที่กำลังใช้ Design Pattern แบบ Page Object Model (แบบ “ปกติ”) ใน Playwright เราจะพบปัญหาหนึ่งที่เราจำเป็นต้องสร้าง Instance ใหม่ในทุกๆไฟล์ของ Test Spec ที่ต้องการใช้งานทุกครั้งครับ</p><p>ซึ่งปัญหานี้เราสามารถใช้ Playwright Fixtures มาช่วยแก้ปัญหาในจุดนี้ครับ</p><p><strong>การเขียน Page Object Model (แบบปกติ)</strong></p><ol><li>เราจะทำการสร้าง Class ของ Page ของแต่ละหน้า ดัง code ด้านล่างนี้ครับ</li></ol><pre>// LoginPage.ts<br>import { expect, type Locator, type Page } from &#39;@playwright/test&#39;<br><br>export class LoginPage {<br> readonly page: Page<br> readonly makeAppointmentLink: Locator<br> readonly usernameInput: Locator<br> readonly passwordInput: Locator<br> readonly loginButton: Locator<br> readonly errorMessage: Locator<br><br> constructor(page: Page) {<br>  this.page = page<br>  this.makeAppointmentLink = page.getByRole(&#39;link&#39;, {<br>   name: &#39;Make Appointment&#39;,<br>  })<br>  this.usernameInput = page.getByLabel(&#39;Username&#39;)<br>  this.passwordInput = page.locator(&#39;#txt-password&#39;)<br>  this.loginButton = page.locator(&#39;#btn-login&#39;)<br>  this.errorMessage = page.locator(&#39;.lead.text-danger&#39;)<br> }<br><br> async navigateToLoginPage() {<br>  await this.page.goto(&#39;https://katalon-demo-cura.herokuapp.com/&#39;)<br>  await this.makeAppointmentLink.click()<br> }<br><br> async login(username: string, password: string) {<br>  await this.usernameInput.fill(username)<br>  await this.passwordInput.fill(password)<br>  await this.loginButton.click()<br> }<br>}</pre><p>2. หลังจากนั้นเราจะทำการ สร้าง Instance ในแต่ละ Test Spec ดัง Code ด้านล่างนี้ครับ</p><pre>// login.spec.ts<br>import { test, expect } from &#39;@playwright/test&#39;<br>import { LoginPage } from &#39;../page-objects/LoginPage&#39;<br><br>test.describe(&#39;POM example - Login feature&#39;, () =&gt; {<br>   let loginPage: LoginPage<br>   test.beforeEach(async ({ page }) =&gt; {<br>     // New Instance loginPage object<br>      loginPage = new LoginPage(page)<br>      <br>      // Navigate to the login page<br>      await loginPage.navigateToLoginPage()<br> })<br><br> test(&#39;POM example - Login successful with valid credentials&#39;, async ({<br>  page,<br> }) =&gt; {<br>    // Perform login with valid credentials<br>    await loginPage.login(&#39;John Doe&#39;, &#39;ThisIsNotAPassword&#39;)<br><br>    // Verify that the user is redirected to the appointment page<br>    await expect(page).toHaveURL(<br>     &#39;https://katalon-demo-cura.herokuapp.com/#appointment&#39;<br>    )<br> })<br><br> test(&#39;POM example - Login failed with invalid credentials&#39;, async ({<br>  page,<br> }) =&gt; {<br>    // Perform login with wrong credentials<br>    // ......<br> })<br>})</pre><pre>// makeAppoinment.spec.ts<br>import { test, expect } from &#39;@playwright/test&#39;<br>import { LoginPage } from &#39;../page-objects/LoginPage&#39;<br>import { MakeAppointmentPage } from &#39;../page-objects/MakeAppointmentPage&#39;<br><br>test.describe(&#39;POM example - Make appointment feature&#39;, () =&gt; {<br>    let loginPage: LoginPage<br>    let makeAppointment: MakeAppointmentPage<br>    test.beforeEach(async ({ page }) =&gt; {<br>    // New Instance loginPage object<br>    loginPage = new LoginPage(page)<br>    makeAppointmentPage = new MakeAppointmentPage(page)<br>    // Navigate to the login page<br>    await loginPage.navigateToLoginPage()<br>    // Perform login with valid credentials<br>    await loginPage.login(&#39;John Doe&#39;, &#39;ThisIsNotAPassword&#39;)<br> })<br><br> test(&#39;POM example - user can make appointment&#39;, async ({<br>  page,<br> }) =&gt; {<br>    await makeAppointmentPage.fillVisitDate(&#39;16/6/2025&#39;)<br>    await makeAppointmentPage.selectFaciltity(&#39;Seoul CURA Healthcare Center&#39;)<br>    // Perform make appointment<br>    // ...<br> })<br>})</pre><h3>Problems</h3><p>จาก Code จะเห็นปัญหาดังนี้</p><ol><li>เราจำเป็นต้องเขียน new LoginPage(page) ในทั้งสองไฟล์ Test ทำให้มีชุด Code ที่ซ้ำกัน</li><li>ยากในการ Maintain ถ้ามีการแก้ไข Constructor ของ LoginPage จำเป็นต้องตามไปแก้ใน Test Spec ทั้งสองไฟล์</li><li>ใช้เวลาในการ Test Setup ที่นานมากขึ้น เพราะทั้งสองไฟล์ต้อง new object ใหม่</li></ol><h3>นำ Playwright Fixtures มาใช้กับ Page Object Model</h3><p>Playwright Fixtures คือระบบที่ Playwright สร้างมาเพื่อช่วยจัดการ Setup Environment ของแต่ละ Test Spec รวมทั้งสามารถช่วยสร้าง Instance Page Object (อย่างเช่น new LoginPage()) ที่ต้องการใช้ซ้ำๆในทุก Test spec ให้ได้อีกด้วย</p><ol><li>วิธีนี้เรายังคงต้องสร้าง Class ของ Page ของแต่ละหน้าเหมือนเดิมครับ</li></ol><pre>// LoginPage.ts<br>import { expect, type Locator, type Page } from &#39;@playwright/test&#39;<br><br>export class LoginPage {<br> readonly page: Page<br> readonly makeAppointmentLink: Locator<br> readonly usernameInput: Locator<br> readonly passwordInput: Locator<br> readonly loginButton: Locator<br> readonly errorMessage: Locator<br><br> constructor(page: Page) {<br>  this.page = page<br>  this.makeAppointmentLink = page.getByRole(&#39;link&#39;, {<br>   name: &#39;Make Appointment&#39;,<br>  })<br>  this.usernameInput = page.getByLabel(&#39;Username&#39;)<br>  this.passwordInput = page.locator(&#39;#txt-password&#39;)<br>  this.loginButton = page.locator(&#39;#btn-login&#39;)<br>  this.errorMessage = page.locator(&#39;.lead.text-danger&#39;)<br> }<br><br> async navigateToLoginPage() {<br>  await this.page.goto(&#39;https://katalon-demo-cura.herokuapp.com/&#39;)<br>  await this.makeAppointmentLink.click()<br> }<br><br> async login(username: string, password: string) {<br>  await this.usernameInput.fill(username)<br>  await this.passwordInput.fill(password)<br>  await this.loginButton.click()<br> }<br>}</pre><p>2. สร้าง Custom Fixtures ที่มีการ new Instance ของ Page Objects ที่ต้องการใช้ซ้ำๆใน Test spec</p><pre>// fixture.ts<br>import { test as base } from &#39;@playwright/test&#39;<br>import { LoginPage } from &#39;./LoginPage&#39;<br>import { MakeAppointmentPage } from &#39;./MakeAppointmentPage&#39;<br><br>export const test = base.extend&lt;{<br> loginPage: LoginPage<br> makeAppointmentPage: MakeAppointmentPage<br>}&gt;({<br> loginPage: async ({ page }, use) =&gt; {<br>  const loginPage = new LoginPage(page)<br>  await use(loginPage)<br> },<br> makeAppointmentPage: async ({ page }, use) =&gt; {<br>  const makeAppointmentPage = new MakeAppointmentPage(page)<br>  await use(makeAppointmentPage)<br> },<br>})<br><br>export { expect } from &#39;@playwright/test&#39;</pre><p>อธิบายการทำงานในส่วน import</p><pre>import { test as base } from &#39;@playwright/test&#39;<br>import { LoginPage } from &#39;./LoginPage&#39;<br>import { MakeAppointmentPage } from &#39;./MakeAppointmentPage&#39;</pre><ul><li>นำเข้า test จาก Playwright และเปลี่ยนชื่อเป็น base</li><li>นำเข้า LoginPage และ MakeAppointmentPage class ที่เป็น Page Object Model ที่เราต้องการ Reuse</li></ul><p>อธิบายการทำงานในส่วนสร้าง Custom Fixtures</p><pre>export const test = base.extend&lt;{<br> loginPage: LoginPage<br> makeAppointmentPage: MakeAppointmentPage<br>}&gt;({<br> loginPage: async ({ page }, use) =&gt; {<br>  const loginPage = new LoginPage(page)<br>  await use(loginPage)<br> },<br> makeAppointmentPage: async ({ page }, use) =&gt; {<br>  const makeAppointmentPage = new MakeAppointmentPage(page)<br>  await use(makeAppointmentPage)<br> },<br>})</pre><ul><li>base.extend() ใช้เพื่อขยาย test fixture เดิมของ Playwright</li><li>กำหนด type loginPage และ makeAppointmentPage</li><li>รับ page จาก Playwright&#39;s built-in fixture</li><li>สร้าง instance ของ LoginPage และ MakeAppointmentPage</li><li>ใช้ await use() เพื่อส่งต่อ loginPage และ makeAppointmentPage ให้กับ test function</li></ul><p>อธิบายการทำงานในส่วน Export expect</p><p>Export expect จาก Playwright เพื่อที่เวลาใช้ใน Test Spec เราจะได้ Import จากไฟล์นี้ไฟล์เดียวครับ</p><pre>export { expect } from &#39;@playwright/test&#39;</pre><p>3. นำเอา custom fixture ที่ได้สร้างมาใช้ในไฟล์ Test Spec</p><pre>// login.spec.ts<br>import { test, expect } from &#39;../page-objects/fixture&#39;<br><br>test.describe(&#39;POM example - Login feature&#39;, () =&gt; {<br> test.beforeEach(async ({ loginPage }) =&gt; {<br>    // Navigate to the login page<br>    await loginPage.navigateToLoginPage()<br> })<br><br> test(&#39;POM example - Login successful with valid credentials&#39;, async ({<br>  loginPage,<br> }) =&gt; {<br>    // Perform login with valid credentials<br>    await loginPage.login(&#39;John Doe&#39;, &#39;ThisIsNotAPassword&#39;)<br><br>    // Verify that the user is redirected to the appointment page<br>    await expect(loginPage.page).toHaveURL(<br>     &#39;https://katalon-demo-cura.herokuapp.com/#appointment&#39;<br>    )<br> })<br><br> test(&#39;POM example - Login failed with invalid credentials&#39;, async ({<br>  loginPage,<br> }) =&gt; {<br>    // Perform login with wrong credentials<br>    // ......<br> })<br>})</pre><pre>// makeAppoinment.spec.ts<br>import { test, expect } from &#39;../page-objects/fixture&#39;<br><br>test.describe(&#39;POM example - Make appointment feature&#39;, () =&gt; {<br> test.beforeEach(async ({ loginPage }) =&gt; {<br>    // Navigate to the login page<br>    await loginPage.navigateToLoginPage()<br>    // Perform login with valid credentials<br>    await loginPage.login(&#39;John Doe&#39;, &#39;ThisIsNotAPassword&#39;)<br> })<br><br> test(&#39;POM example - user can make appointment&#39;, async ({<br>  makeAppointmentPage,<br> }) =&gt; {<br>    await makeAppointmentPage.fillVisitDate(&#39;16/6/2025&#39;)<br>    await makeAppointmentPage.selectFaciltity(&#39;Seoul CURA Healthcare Center&#39;)<br>    // Perform make appointment<br>    // ...<br> })<br>})</pre><h3>Benefit of Playwright Fixtures Approach</h3><p>จาก Code จะเห็นว่า เราสามารถแก้ปัญหาได้ดังนี้ครับ</p><ol><li>ช่วยลด Duplicate Code ทั้งในส่วนทั้ง Import และ ในส่วนสร้าง Instance ของ Page Object ที่ซ้ำกันได้ในทุก Test Spec ครับ</li><li>Maintain ได้ง่ายขึ้น เพราะเมื่อ Constructor ของ LoginPage มีการเปลี่ยนแปลงเราไม่จำเป็นต้องตามไปแก้ในทุกไฟล์</li><li>ใช้เวลาในการ Test Setup ที่เร็วมากขึ้น เพราะไม่จำเป็นต้องสร้าง Object ใหม่ทุกครั้ง</li></ol><h3>Conclusion</h3><p>การนำใช้ Playwright Fixtures มาใช้กับ Page Object Model จะทำให้ Code ของเราสะอาดมากขึ้น , ลดความซ้ำซ้อนของ Code ทำให้ maintain ได้ง่ายมากขึ้น แถมยังช่วยเพิ่มความเร็วของการรันเทส ลองไปใช้กันดูนะครับ ^^</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=6997fcbb5063" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>