Spaces:
Running
Running
from playwright.sync_api import sync_playwright | |
import time | |
import os | |
URL = "https://yozora721-pnp-chatbot-v1.hf.space" | |
PERTANYAAN = "Halo" | |
SCREENSHOTS_DIR = "screenshots" | |
def kirim_pertanyaan(page, pertanyaan: str): | |
"""Ketik pertanyaan dan kirim, tunggu hingga pesan keluar.""" | |
page.locator('textarea[placeholder="Masukkan pertanyaan"]').fill(pertanyaan) | |
page.keyboard.press("Enter") | |
page.wait_for_selector('.msg', timeout=60000) | |
def jumlah_tag_audio(page) -> int: | |
"""Hitung jumlah tag <audio>.""" | |
return len(page.query_selector_all("audio")) | |
def buat_screenshot(page, status: str): | |
"""Buat screenshot area chat .msg dan simpan dengan status tertentu.""" | |
os.makedirs(SCREENSHOTS_DIR, exist_ok=True) | |
chat_area = page.locator('.msg') | |
chat_area.screenshot(path=f"{SCREENSHOTS_DIR}/chat_{status}.png") | |
print(f"π· Screenshot area chat disimpan: {SCREENSHOTS_DIR}/chat_{status}.png") | |
def test_tts(page, aktif: bool): | |
"""Test untuk status TTS aktif/nonaktif.""" | |
status = "AKTIF" if aktif else "NONAKTIF" | |
# Klik Toggle sesuai kebutuhan | |
if aktif and page.locator('text=π Text-to-Speech Nonaktif').count() > 0: | |
page.locator('text=π Text-to-Speech Nonaktif').click() | |
elif not aktif and page.locator('text=π Text-to-Speech Aktif').count() > 0: | |
page.locator('text=π Text-to-Speech Aktif').click() | |
# Kirim pertanyaan | |
kirim_pertanyaan(page, PERTANYAAN) | |
# Hitung jumlah audio | |
jumlah_audio = jumlah_tag_audio(page) | |
if aktif: | |
if jumlah_audio > 0: | |
print(f"β [AKTIF] Ada {jumlah_audio} tag <audio> (sesuai ekspektasi).") | |
else: | |
print(f"β [AKTIF] Tidak ditemukan tag <audio>.") | |
else: | |
if jumlah_audio == 0: | |
print(f"β [NONAKTIF] Tidak ditemukan tag <audio> (sesuai ekspektasi).") | |
else: | |
print(f"β [NONAKTIF] Ada {jumlah_audio} tag <audio>, tidak sesuai ekspektasi.") | |
# Simpan screenshot area chat | |
buat_screenshot(page, status) | |
# Beri waktu sebelum test selanjutnya | |
time.sleep(2) | |
def main(): | |
"""Main entry point.""" | |
with sync_playwright() as p: | |
browser = p.chromium.launch(headless=False) # True kalau tidak perlu lihat | |
page = browser.new_page() | |
page.goto(URL) | |
# Test dengan TTS Aktif | |
test_tts(page, True) | |
# Test dengan TTS Nonaktif | |
test_tts(page, False) | |
browser.close() | |
print("\nπ Selesai! Cek folder 'screenshots' untuk melihat area chat.") | |
if __name__ == '__main__': | |
main() | |