From 99f87360beb5f13646a54b644b23f808ae3099f6 Mon Sep 17 00:00:00 2001 From: Nantawat Sukrisunt Date: Fri, 8 Nov 2024 20:58:48 +0700 Subject: [PATCH] Update upload file --- tests/helpers/dropdownUtils.ts | 27 +++++++++++++++++ tests/mockup/1x1.png | Bin 0 -> 95 bytes tests/test-1-project-apply.spec.ts | 46 ++++++++++------------------- 3 files changed, 42 insertions(+), 31 deletions(-) create mode 100644 tests/helpers/dropdownUtils.ts create mode 100644 tests/mockup/1x1.png diff --git a/tests/helpers/dropdownUtils.ts b/tests/helpers/dropdownUtils.ts new file mode 100644 index 0000000..9208ce0 --- /dev/null +++ b/tests/helpers/dropdownUtils.ts @@ -0,0 +1,27 @@ +// dropdownUtils.ts +import { Page, Locator } from '@playwright/test'; + +export const selectFirstOption = async (page: Page, triggerLocator: Locator) => { + let selected = false + while (!selected) { + try { + await triggerLocator.hover(); + await triggerLocator.click({ force: true }); + + // Select the first available option + const firstOption = page.getByRole("option").first(); + await firstOption.waitFor({ state: 'visible', timeout: 1000 }); + + // Retrieve and log the text content of the first option + const optionText = await firstOption.textContent(); + console.log(`${optionText}`); + await firstOption.click(); + console.log("Selected."); + + selected = true; + } catch (error) { + console.log("Retrying as the combobox disappeared."); + await page.waitForTimeout(100); + } + } +}; diff --git a/tests/mockup/1x1.png b/tests/mockup/1x1.png new file mode 100644 index 0000000000000000000000000000000000000000..1914264c08781d1f30ee0b8482bccf44586f2dc1 GIT binary patch literal 95 zcmeAS@N?(olHy`uVBq!ia0vp^j3CU&3?x-=hn)ga%mF?ju0VQumF+E%TuG2$FoVOh l8)-lem#2$k2*>s01R$Gz9%CSj!PC{xWt~$(697H@6ZHT9 literal 0 HcmV?d00001 diff --git a/tests/test-1-project-apply.spec.ts b/tests/test-1-project-apply.spec.ts index b8f9b0d..6d78c6e 100644 --- a/tests/test-1-project-apply.spec.ts +++ b/tests/test-1-project-apply.spec.ts @@ -1,51 +1,35 @@ -import { test, Locator, Page } from '@playwright/test'; - -// Helper function to handle dropdown selection with retry mechanism, automatically selecting the first option -const selectFirstOption = async (page: Page, triggerLocator: Locator) => { - - try { - await triggerLocator.hover(); - await triggerLocator.click({ force: true }); - - // Select the first available option - const firstOption = page.getByRole("option").first(); - await firstOption.waitFor({ state: 'visible', timeout: 1000 }); - await firstOption.click(); - console.log("First option selected."); - - } catch (error) { - console.log("Retrying as the combobox disappeared..."); - await page.waitForTimeout(100); - } - -}; +import { test } from '@playwright/test'; +import { selectFirstOption } from './helpers/dropdownUtils'; +import path from 'path'; test('test', async ({ page }) => { await page.goto("http://127.0.0.1:3000/"); await page.getByRole('button', { name: 'Projects' }).hover(); await page.getByRole('link', { name: 'Projects Start your new' }).click(); - await page.locator('#projectName').fill('DummyTester'); - await page.locator('#projectName').fill('DummyTester'); + const projectName = page.locator('#projectName') + await projectName.pressSequentially('DummyTester'); + await projectName.click(); + + const img = path.join(__dirname, 'mockup', '1x1.png'); + await page.locator('#projectLogo').click(); + await page.locator('#projectLogo').setInputFiles(img); + await page.locator('#projectPhotos').click(); + await page.locator('#projectPhotos').setInputFiles(img); - // Select first option in 'Select a Project type' const projectTypeButton = page.locator('button').filter({ hasText: 'Select a Project type' }); await selectFirstOption(page, projectTypeButton); await page.locator('#shortDescription').fill('0123456789'); - await page.getByPlaceholder('https:// ').fill('https://www.google.com/'); + await page.getByPlaceholder('https:// ').fill('https://www.test.md'); await page.getByPlaceholder('$ 500').fill('499'); await page.getByPlaceholder('$ 1,000,000').fill('99999999'); await page.locator('#deadline').fill('2024-11-29T21:19'); - // Log the text content of the second combobox const tag = page.getByRole('combobox').nth(1); - const tagText = await tag.textContent(); - console.log('Tag text:', tagText); - - // Select first option in the tag combobox await selectFirstOption(page, tag); - // Submit the form + await projectName.pressSequentially('1234'); + await page.getByRole('button', { name: 'Submit application' }).click(); });