Skip to content

Commit

Permalink
update(test): validate file was downloaded correctly (#159)
Browse files Browse the repository at this point in the history
  • Loading branch information
luisecm authored Sep 19, 2024
1 parent 86aed9a commit 9c9950c
Show file tree
Hide file tree
Showing 2 changed files with 127 additions and 13 deletions.
61 changes: 48 additions & 13 deletions playwright/PageObjects/ChatsMain.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import MainPage from "./MainPage";
import { expect, type Locator, type Page } from "@playwright/test";
const fs = require("fs");

export class ChatsMainPage extends MainPage {
readonly addSomeone: Locator;
Expand Down Expand Up @@ -891,27 +892,45 @@ export class ChatsMainPage extends MainPage {
await deleteButton.click();
}

async downloadFileLastMessage(type: string = "file", sent: boolean = true) {
async downloadFileLastMessage(filename: string, sent: boolean = true) {
// Declare variable for file locator
let fileLocator: Locator;
if (sent) {
if (type === "file") {
fileLocator = await this.getLastFilesSent();
} else {
let downloadButton: Locator;

// Extract the file extension from the filename
const extension = filename.split(".").pop();

// Define a list of image file extensions
const imageExtensions = ["jpg", "png", "gif"];

if (imageExtensions.includes(extension)) {
if (sent) {
fileLocator = await this.getLastImagesSent();
} else {
fileLocator = await this.getLastImagesReceived();
}
downloadButton = fileLocator.getByTestId("image-embed-download-button");
} else {
if (type === "file") {
fileLocator = await this.getLastFilesReceived();
if (sent) {
fileLocator = await this.getLastFilesSent();
} else {
fileLocator = await this.getLastImagesReceived();
fileLocator = await this.getLastFilesReceived();
}
downloadButton = fileLocator.getByTestId("file-embed-download-button");
}
const downloadButton = fileLocator.getByTestId(
type === "file"
? "file-embed-download-button"
: "image-embed-download-button",
);

// Wait for the download event
const downloadPromise = this.page.waitForEvent("download");
await downloadButton.click();
const download = await downloadPromise;

// Save the file manually to the specified folder
const savedFilePath = "./downloads/" + filename;

await download.saveAs(savedFilePath); // Save the file to the desired folder

// Return the saved file path for further validation
return savedFilePath;
}

async getFilePreview(filename: string) {
Expand Down Expand Up @@ -985,6 +1004,22 @@ export class ChatsMainPage extends MainPage {
}
}

async validateDownloadedFile(filename: string) {
const downloadPath = "./downloads/" + filename; // Specify your download folder

// Check if the file exists
const fileExists = fs.existsSync(downloadPath);

// Assert that the file exists
if (fileExists) {
console.log(`File ${filename} was downloaded successfully.`);
return true;
} else {
console.error(`File ${filename} was not found.`);
return false;
}
}

async validateFilePreviews(filePaths: string[]) {
const filenames = filePaths.map((path) => path.split("/").pop());
const fileExtensions = filenames.map((path) => "." + path.split(".").pop());
Expand Down
79 changes: 79 additions & 0 deletions playwright/specs/03-friends-two-instances.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1488,14 +1488,93 @@ test.describe("Two instances tests - Friends and Chats", () => {
false,
);

// B53 - User can download media from chat by clicking download
// Download last files sent and received
await chatsMainPageSecond.downloadFileLastMessage("test.txt", true);
await chatsMainPageSecond.validateDownloadedFile("test.txt");
await chatsMainPageFirst.downloadFileLastMessage("test.txt", false);
await chatsMainPageFirst.validateDownloadedFile("test.txt");

// Download last images sent and received
await chatsMainPageSecond.downloadFileLastMessage("logo.jpg", true);
await chatsMainPageSecond.validateDownloadedFile("logo.jpg");
await chatsMainPageFirst.downloadFileLastMessage("logo.jpg", false);
await chatsMainPageFirst.validateDownloadedFile("logo.jpg");

// B52 - User should be able to click on image in chat to see image preview
await chatsMainPageSecond.openImagePreviewLastImageSent();
await chatsMainPageSecond.validateImagePreviewIsVisible();
await chatsMainPageSecond.closeImagePreview();

await chatsMainPageFirst.openImagePreviewLastImageReceived();
await chatsMainPageFirst.validateImagePreviewIsVisible();
await chatsMainPageFirst.closeImagePreview();
});

test("Sending and receiving emojis, gifs and stickers tests", async ({
firstUserContext,
secondUserContext,
}) => {
// Declare constants required from the fixtures
const context1 = firstUserContext.context;
const page1 = firstUserContext.page;
const page2 = secondUserContext.page;
const friendsScreenFirst = new FriendsScreen(page1);
const friendsScreenSecond = new FriendsScreen(page2);
const chatsMainPageFirst = new ChatsMainPage(page1);
const chatsMainPageSecond = new ChatsMainPage(page2);

// Setup accounts for testing
await setupChats(
chatsMainPageFirst,
chatsMainPageSecond,
context1,
friendsScreenFirst,
friendsScreenSecond,
page1,
);

let fileLocations = [
"playwright/assets/logo.jpg",
"playwright/assets/test.txt",
];

await chatsMainPageSecond.uploadFiles(fileLocations);
await chatsMainPageSecond.validateFilePreviews(fileLocations);
await chatsMainPageSecond.sendMessage("bunch of files");

// Validate file sent is displayed on local side
await chatsMainPageSecond.validateFileEmbedInChat("test.txt", "14 B", true);

// Validate image sent is displayed on local side
await chatsMainPageSecond.validateImageEmbedInChat(
"logo.jpg",
"7.75 kB",
true,
);

// Validate file received is displayed in chat on remote side
await chatsMainPageFirst.validateFileEmbedInChat("test.txt", "14 B", false);

// Validate image received is displayed in chat on remote side
await chatsMainPageFirst.validateImageEmbedInChat(
"logo.jpg",
"7.75 kB",
false,
);

// B53 - User can download media from chat by clicking download
// Download last files sent and received
await chatsMainPageSecond.downloadFileLastMessage("file", true);
await chatsMainPageSecond.validateDownloadedFile("test.txt");
await chatsMainPageFirst.downloadFileLastMessage("file", false);
await chatsMainPageFirst.validateDownloadedFile("test.txt");

// Download last images sent and received
await chatsMainPageSecond.downloadFileLastMessage("image", true);
await chatsMainPageSecond.validateDownloadedFile("logo.jpg");
await chatsMainPageFirst.downloadFileLastMessage("image", false);
await chatsMainPageFirst.validateDownloadedFile("logo.jpg");

// B52 - User should be able to click on image in chat to see image preview
await chatsMainPageSecond.openImagePreviewLastImageSent();
Expand Down

0 comments on commit 9c9950c

Please sign in to comment.