Automate 'Check My Links' through Playwright

Check My Links –  Download Link

CRX plugin link to get the extension folder – Download Link

Please use the below code to automate Check My Links using Playwright. Kindly follow the attached tutorial below for the detailed explanation and working demo.

Code to automate one Page

				
					import { test as base, chromium, BrowserContext } from '@playwright/test';
import path from 'path';

export const test = base.extend<{
  context: BrowserContext;
  extensionId: string;
}>({
  context: async ({ }, use) => {
    const pathToExtension = path.join(__dirname, 'checkmylinks_demo');
    const context = await chromium.launchPersistentContext('', {
      headless: false,
      args: [
        `--disable-extensions-except=${pathToExtension}`,
        `--load-extension=${pathToExtension}`,
      ],
    });
    await use(context);
    await context.close();
  },
  extensionId: async ({ context }, use) => {
    // for manifest v3:
    let [background] = context.serviceWorkers();
    if (!background)
      background = await context.waitForEvent('serviceworker');

    const extensionId = background.url().split('/')[2];
    await use(extensionId);
  },
});
export const expect = test.expect;

test('example test', async ({ page }) => {
  await page.goto('https://selectorshub.com/xpath-practice-page/');
  
  await page.waitForTimeout(5000);
  const iframe = await page.frameLocator("//iframe[@id='check-my-link-iframe']")
  
  await iframe.locator("xpath=//strong[@class='queued'][text()='0']").waitFor();
  const locator =   iframe.locator("xpath=//strong[@class='queued'][text()='0']");
  const count =  await locator.count();
  
  console.log("count value-"+count);

  if (count > 0) {
    console.log("count value-"+count);
    // Start waiting for download before clicking. Note no await.
    const downloadPromise = page.waitForEvent('download');
    await iframe.locator("xpath=//span[text()='Export All']").click();
    const download = await downloadPromise;
    // Wait for the download process to complete and save the downloaded file somewhere.
    await download.saveAs('/Users/sanjaykumar/Downloads/' + download.suggestedFilename());
  }

  await page.waitForTimeout(20000);
});


				
			

Code to automate Multiple Pages & get report for all in one go (recommended)

				
					import { test as base, chromium, BrowserContext } from '@playwright/test';
import path from 'path';

export const test = base.extend<{
  context: BrowserContext;
  extensionId: string;
}>({
  context: async ({ }, use) => {
    const pathToExtension = path.join(__dirname, 'checkmylinks_demo');
    const context = await chromium.launchPersistentContext('', {
      headless: false,
      args: [
        `--disable-extensions-except=${pathToExtension}`,
        `--load-extension=${pathToExtension}`,
      ],
    });
    await use(context);
    await context.close();
  },
  extensionId: async ({ context }, use) => {
    // for manifest v3:
    let [background] = context.serviceWorkers();
    if (!background)
      background = await context.waitForEvent('serviceworker');
    const extensionId = background.url().split('/')[2];
    await use(extensionId);
  },
});

export const expect = test.expect;
const fs = require('fs');

test('example test', async ({ page }) => {
  await page.goto('https://google.com/');
  await downloadReport(page, "google1.csv");
});

test('example test1', async ({ page }) => {
  await page.goto('https://tricentis.com/');
  await downloadReport(page, "tricentis1.csv");
});

test('example test2', async ({ page }) => {
  await page.goto('https://selectorshub.com/');
  await downloadReport(page, "selectorshub1.csv");
});

test('example test3', async ({ page }) => {
  await page.goto('https://selectorshub.com/xpath-practice-page/');
  await downloadReport(page, "practicepage.csv");
});

async function downloadReport(page, fileName){
  await page.waitForTimeout(5000);
  const iframe = await page.frameLocator("//iframe[@id='check-my-link-iframe']")
  
  await iframe.locator("xpath=//strong[@class='queued'][text()='0']").waitFor();
  const locator =   iframe.locator("xpath=//strong[@class='queued'][text()='0']");
  const count =  await locator.count();
  
  console.log("count value-"+count);

  if (count > 0) {
    console.log("count value-"+count);
    // Start waiting for download before clicking. Note no await.
    const downloadPromise = page.waitForEvent('download');
    await iframe.locator("xpath=//span[text()='Export All']").click();
    const download = await downloadPromise;

    // Wait for download to complete
  const tempPath = await download.path();

  if (tempPath) {
    const newFileName = fileName; // Change to your desired name
    const targetDir = '/Users/sanjaykumar/Downloads/';  // Ensure this directory exists
    const newPath = path.join(targetDir, newFileName);

    fs.renameSync(tempPath, newPath);
    console.log(`File renamed to: ${newPath}`);
  } else {
    console.log('Download path not found!');
  }
}
}