|
| 1 | +const { test, expect } = require('@playwright/test'); |
| 2 | + |
| 3 | +test('Check Simple and Multi-Level Dropdown functionality', async ({ page }) => { |
| 4 | + // Define for selectors |
| 5 | + const targetUrl = 'https://qa-practice.netlify.app/dropdowns'; |
| 6 | + const dropdownMenuSelector = '#dropdown-menu'; |
| 7 | + const dropdownBtnSelector = '#multi-level-dropdown-btn'; |
| 8 | + const someActionOptionSelector = '//a[text()="Some action"]'; |
| 9 | + const someOtherOptionSelector = '//a[text()="Some other action"]'; |
| 10 | + const hoverMeSelector = '//a[text()="Hover me for more options"]'; |
| 11 | + const hoverEvenMoreSelector = '//a[text()="Even More.."]'; |
| 12 | + const hoverAnotherLevelSelector = '//a[text()="another level"]'; |
| 13 | + |
| 14 | + // Second Level Options |
| 15 | + const secondLevelSelectors = [ |
| 16 | + '//a[text()="Second level - 1"]', |
| 17 | + '//a[text()="Second level - 2"]', |
| 18 | + '//a[text()="Second level -3"]', |
| 19 | + ]; |
| 20 | + |
| 21 | + // Third Level Options |
| 22 | + const thirdLevelSelectors = [ |
| 23 | + '//a[text()="3rd level - 1"]', |
| 24 | + '//a[text()="3rd level - 2"]', |
| 25 | + ]; |
| 26 | + |
| 27 | + // Fourth Level Options |
| 28 | + const fourthLevelSelectors = [ |
| 29 | + '//a[text()="4th level - 1"]', |
| 30 | + '//a[text()="4th level - 2"]', |
| 31 | + '//a[text()="4th level - 3"]', |
| 32 | + ]; |
| 33 | + |
| 34 | + // Navigate to the page |
| 35 | + await page.goto(targetUrl); |
| 36 | + |
| 37 | + // Wait for dropdown to be visible and select a country |
| 38 | + await page.waitForSelector(dropdownMenuSelector); |
| 39 | + const countryValue = 'India'; |
| 40 | + await page.selectOption(dropdownMenuSelector, { value: countryValue }); |
| 41 | + |
| 42 | + // Validate selection |
| 43 | + const selectedValue = await page.$eval(dropdownMenuSelector, el => el.value); |
| 44 | + expect(selectedValue).toBe(countryValue); |
| 45 | + console.log(`Successfully selected: ${countryValue}`); |
| 46 | + |
| 47 | + // Handle Dropdown Button and Options |
| 48 | + const dropdownBtn = page.locator(dropdownBtnSelector); |
| 49 | + await dropdownBtn.click(); |
| 50 | + await page.locator(someActionOptionSelector).click(); |
| 51 | + await dropdownBtn.click(); |
| 52 | + await page.locator(someOtherOptionSelector).click(); |
| 53 | + await dropdownBtn.click(); |
| 54 | + |
| 55 | + // Function to handle hover actions and validation |
| 56 | + const hoverAndValidate = async (hoverSelector, levelSelectors, levelName) => { |
| 57 | + const hoverElement = page.locator(hoverSelector); |
| 58 | + await hoverElement.hover(); |
| 59 | + await page.waitForTimeout(1000); // Wait for the next level to appear |
| 60 | + |
| 61 | + for (const selector of levelSelectors) { |
| 62 | + const levelOption = page.locator(selector); |
| 63 | + await levelOption.waitFor({ state: 'visible', timeout: 10000 }); |
| 64 | + if (await levelOption.isVisible()) { |
| 65 | + console.log(`${levelName} option visible: ${await levelOption.textContent()}`); |
| 66 | + } else { |
| 67 | + console.error(`${levelName} option is not visible`); |
| 68 | + } |
| 69 | + } |
| 70 | + }; |
| 71 | + |
| 72 | + // Hover over each level and validate visibility |
| 73 | + await hoverAndValidate(hoverMeSelector, secondLevelSelectors, 'Second level'); |
| 74 | + await hoverAndValidate(hoverEvenMoreSelector, thirdLevelSelectors, 'Third level'); |
| 75 | + await hoverAndValidate(hoverAnotherLevelSelector, fourthLevelSelectors, 'Fourth level'); |
| 76 | +}); |
0 commit comments