← Back to list

วิธีเช็คข้อมูลในตารางด้วย Playwright

การตรวจสอบข้อมูลในตาราง (Table) เป็นหนึ่งในขั้นตอนสำคัญของการเขียน Test โดยเฉพาะกรณีที่มีข้อมูลหลายแถว การเขียนให้ “เจาะจงและไม่สับสน”…

kumoriiz · 2026-05-04 07:08 · 0 claps · 0.7 min read
#playwrights
Open on Medium ↗

วิธีเช็คข้อมูลในตารางด้วย Playwright

การตรวจสอบข้อมูลในตาราง (Table) เป็นหนึ่งในขั้นตอนสำคัญของการเขียน Test โดยเฉพาะกรณีที่มีข้อมูลหลายแถว การเขียนให้ “เจาะจงและไม่สับสน” จะช่วยลดปัญหา Test พังในอนาคตได้

1. เช็คแบบเจาะจงแถว (Row-based Assertion) — แนะนำ

วิธีนี้ช่วยให้มั่นใจว่า “ข้อมูลทั้งหมดอยู่ในแถวเดียวกัน”

const row = page.locator('#user-table tbody tr', {
  hasText: 'somchai@test.com'
});

await expect(row).toContainText('สมชาย ทดสอบ');
await expect(row).toContainText('Admin');

2. เช็คค่าจากตำแหน่งคอลัมน์ (Column-based)

เหมาะกับกรณีที่ต้องการตรวจสอบค่าที่แน่นอนในแต่ละช่อง

const row = page.locator('#user-table tbody tr', {
  hasText: 'somchai@test.com'
});

await expect(row.locator('td').nth(1)).toHaveText('สมชาย ทดสอบ');

3. เช็คว่าข้อมูลมีอยู่ในตาราง

await expect(page.locator('#user-table'))
  .toContainText('somchai@test.com');

วิธีนี้ง่าย แต่ไม่การันตีว่าข้อมูลอยู่ในแถวเดียวกัน

4. เช็คจำนวนแถว

ใช้ตรวจสอบว่ามีการเพิ่มข้อมูลจริง

const rowCount = await page.locator('#user-table tbody tr').count();
expect(rowCount).toBeGreaterThan(0);

5. รอให้ข้อมูลแสดงผล (ลดปัญหา Flaky Test)

await expect(
  page.locator('#user-table tbody tr', {
    hasText: 'somchai@test.com'
  })
).toBeVisible();

⚠️ ข้อควรระวัง

  • หลีกเลี่ยงการใช้ selector กว้างเกินไป เช่น tr อย่างเดียว
  • หลีกเลี่ยงข้อมูลซ้ำ เช่น “สมชาย” อย่างเดียว
  • ควรใช้ข้อมูลแบบ Unique เช่น:
const email = `somchai_${Date.now()}@test.com`;

🔥 Best Practice

const email = `test_${Date.now()}@mail.com`;

const row = page.locator('#user-table tbody tr', {
  hasText: email
});
await expect(row).toBeVisible();
await expect(row).toContainText('ชื่อที่กรอก');
await expect(row).toContainText('Admin');

การเขียน Test ให้เจาะจงตั้งแต่ต้น จะช่วยให้ระบบมีความเสถียร และลดปัญหา Test พังในระยะยาวได้อย่างมาก


메타데이터
post_id
ff91efffa22e
slug
วิธีเช็คข้อมูลในตารางด้วย-playwright-ff91efffa22e
url
https://medium.com/@kumoriiz/%E0%B8%A7%E0%B8%B4%E0%B8%98%E0%B8%B5%E0%B9%80%E0%B8%8A%E0%B9%87%E0%B8%84%E0%B8%82%E0%B9%89%E0%B8%AD%E0%B8%A1%E0%B8%B9%E0%B8%A5%E0%B9%83%E0%B8%99%E0%B8%95%E0%B8%B2%E0%B8%A3%E0%B8%B2%E0%B8%87%E0%B8%94%E0%B9%89%E0%B8%A7%E0%B8%A2-playwright-ff91efffa22e
canonical_url
https://medium.com/@kumoriiz/%E0%B8%A7%E0%B8%B4%E0%B8%98%E0%B8%B5%E0%B9%80%E0%B8%8A%E0%B9%87%E0%B8%84%E0%B8%82%E0%B9%89%E0%B8%AD%E0%B8%A1%E0%B8%B9%E0%B8%A5%E0%B9%83%E0%B8%99%E0%B8%95%E0%B8%B2%E0%B8%A3%E0%B8%B2%E0%B8%87%E0%B8%94%E0%B9%89%E0%B8%A7%E0%B8%A2-playwright-ff91efffa22e
author_url
https://medium.com/@kumoriiz
status
ok
fetched_at
2026-06-22 12:55:45