import { test, expect } from '@playwright/test';

test('#9 Google Play button should redirect to Play Store, not download APK', async ({ page, context }) => {
  // Step 1: Go to the site
  await page.goto('https://uat.dailykart.net');

  // Step 2: Scroll to footer (ensures element is in view)
  await page.evaluate(() => window.scrollTo(0, document.body.scrollHeight));
  await page.waitForTimeout(1000); // Wait for any lazy-loaded footer elements

  // Step 3: Locate and click the Google Play button
  const playButton = page.locator('img[alt*="Google Play"], a:has(img[alt*="Google Play"])');
  await expect(playButton).toBeVisible();

  // Intercept the navigation to verify the URL
  const [newPage] = await Promise.all([
    context.waitForEvent('page'),
    playButton.click(), // opens a new tab or window
  ]);

  await newPage.waitForLoadState();

  const finalURL = newPage.url();

  // Step 4: Validate URL
  console.log(' Opened URL:', finalURL);
  expect(finalURL).toContain('play.google.com'); // Should be Play Store
  expect(finalURL).not.toMatch(/\.apk$/); // Should NOT be a direct APK link
});
