Skip to content

Commit 1521457

Browse files
Modify DatePickers.spec.js script
1 parent 5d2d926 commit 1521457

File tree

1 file changed

+27
-11
lines changed

1 file changed

+27
-11
lines changed

tests/Date Pickers/DatePickers.spec.js

Lines changed: 27 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,14 @@ import { test, expect } from '@playwright/test';
33
test('Verify Calendar Functionality', async ({ page }) => {
44
// Test Data
55
const targeturl = 'https://qa-practice.netlify.app/calendar';
6-
const rangeStartDate = '1';
7-
const rangeEndDate = '10';
8-
const expectedRangeValue = '01/01/2018 - 01/10/2018';
6+
const rangeStartDate = '1';
7+
const rangeEndDate = '10';
8+
const expectedRangeValue = '01/01/2018 - 01/10/2018';
99

1010
// Get today's date
1111
const today = new Date();
12-
const todayDate = today.getDate().toString();
13-
const todayMonthYear = today.toLocaleString('default', { month: 'long', year: 'numeric' }); // Month and year (e.g., "January 2025")
12+
const todayDate = today.getDate().toString();
13+
const todayMonthYear = today.toLocaleString('default', { month: 'long', year: 'numeric' });
1414

1515
// Format today's date to MM/DD/YYYY
1616
const formattedTodayDate = `${String(today.getMonth() + 1).padStart(2, '0')}/${String(today.getDate()).padStart(2, '0')}/${today.getFullYear()}`;
@@ -20,18 +20,34 @@ test('Verify Calendar Functionality', async ({ page }) => {
2020

2121
// Range Date Picker Example
2222
await page.locator('#range-date-calendar').click();
23-
await page.getByRole('cell', { name: rangeStartDate, exact: true }).first().click();
24-
await page.getByRole('cell', { name: rangeEndDate }).first().click();
23+
await page.getByRole('cell', { name: rangeStartDate, exact: true }).first().click();
24+
await page.getByRole('cell', { name: rangeEndDate }).first().click();
2525
await page.getByRole('button', { name: 'Apply' }).click();
2626

2727
// Basic Date Picker Example
2828
await page.getByPlaceholder('Pick a date').click();
29-
await page.locator('div').filter({ hasText: todayMonthYear }).first().click();
30-
await page.getByRole('cell', { name: todayDate }).click();
29+
30+
// Wait for the correct month/year to be displayed
31+
while (true) {
32+
// Filter to find the correct 'th.datepicker-switch' element for month and year
33+
const visibleHeader = await page.locator('th.datepicker-switch')
34+
.filter({ hasText: /^[A-Za-z]+\s\d{4}$/ })
35+
.first();
36+
37+
const calendarHeader = await visibleHeader.textContent();
38+
if (calendarHeader?.trim() === todayMonthYear) break;
39+
40+
await page.locator('button.next').click();
41+
}
42+
43+
// Wait for the target day to be visible and click it
44+
const dayLocator = page.locator('td.day').filter({ hasText: todayDate });
45+
await dayLocator.waitFor({ state: 'visible' });
46+
await dayLocator.click();
3147

3248
// Validate date picker interactions
33-
await expect(page.getByPlaceholder('Pick a date')).toHaveValue(formattedTodayDate);
49+
await expect(page.getByPlaceholder('Pick a date')).toHaveValue(formattedTodayDate);
3450

3551
// Assert the selected range date values
36-
await expect(page.locator('#range-date-calendar')).toHaveValue(expectedRangeValue);
52+
await expect(page.locator('#range-date-calendar')).toHaveValue(expectedRangeValue);
3753
});

0 commit comments

Comments
 (0)