CupCode.cc
Section 04 · Module 03 · Lesson 01 · ~15 นาที

Navigation และ Form Interactions

เวลาเรียน: 15 นาที ต้องรู้มาก่อน: Module 02: Locators และ Assertions รูปแบบ: อ่าน + แบบฝึกสั้น

จบบทนี้แล้วคุณจะ...

  • ใช้ page.goto(), click(), fill(), selectOption(), check() ได้
  • เขียน test สำหรับ form submission ครบ flow
  • จัดการ navigation และ redirects
  • เข้าใจเรื่อง waiting for navigation
1 · Navigation — เปิดหน้าเว็บ
// เปิด URL เต็ม
await page.goto('https://example.com');

// ใช้ baseURL จาก config (แนะนำ)
// ถ้า baseURL = 'http://localhost:3000'
await page.goto('/login');        // → http://localhost:3000/login
await page.goto('/products');     // → http://localhost:3000/products

// รอจน page โหลดเสร็จจริงๆ
await page.goto('/login', { waitUntil: 'networkidle' });

Navigation ผ่านการคลิก

// คลิกลิงก์แล้ว Playwright จะรอ navigation อัตโนมัติ
await page.getByRole('link', { name: 'Products' }).click();

// ตรวจว่าไปถูกหน้า
await expect(page).toHaveURL(/.*products/);
2 · Click — คลิก Element
// คลิกปุ่ม
await page.getByRole('button', { name: 'Submit' }).click();

// Double click
await page.getByText('Edit').dblclick();

// Right click
await page.getByText('Item').click({ button: 'right' });

// Click ที่ตำแหน่งเฉพาะ
await page.getByTestId('canvas').click({ position: { x: 100, y: 200 } });
3 · Fill — กรอกข้อมูล
// กรอก input field (ลบค่าเดิมก่อนกรอก)
await page.getByLabel('Email').fill('[email protected]');
await page.getByLabel('Password').fill('mypassword123');

// พิมพ์ทีละตัวอักษร (สำหรับ autocomplete, debounce)
await page.getByLabel('Search').pressSequentially('laptop', { delay: 100 });

// ลบค่าใน field
await page.getByLabel('Email').clear();
4 · Select, Check, Radio
// Dropdown — เลือก option
await page.getByLabel('Country').selectOption('Thailand');

// เลือกด้วย value attribute
await page.getByLabel('Country').selectOption({ value: 'th' });

// Checkbox — check
await page.getByRole('checkbox', { name: 'Accept terms' }).check();

// Checkbox — uncheck
await page.getByRole('checkbox', { name: 'Newsletter' }).uncheck();

// Radio button
await page.getByRole('radio', { name: 'Credit Card' }).check();
5 · Form Submission — ตัวอย่างเต็ม
import { test, expect } from '@playwright/test';

test('should login successfully', async ({ page }) => {
  // 1. ไปที่หน้า login
  await page.goto('/login');

  // 2. กรอก email
  await page.getByLabel('Email').fill('[email protected]');

  // 3. กรอก password
  await page.getByLabel('Password').fill('password123');

  // 4. check "Remember me"
  await page.getByRole('checkbox', { name: 'Remember me' }).check();

  // 5. คลิก submit
  await page.getByRole('button', { name: 'Sign In' }).click();

  // 6. ตรวจว่า redirect ไปหน้า dashboard
  await expect(page).toHaveURL(/.*dashboard/);

  // 7. ตรวจว่าเห็นข้อความต้อนรับ
  await expect(page.getByText('Welcome back')).toBeVisible();
});
6 · Handling Redirects
test('should redirect to login when unauthorized', async ({ page }) => {
  // พยายามเข้าหน้าที่ต้อง login ก่อน
  await page.goto('/admin/settings');

  // ตรวจว่า redirect ไปหน้า login
  await expect(page).toHaveURL(/.*login/);
});
7 · Waiting for Navigation

Playwright auto-wait navigation เมื่อคลิกลิงก์หรือ submit form แต่บางกรณีต้อง wait เอง:

// รอ URL เปลี่ยน
await page.waitForURL('**/dashboard');

// รอ response จาก API
await page.waitForResponse(resp =>
  resp.url().includes('/api/login') && resp.status() === 200
);
แบบฝึกหัด

เขียน test สำหรับ registration form ที่มี fields ต่อไปนี้:

  • Full Name (text input)
  • Email (email input)
  • Password (password input)
  • Country (dropdown: Thailand, Japan, USA)
  • Accept Terms (checkbox)
  • Submit button

หลัง submit สำเร็จ ต้อง redirect ไป /welcome และเห็นข้อความ "Registration successful"

ดูคำตอบแนะนำ
import { test, expect } from '@playwright/test';

test('should register new account', async ({ page }) => {
  await page.goto('/register');

  // กรอก Full Name
  await page.getByLabel('Full Name').fill('สมชาย ใจดี');

  // กรอก Email
  await page.getByLabel('Email').fill('[email protected]');

  // กรอก Password
  await page.getByLabel('Password').fill('SecurePass123!');

  // เลือก Country
  await page.getByLabel('Country').selectOption('Thailand');

  // Accept Terms
  await page.getByRole('checkbox', { name: 'Accept Terms' }).check();

  // Submit
  await page.getByRole('button', { name: 'Submit' }).click();

  // ตรวจ redirect
  await expect(page).toHaveURL(/.*welcome/);

  // ตรวจข้อความ
  await expect(page.getByText('Registration successful')).toBeVisible();
});

สรุปสิ่งที่ต้องจำ

  • page.goto() เปิดหน้าเว็บ — ใช้กับ baseURL จาก config
  • fill() กรอกค่า (ลบค่าเดิมก่อน), clear() ลบค่า
  • selectOption() เลือก dropdown, check()/uncheck() สำหรับ checkbox
  • click() คลิก element — Playwright รอ navigation อัตโนมัติ
  • เขียน test ตาม flow: ไปหน้า → กรอก → submit → ตรวจผลลัพธ์
  • ใช้ waitForURL() หรือ waitForResponse() เมื่อ auto-wait ไม่เพียงพอ