#การทดสอบซอฟต์แวร์ด้วย Selenium + Python และ uv บน Windows และ macOS

บทความนี้จะแนะนำการเริ่มต้นทำ Web UI Automated Testing ด้วย Selenium + Python และใช้ uv สำหรับจัดการ Python, Virtual Environment และ Dependencies โดยเริ่มตั้งแต่ศูนย์ทั้งบน Windows และ macOS

เว็บไซต์ที่ใช้ทดลองคือ

https://seleniumbase.io/simple/login

ข้อมูลสำหรับ Login:

Username: demo_user
Password: secret_pass

แนวทางของบทความนี้จะเรียงลำดับดังนี้

ติดตั้ง uv
   ↓
ติดตั้ง Python ผ่าน uv
   ↓
สร้าง Project
   ↓
ติดตั้ง Selenium
   ↓
ทดลอง Selenium แบบ Python Script
   ↓
uv run python main.py
   ↓
เพิ่ม pytest
   ↓
สร้าง Automated Test
   ↓
uv run pytest
   ↓
Explicit Wait / Fixture / Headless / Screenshot

จุดสำคัญคือ เมื่อใช้ uv แล้ว ไม่จำเป็นต้องสร้าง Virtual Environment ด้วย python -m venv .venv เอง


#1. เครื่องมือที่ใช้

ใน Workshop นี้จะใช้

  • Python — ภาษาเขียน Test Script
  • Selenium WebDriver — ควบคุม Web Browser
  • pytest — Test Runner
  • uv — จัดการ Python Environment และ Dependencies
  • Google Chrome — Browser สำหรับทดลอง
  • SeleniumBase Simple Login — เว็บไซต์ตัวอย่าง

#2. uv คืออะไร

uv เป็นเครื่องมือสำหรับจัดการ Python Project ที่รวมความสามารถหลายอย่างไว้ในตัวเดียว เช่น

Python installation
Virtual Environment
Package installation
Dependency management
Lock file
Running Python commands
Running CLI tools

คำสั่งที่ใช้บ่อย เช่น

uv init
uv python install
uv python pin
uv add
uv remove
uv sync
uv run
uv tree

ตัวอย่างเช่น

uv run python main.py

หรือ

uv run pytest

โดย uv จะเลือก Python environment ของโปรเจกต์ให้โดยอัตโนมัติ


#3. ต้องใช้ python -m venv .venv หรือไม่

ถ้าใช้ uv เป็นตัวจัดการโปรเจกต์ ไม่จำเป็นต้องใช้

python -m venv .venv

workflow แบบเดิมอาจเป็น

python -m venv .venv
source .venv/bin/activate
pip install selenium
python main.py

แต่เมื่อใช้ uv สามารถทำได้ง่ายกว่า

uv add selenium
uv run python main.py

uv จะสร้างและจัดการ .venv ของโปรเจกต์ให้เมื่อจำเป็น

ดังนั้นในบทความนี้ จะไม่ใช้

python -m venv .venv

และไม่จำเป็นต้อง Activate Virtual Environment ก่อนรันโปรแกรม


#4. ติดตั้ง uv บน Windows

เปิด PowerShell หรือ Windows Terminal

ตรวจสอบก่อน

uv --version

ถ้ายังไม่มี ให้ติดตั้งด้วย Official Installer

powershell -ExecutionPolicy ByPass -c "irm https://astral.sh/uv/install.ps1 | iex"

ปิด Terminal แล้วเปิดใหม่ จากนั้นตรวจสอบ

uv --version

ตัวอย่าง

uv 0.x.x

หมายเลขเวอร์ชันอาจแตกต่างกันตามรุ่นที่ติดตั้ง

#ทางเลือก: ติดตั้งด้วย WinGet

winget install --id=astral-sh.uv -e

จากนั้นตรวจสอบ

uv --version

#5. ติดตั้ง uv บน macOS

เปิด Terminal และตรวจสอบ

uv --version

ถ้ายังไม่มี ให้ติดตั้งด้วย

curl -LsSf https://astral.sh/uv/install.sh | sh

จากนั้นเปิด Terminal ใหม่ หรือ reload shell

source ~/.zshrc

ตรวจสอบ

uv --version

#ทางเลือก: Homebrew

ถ้ามี Homebrew อยู่แล้ว

brew install uv

ตรวจสอบ

uv --version

#6. ติดตั้ง Python ด้วย uv

เราสามารถให้ uv จัดการ Python ได้โดยตรง

ตรวจสอบ Python ที่มีอยู่

uv python list

ในบทความนี้ใช้ Python 3.12

uv python install 3.12

ตรวจสอบ

uv run python --version

ตัวอย่าง

Python 3.12.x

#7. สร้าง Selenium Project

คำสั่งเหมือนกันทั้ง Windows และ macOS

uv init --no-package selenium-python-testing

เข้า directory

cd selenium-python-testing

กำหนด Python ของโปรเจกต์

uv python pin 3.12

ตรวจสอบ

uv run python --version

โครงสร้างเริ่มต้นประมาณ

selenium-python-testing/
├── .python-version
├── README.md
├── main.py
└── pyproject.toml

--no-package เหมาะกับ Workshop นี้ เพราะเราต้องการโปรเจกต์สำหรับ Test Script โดยตรง


#8. ติดตั้ง Selenium

เพิ่ม Selenium

uv add selenium

ตรวจสอบ dependency

uv tree

หลังจากรัน uv add จะมี .venv และ uv.lock ถูกจัดการให้โดย uv

โครงสร้างจะประมาณ

selenium-python-testing/
├── .venv/
├── .python-version
├── README.md
├── main.py
├── pyproject.toml
└── uv.lock

เราไม่ต้องสร้าง .venv เองด้วย

python -m venv .venv

#9. ทดลอง Selenium แบบ Python Script ก่อน

ก่อนใช้ pytest เราจะทดลอง Selenium เป็น Python Program ธรรมดาก่อน

แก้ไขไฟล์

main.py

เป็น

from selenium import webdriver


driver = webdriver.Chrome()

try:
    driver.get("https://seleniumbase.io/simple/login")

    print("Title:", driver.title)
    print("URL:", driver.current_url)

finally:
    driver.quit()

#10. รัน Selenium ด้วย uv

ใช้คำสั่ง

uv run python main.py

คำสั่งนี้ใช้ได้ทั้ง Windows และ macOS

Selenium จะ

1. เปิด Google Chrome
2. เข้าเว็บไซต์ SeleniumBase
3. อ่าน Page Title
4. อ่าน URL
5. ปิด Browser

ตัวอย่างผลลัพธ์ใน Terminal

Title: Login Page
URL: https://seleniumbase.io/simple/login

#11. ใช้ uv run python3 main.py ได้หรือไม่

บน macOS หรือ Linux บางเครื่อง อาจใช้

uv run python3 main.py

ได้

แต่สำหรับโปรเจกต์ที่ใช้ uv แนะนำให้ใช้

uv run python main.py

เพราะเป็นคำสั่งที่สม่ำเสมอกว่า และใช้ได้เหมือนกันทั้ง Windows และ macOS

ดังนั้นบทความนี้จะใช้

uv run python

เป็นหลัก


#12. Selenium Manager และ ChromeDriver

Selenium รุ่นใหม่มี Selenium Manager

เมื่อเขียน

driver = webdriver.Chrome()

Selenium จะจัดการ WebDriver ที่เหมาะสมให้อัตโนมัติในกรณีทั่วไป

ดังนั้นโดยปกติไม่ต้อง

Download chromedriver manually
Copy chromedriver ไปไว้ใน Project
เพิ่ม chromedriver เข้า PATH

สิ่งที่ควรมีคือ Google Chrome ที่ติดตั้งและใช้งานได้ตามปกติ


#13. ทดลองเปิดเว็บไซต์และตรวจสอบ Title

แก้ไข main.py

from selenium import webdriver


driver = webdriver.Chrome()

try:
    driver.get("https://seleniumbase.io/simple/login")

    assert "Login" in driver.title

    print("Test Passed")

finally:
    driver.quit()

รัน

uv run python main.py

ถ้าผ่านจะเห็น

Test Passed

ตอนนี้เราเริ่มมี Assertion แล้ว แต่ยังเป็น Python Script ปกติอยู่


#14. หา Element ด้วย Selenium

Selenium รองรับ Locator หลายแบบ

By.ID
By.NAME
By.CLASS_NAME
By.TAG_NAME
By.LINK_TEXT
By.PARTIAL_LINK_TEXT
By.CSS_SELECTOR
By.XPATH

ก่อนใช้ By ต้อง import

from selenium.webdriver.common.by import By

ตัวอย่าง Username

driver.find_element(By.ID, "username")

Password

driver.find_element(By.ID, "password")

Sign in

driver.find_element(By.LINK_TEXT, "Sign in")

#15. ทดลอง Login ด้วย Python Script

แก้ไข main.py

from selenium import webdriver
from selenium.webdriver.common.by import By


driver = webdriver.Chrome()

try:
    driver.get("https://seleniumbase.io/simple/login")

    username = driver.find_element(
        By.ID,
        "username",
    )

    password = driver.find_element(
        By.ID,
        "password",
    )

    username.send_keys("demo_user")
    password.send_keys("secret_pass")

    driver.find_element(
        By.LINK_TEXT,
        "Sign in",
    ).click()

    assert "Welcome!" in driver.page_source

    print("Login Test Passed")

finally:
    driver.quit()

รัน

uv run python main.py

Flow:

เปิด Browser
    ↓
เปิดหน้า Login
    ↓
กรอก demo_user
    ↓
กรอก secret_pass
    ↓
Click Sign in
    ↓
ตรวจสอบ Welcome!
    ↓
ปิด Browser

#16. ทำไมต้องใช้ pytest

Python Script แบบก่อนหน้าใช้งานได้ แต่เมื่อมี Test Case จำนวนมากจะเริ่มจัดการยาก เช่น

test_login_success
test_login_invalid_password
test_logout
test_profile
test_register

pytest ช่วยเรื่อง

Test Discovery
Test Runner
Fixtures
Parameterized Testing
Assertions
Reports
Markers
Test Selection

ดังนั้นขั้นถัดไปคือเพิ่ม pytest เข้าโปรเจกต์


#17. ติดตั้ง pytest ด้วย uv

เพิ่มเป็น Development Dependency

uv add --dev pytest

ตรวจสอบ

uv tree

เราสามารถตรวจสอบ pytest

uv run pytest --version

#18. สร้างโฟลเดอร์ tests

สร้าง

tests/

#Windows PowerShell

mkdir tests

#macOS

mkdir tests

จากนั้นสร้าง

tests/test_login.py

#19. เปลี่ยนจาก Python Script เป็น pytest

สร้าง tests/test_login.py

from selenium import webdriver
from selenium.webdriver.common.by import By


def test_login_success():
    driver = webdriver.Chrome()

    try:
        driver.get(
            "https://seleniumbase.io/simple/login"
        )

        driver.find_element(
            By.ID,
            "username",
        ).send_keys("demo_user")

        driver.find_element(
            By.ID,
            "password",
        ).send_keys("secret_pass")

        driver.find_element(
            By.LINK_TEXT,
            "Sign in",
        ).click()

        assert "Welcome!" in driver.page_source

    finally:
        driver.quit()

#20. รัน pytest

รัน

uv run pytest

หรือแสดงรายละเอียด

uv run pytest -v

ตัวอย่าง

tests/test_login.py::test_login_success PASSED

#21. ทำไมไม่ใช้ uv run python tests/test_login.py

ไฟล์ pytest มีฟังก์ชัน

def test_login_success():
    ...

ถ้ารัน

uv run python tests/test_login.py

Python จะอ่านไฟล์และประกาศฟังก์ชัน แต่ไม่ได้มีคำสั่งเรียก

test_login_success()

ดังนั้น Test อาจไม่ถูก Execute

เมื่อเขียน Test ด้วย pytest ควรใช้

uv run pytest

หรือ

uv run python -m pytest

สองแบบนี้ถูกต้องสำหรับ pytest


#22. สรุปความแตกต่างของคำสั่งรัน

#Python Script

ไฟล์

main.py

รัน

uv run python main.py

ใช้เมื่อเรียน Selenium พื้นฐาน หรือเขียน Python Program ทั่วไป

#pytest Test

ไฟล์

tests/test_login.py

รัน

uv run pytest

หรือ

uv run python -m pytest

ใช้เมื่อสร้าง Automated Test Suite


#23. ใช้ Explicit Wait

Web Application อาจมี

Network Delay
JavaScript Rendering
Animation
API Loading
Dynamic Elements

ดังนั้นไม่ควรพึ่ง

time.sleep(5)

เป็นหลัก

ควรใช้

WebDriverWait

ตัวอย่าง

from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.support.ui import WebDriverWait


def test_login_success():
    driver = webdriver.Chrome()
    wait = WebDriverWait(driver, 10)

    try:
        driver.get(
            "https://seleniumbase.io/simple/login"
        )

        username = wait.until(
            EC.visibility_of_element_located(
                (By.ID, "username")
            )
        )

        username.send_keys("demo_user")

        driver.find_element(
            By.ID,
            "password",
        ).send_keys("secret_pass")

        wait.until(
            EC.element_to_be_clickable(
                (By.LINK_TEXT, "Sign in")
            )
        ).click()

        heading = wait.until(
            EC.visibility_of_element_located(
                (By.TAG_NAME, "h1")
            )
        )

        assert heading.text == "Welcome!"

    finally:
        driver.quit()

รัน

uv run pytest -v

#24. ใช้ pytest Fixture

เมื่อมีหลาย Test Case เราไม่ควรเขียน

driver = webdriver.Chrome()

และ

driver.quit()

ซ้ำทุกไฟล์

สร้าง

tests/conftest.py

ใส่

import pytest
from selenium import webdriver


@pytest.fixture
def driver():
    driver = webdriver.Chrome()
    driver.set_window_size(1440, 1000)

    yield driver

    driver.quit()

จากนั้น tests/test_login.py

from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.support.ui import WebDriverWait


def test_login_success(driver):
    wait = WebDriverWait(driver, 10)

    driver.get(
        "https://seleniumbase.io/simple/login"
    )

    wait.until(
        EC.visibility_of_element_located(
            (By.ID, "username")
        )
    ).send_keys("demo_user")

    driver.find_element(
        By.ID,
        "password",
    ).send_keys("secret_pass")

    wait.until(
        EC.element_to_be_clickable(
            (By.LINK_TEXT, "Sign in")
        )
    ).click()

    heading = wait.until(
        EC.visibility_of_element_located(
            (By.TAG_NAME, "h1")
        )
    )

    assert heading.text == "Welcome!"

#25. ทดสอบ Login และ Logout

เพิ่ม Scenario

Given ผู้ใช้อยู่หน้า Login
When Login ด้วยข้อมูลที่ถูกต้อง
Then แสดง Welcome!
When Click Sign out
Then แสดงข้อความ signed out

ตัวอย่าง

from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.support.ui import WebDriverWait


def test_login_and_logout(driver):
    wait = WebDriverWait(driver, 10)

    driver.get(
        "https://seleniumbase.io/simple/login"
    )

    wait.until(
        EC.visibility_of_element_located(
            (By.ID, "username")
        )
    ).send_keys("demo_user")

    driver.find_element(
        By.ID,
        "password",
    ).send_keys("secret_pass")

    wait.until(
        EC.element_to_be_clickable(
            (By.LINK_TEXT, "Sign in")
        )
    ).click()

    heading = wait.until(
        EC.visibility_of_element_located(
            (By.TAG_NAME, "h1")
        )
    )

    assert heading.text == "Welcome!"

    wait.until(
        EC.element_to_be_clickable(
            (By.LINK_TEXT, "Sign out")
        )
    ).click()

    message = wait.until(
        EC.visibility_of_element_located(
            (By.ID, "top_message")
        )
    )

    assert "signed out" in message.text.lower()

#26. Data-Driven Testing

pytest สามารถใช้

@pytest.mark.parametrize

เพื่อทดสอบข้อมูลหลายชุด

import pytest
from selenium.webdriver.common.by import By


@pytest.mark.parametrize(
    "username,password",
    [
        ("invalid_user", "invalid_pass"),
        ("demo_user", "wrong_password"),
        ("wrong_user", "secret_pass"),
    ],
)
def test_invalid_login(
    driver,
    username,
    password,
):
    driver.get(
        "https://seleniumbase.io/simple/login"
    )

    driver.find_element(
        By.ID,
        "username",
    ).send_keys(username)

    driver.find_element(
        By.ID,
        "password",
    ).send_keys(password)

    driver.find_element(
        By.LINK_TEXT,
        "Sign in",
    ).click()

    assert "Welcome!" not in driver.page_source

รัน

uv run pytest -v

#27. Headless Mode

Headless Mode คือการรัน Browser โดยไม่เปิดหน้าต่าง GUI

เหมาะสำหรับ

CI/CD
GitHub Actions
GitLab CI
Jenkins
Docker
Cloud VM

แก้ไข tests/conftest.py

import pytest
from selenium import webdriver
from selenium.webdriver.chrome.options import Options


def pytest_addoption(parser):
    parser.addoption(
        "--headless",
        action="store_true",
        default=False,
        help="Run Chrome in headless mode",
    )


@pytest.fixture
def driver(request):
    options = Options()

    if request.config.getoption("--headless"):
        options.add_argument("--headless=new")

    options.add_argument(
        "--window-size=1440,1000"
    )

    driver = webdriver.Chrome(
        options=options
    )

    yield driver

    driver.quit()

รันแบบเห็น Browser

uv run pytest -v

รันแบบ Headless

uv run pytest -v --headless

คำสั่งเดียวกันใช้ได้ทั้ง Windows และ macOS


#28. Capture Screenshot เมื่อ Test Fail

แก้ tests/conftest.py

from pathlib import Path

import pytest
from selenium import webdriver
from selenium.webdriver.chrome.options import Options


def pytest_addoption(parser):
    parser.addoption(
        "--headless",
        action="store_true",
        default=False,
        help="Run Chrome in headless mode",
    )


@pytest.hookimpl(hookwrapper=True)
def pytest_runtest_makereport(item, call):
    outcome = yield
    report = outcome.get_result()

    setattr(
        item,
        f"rep_{report.when}",
        report,
    )


@pytest.fixture
def driver(request):
    options = Options()

    if request.config.getoption("--headless"):
        options.add_argument("--headless=new")

    options.add_argument(
        "--window-size=1440,1000"
    )

    driver = webdriver.Chrome(
        options=options
    )

    yield driver

    if hasattr(request.node, "rep_call"):
        if request.node.rep_call.failed:
            screenshot_dir = Path(
                "screenshots"
            )

            screenshot_dir.mkdir(
                exist_ok=True
            )

            screenshot_file = (
                screenshot_dir
                / f"{request.node.name}.png"
            )

            driver.save_screenshot(
                str(screenshot_file)
            )

    driver.quit()

เมื่อ Test Fail จะได้

screenshots/
└── test_login_success.png

#29. โครงสร้าง Project ที่แนะนำ

เมื่อทำ Workshop ครบ

selenium-python-testing/
│
├── .venv/
├── .python-version
│
├── screenshots/
│
├── tests/
│   ├── conftest.py
│   └── test_login.py
│
├── .gitignore
├── main.py
├── pyproject.toml
├── README.md
└── uv.lock

บทบาทของแต่ละส่วน

main.py
    Selenium Python Script สำหรับทดลองพื้นฐาน

tests/
    Automated Tests ที่รันด้วย pytest

conftest.py
    pytest Fixtures และ Configuration

pyproject.toml
    Project Configuration / Dependencies

uv.lock
    Lock Dependencies

.venv/
    Python Virtual Environment ที่ uv จัดการให้

#30. .gitignore

สร้าง

.gitignore

ใส่

.venv/
__pycache__/
.pytest_cache/
*.pyc

screenshots/

.DS_Store
Thumbs.db

#31. คำสั่ง uv ที่ควรรู้

ตรวจสอบ uv

uv --version

ติดตั้ง Python

uv python install 3.12

สร้าง Project

uv init --no-package selenium-python-testing

กำหนด Python Version

uv python pin 3.12

เพิ่ม Selenium

uv add selenium

เพิ่ม pytest

uv add --dev pytest

ดู Dependency

uv tree

Synchronize Environment

uv sync

รัน Python Script

uv run python main.py

รัน pytest

uv run pytest

#32. คำสั่ง pytest ที่ควรรู้

รันทุก Test

uv run pytest

แสดงรายละเอียด

uv run pytest -v

แสดง output ของ print()

uv run pytest -v -s

รันไฟล์เดียว

uv run pytest tests/test_login.py -v

รัน Test Case เดียว

uv run pytest \
  tests/test_login.py::test_login_success \
  -v

รัน Test ที่มีคำว่า login

uv run pytest -k login -v

Headless

uv run pytest -v --headless

#33. ต้อง Activate .venv หรือไม่

ไม่จำเป็น

สามารถใช้

uv run python main.py

และ

uv run pytest

ได้โดยตรง

แต่ถ้าต้องการ Activate เองก็ทำได้

#Windows PowerShell

.venv\Scripts\Activate.ps1

#macOS

source .venv/bin/activate

จากนั้นอาจรัน

python main.py

หรือ

pytest

ได้

แต่สำหรับ Workshop นี้แนะนำให้ใช้ uv run เพราะลดความแตกต่างระหว่าง Windows และ macOS


#34. Workflow ที่แนะนำสำหรับผู้เริ่มต้น

#ขั้นที่ 1: ติดตั้ง uv

uv --version

#ขั้นที่ 2: สร้าง Project

uv init --no-package selenium-python-testing
cd selenium-python-testing

#ขั้นที่ 3: กำหนด Python

uv python pin 3.12

#ขั้นที่ 4: เพิ่ม Selenium

uv add selenium

#ขั้นที่ 5: เขียน Selenium Script

main.py

#ขั้นที่ 6: รัน

uv run python main.py

#ขั้นที่ 7: เพิ่ม pytest

uv add --dev pytest

#ขั้นที่ 8: สร้าง Automated Test

tests/test_login.py

#ขั้นที่ 9: รัน Test Suite

uv run pytest -v

#35. เปรียบเทียบ Python Script กับ pytest

หัวข้อ Python Script pytest
ตัวอย่างไฟล์ main.py tests/test_login.py
คำสั่ง uv run python main.py uv run pytest
เหมาะกับ เรียน Selenium พื้นฐาน Automated Test Suite
Test Discovery ไม่มี มี
Fixture ไม่มี มี
Parametrize ต้องเขียนเอง มี
Report จำกัด รองรับ Plugin
CI/CD ทำได้ เหมาะกว่า

ดังนั้นในช่วงแรกควรเรียน

Selenium + Python

ก่อน แล้วจึงเพิ่ม

pytest

#36. ปัญหาที่พบบ่อย

#uv command not found

Windows:

uv --version

หากไม่พบ ให้เปิด Terminal ใหม่ หรือติดตั้งด้วย

winget install --id=astral-sh.uv -e

macOS:

source ~/.zshrc
uv --version

#Chrome เปิดไม่ได้

ทดลอง

from selenium import webdriver

driver = webdriver.Chrome()
driver.get("https://example.com")
driver.quit()

รัน

uv run python main.py

#Selenium Manager ดาวน์โหลด Driver ไม่ได้

อาจเกิดจาก

Firewall
Proxy
SSL Inspection
Internet Policy
Restricted Network

โดยเฉพาะเครื่องในองค์กรหรือห้อง Lab


#Test Fail เพราะ Element ยังไม่พร้อม

หลีกเลี่ยง

time.sleep(5)

เป็นวิธีหลัก

ใช้

WebDriverWait

แทน


#37. Roadmap สำหรับพัฒนาต่อ

หลังจากพื้นฐานนี้สามารถเรียนต่อได้ตามลำดับ

Selenium Python
      ↓
Locator
      ↓
Assertion
      ↓
Explicit Wait
      ↓
pytest
      ↓
Fixture
      ↓
Parameterized Testing
      ↓
Page Object Model
      ↓
Screenshot / Evidence
      ↓
Headless
      ↓
Test Report
      ↓
Parallel Testing
      ↓
Cross-Browser
      ↓
Selenium Grid
      ↓
Docker
      ↓
CI/CD

หัวข้อที่ควรศึกษาต่อ

  • Page Object Model
  • pytest markers
  • pytest-html
  • Allure Report
  • pytest-xdist
  • Selenium Grid
  • Docker
  • GitHub Actions
  • GitLab CI
  • Jenkins
  • Cross-Browser Testing
  • Visual Testing

#สรุป

เมื่อใช้ uv เราไม่จำเป็นต้องสร้าง Virtual Environment ด้วย

python -m venv .venv

เอง

workflow เริ่มต้นสามารถทำได้เพียง

uv init --no-package selenium-python-testing

cd selenium-python-testing

uv python pin 3.12

uv add selenium

จากนั้นทดลอง Selenium แบบ Python Script

uv run python main.py

เมื่อเข้าใจ Selenium พื้นฐานแล้วจึงเพิ่ม pytest

uv add --dev pytest

และรัน Automated Test ด้วย

uv run pytest -v

ดังนั้นควรแยกความเข้าใจเป็นสองระดับ

Selenium Python Script
        ↓
uv run python main.py

Automated Testing with pytest
        ↓
uv run pytest

แนวทางนี้ช่วยให้ผู้เริ่มต้นเข้าใจชัดเจนว่า

  • uv ทำหน้าที่จัดการ Python Environment และ Dependencies
  • Selenium ทำหน้าที่ควบคุม Browser
  • Python เป็นภาษาที่ใช้เขียน Test Script
  • pytest เป็น Test Runner สำหรับจัดการ Automated Test Suite

และ Test Code ชุดเดียวกันสามารถพัฒนาต่อไปใช้บน Windows, macOS, CI/CD และ Cloud Environment ได้


#References