chatbot-template/tests/pages/chat.ts

114 lines
2.8 KiB
TypeScript
Raw Normal View History

import { expect, type Page } from "@playwright/test";
const CHAT_ID_REGEX =
/^http:\/\/localhost:3000\/chat\/[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/;
2025-03-09 21:02:19 -07:00
export class ChatPage {
private readonly page: Page;
constructor(page: Page) {
this.page = page;
}
2025-03-09 21:02:19 -07:00
get sendButton() {
return this.page.getByTestId("send-button");
2025-03-09 21:02:19 -07:00
}
get stopButton() {
return this.page.getByTestId("stop-button");
2025-03-09 21:02:19 -07:00
}
get multimodalInput() {
return this.page.getByTestId("multimodal-input");
2025-03-09 21:02:19 -07:00
}
get messagesContainer() {
return this.page.locator("[data-testid='messages-container']");
}
async goto() {
await this.page.goto("/");
await this.page.waitForLoadState("networkidle");
}
2025-03-09 21:02:19 -07:00
async createNewChat() {
await this.goto();
2025-03-09 21:02:19 -07:00
}
getCurrentURL(): string {
2025-03-09 21:02:19 -07:00
return this.page.url();
}
async sendUserMessage(message: string) {
2025-03-11 14:39:36 -07:00
await this.multimodalInput.click();
await this.multimodalInput.fill(message);
await this.sendButton.click();
2025-03-09 21:02:19 -07:00
}
async waitForResponse(timeout = 30_000) {
const response = await this.page.waitForResponse(
(res) => res.url().includes("/api/chat") && res.status() === 200,
{ timeout }
2025-03-11 14:39:36 -07:00
);
await response.finished();
}
async isGenerationComplete(timeout = 30_000) {
await this.waitForResponse(timeout);
await this.page.waitForTimeout(500);
2025-03-09 21:02:19 -07:00
}
async hasChatIdInUrl() {
await expect(this.page).toHaveURL(CHAT_ID_REGEX);
2025-03-09 21:02:19 -07:00
}
async getAssistantMessages() {
return await this.page.getByTestId("message-assistant").all();
2025-03-09 21:02:19 -07:00
}
async getUserMessages() {
return await this.page.getByTestId("message-user").all();
2025-03-09 21:02:19 -07:00
}
async getLastAssistantMessageContent(): Promise<string | null> {
const messages = await this.getAssistantMessages();
const lastMessage = messages.at(-1);
if (!lastMessage) {
return null;
2025-03-09 21:02:19 -07:00
}
const content = await lastMessage
.getByTestId("message-content")
.innerText();
return content;
}
async getLastUserMessageContent(): Promise<string | null> {
const messages = await this.getUserMessages();
const lastMessage = messages.at(-1);
if (!lastMessage) {
return null;
}
const content = await lastMessage
.getByTestId("message-content")
.innerText();
return content;
2025-03-09 21:02:19 -07:00
}
async isElementVisible(testId: string) {
await expect(this.page.getByTestId(testId)).toBeVisible();
}
2025-03-09 21:02:19 -07:00
async isElementNotVisible(testId: string) {
await expect(this.page.getByTestId(testId)).not.toBeVisible();
2025-03-09 21:02:19 -07:00
}
2025-04-25 23:40:15 -07:00
async expectToastToContain(text: string) {
await expect(this.page.getByTestId("toast")).toContainText(text);
2025-04-25 23:40:15 -07:00
}
async waitForInputToBeReady() {
await expect(this.multimodalInput).toBeVisible();
await expect(this.sendButton).toBeVisible();
}
2025-03-09 21:02:19 -07:00
}