joplin-notes/joplin notion extension/Web Development/nodejs/playwright.md
2026-11-04 00:33:23 +05:30

4.5 KiB

playwright https://playwright.dev/docs/intro

  • npm i playwright and npx playwright install firefox to download firefox
  • Normal branded firefox wont work with playwright. playwright will download its own patch of firefox   Simple example
const { firefox } = require("playwright");

(async () => {
  const browser = await firefox.launch({
    // executablePath: "/usr/lib/firefox/firefox",
    headless: false,
  });
  const page = await browser.newPage();
  await page.goto("https://playwright.dev/");
	
  // .DocSearch is a CSS selector for search button
  await page.locator(".DocSearch").click();
  //   await browser.close();
})();

  • In order to preserve profile use launchPersistentContext method instead of launch. eg
const { firefox } = require("playwright");

(async () => {
  const browser = await firefox.launchPersistentContext(
    "/home/hpes/.mozilla/firefox/r7e7y6qd.playwright",
    {
      // executablePath: "/usr/lib/firefox/firefox",
      headless: false,
      timeout: 0,
      //   args: ["--profile-directory=playwright"],
    }
  );
  const page = await browser.newPage();
  await page.goto("https://rumble.com");
  //   await page.locator(".DocSearch").click();
  //   await browser.close();
})();

  • for uploading videos in rumble
const promiseSetTimeout = (time) => {
  return new Promise((resolve) => {
    setTimeout(resolve, time);
  });
};

const { firefox } = require("playwright");
const path = require("path");

const videoTitle = "The Witcher 3 evil soft first touches p3 toad price fight";
const tagsValue = "#gaming #witcher3";
const channelValue = "6448233";

const video_dirname =
  "/mnt/hdd/windows/witcher 3 reocrded videos/1080p/original/missions/hos/evil soft first touches/";

const video_filename =
  "The Witcher 3 evil soft first touches p3 toad price fight.mp4";

const thumbnail_dirname =
  "/mnt/hdd/windows/witcher 3 reocrded videos/1080p/original/missions/hos/evil soft first touches/assets";
const thumbnail_filename =
  "evil soft first touches p3 fighting toad prince.png";
(async () => {
  const browser = await firefox.launchPersistentContext(
    "/home/hpes/.mozilla/firefox/r7e7y6qd.playwright",
    {
      // executablePath: "/usr/lib/firefox/firefox",
      headless: false,
      timeout: 0,
      //   args: ["--profile-directory=playwright"],
    }
  );

  //   const page1 = await browser.newPage();
  //   const page2 = await browser.newPage();

  //   await page1.goto("https://rumble.com/upload.php");
  //   await page2.goto("https://rumble.com/upload.php");

  //   await page1.waitForSelector(".title");

  //   const videoCompleteh3Handle = await page1.locator(".title");

  //   // completion of upload successfully
  //   videoCompleteh3Handle.innerText() === "Video Upload Complete!";

  const page = await browser.newPage();

  await page.goto("https://rumble.com/upload.php");

  // filling video title
  await page.locator("#title").fill(videoTitle);

  // filling primary category
  await page
    .locator(
      ".video-details > div:nth-child(7) > div:nth-child(1) > div:nth-child(1) > input:nth-child(2)"
    )
    .fill("Gaming");

  // filling tags
  await page.locator("#tags").fill(tagsValue);

  const selectHandle = await page.locator("#channelId");

  selectHandle.selectOption(channelValue);

  const uploadVideoHandle = await page.locator("#Filedata");

  uploadVideoHandle.setInputFiles(path.join(video_dirname, video_filename));

  await promiseSetTimeout(3000);

  // uploading custom thumbnail
  const uploadThumbnailHandle = await page.locator("#customThumb");
  uploadThumbnailHandle.setInputFiles(
    path.join(thumbnail_dirname, thumbnail_filename)
  );

  await promiseSetTimeout(2000);

  const uploadButtonHandle = await page.locator("#submitForm");

  // go to next step for final submission
  uploadButtonHandle.click();

  await promiseSetTimeout(4000);

  // checking checkboxes before final submission
  //   await page.locator("#crights").check();
  //   await page.locator("#cterms").check();

  //   await page.locator("#crights").click();
  //   await page.locator("#cterms").click();

  // crights
  await page
    .locator("div.form-checkbox:nth-child(2) > label:nth-child(2)")
    .click();

  // cterms
  await page
    .locator("div.form-checkbox:nth-child(3) > label:nth-child(2)")
    .click();

  await promiseSetTimeout(2000);

  // finally submitting it
  // await page.locator("#submitForm2").click();

  //   await page.locator(".DocSearch").click();
  //   await browser.close();
})();