{"id":58649,"date":"2023-09-21T10:21:45","date_gmt":"2023-09-21T04:51:45","guid":{"rendered":"https:\/\/www.tothenew.com\/blog\/?p=58649"},"modified":"2023-09-28T10:24:59","modified_gmt":"2023-09-28T04:54:59","slug":"playwright-with-ci-cd-harnessing-headless-browsers-xvfb-for-seamless-automation-in-node-js","status":"publish","type":"post","link":"https:\/\/www.tothenew.com\/blog\/playwright-with-ci-cd-harnessing-headless-browsers-xvfb-for-seamless-automation-in-node-js\/","title":{"rendered":"Playwright with CI\/CD: Harnessing Headless Browsers (Xvfb) for Seamless Automation in Node JS"},"content":{"rendered":"<p><span style=\"font-weight: 400;\"><img decoding=\"async\" loading=\"lazy\" class=\"wp-image-58650 aligncenter\" src=\"\/blog\/wp-ttn-blog\/uploads\/2023\/09\/Screenshot-from-2023-09-24-13-09-07-300x159.png\" alt=\"\" width=\"306\" height=\"162\" srcset=\"\/blog\/wp-ttn-blog\/uploads\/2023\/09\/Screenshot-from-2023-09-24-13-09-07-300x159.png 300w, \/blog\/wp-ttn-blog\/uploads\/2023\/09\/Screenshot-from-2023-09-24-13-09-07-624x331.png 624w, \/blog\/wp-ttn-blog\/uploads\/2023\/09\/Screenshot-from-2023-09-24-13-09-07.png 699w\" sizes=\"(max-width: 306px) 100vw, 306px\" \/><\/span><\/p>\n<p>In the fast-paced world of software development, automation has become the key to ensuring the quality and reliability of web applications. When it comes to web testing and scraping, Playwright has emerged as a powerful tool for automation and interactions with web pages across multiple browsers. In this blog, we&#8217;ll delve into the exciting realm of Playwright and explore how to set up Continuous Integration\/Continuous Deployment (CI\/CD) pipelines with Headless Browsers using magical Xvfb (X Virtual Framebuffer).<\/p>\n<p><em><span style=\"font-weight: 400;\">Introducing Xvfb: The Headless Wonder<\/span><\/em><\/p>\n<p><span style=\"font-weight: 400;\">Xvfb, short for X Virtual Framebuffer, is a Linux-based tool that creates a virtual display server. It&#8217;s particularly handy when you need to run graphical applications on a system that doesn&#8217;t have a physical display. In the context of web automation, Xvfb allows you to run headless browsers\u2014browsers without a graphical user interface (GUI)\u2014on servers or in CI\/CD environments. This is a game-changer when you need to automate browser interactions in a server environment.<\/span><\/p>\n<p><em><span style=\"font-weight: 400;\">Setting Up Playwright with CI\/CD and Xvfb<\/span><\/em><\/p>\n<p><span style=\"font-weight: 400;\">Now that we&#8217;ve covered the essentials, let&#8217;s explore how to set up Playwright with CI\/CD and Xvfb:<\/span><\/p>\n<ol>\n<li><span style=\"font-weight: 400;\"><span style=\"font-weight: 400;\">\u00a0<strong>Choose Your CI\/CD Platform<\/strong>: Popular CI\/CD platforms like J<em>enkins, Travis CI, CircleCI<\/em>, and <em>GitHub<\/em> Actions all support Playwright. Select the one that best suits your needs and set up your project.\n<p><\/span><\/span><\/li>\n<li style=\"font-weight: 400;\"><span style=\"font-weight: 400;\"><strong>\u00a0Install Xvfb<\/strong>: Depending on your Linux distribution, you can install Xvfb using package managers like <\/span><em><span style=\"font-weight: 400;\">apt<\/span><span style=\"font-weight: 400;\">, <\/span><span style=\"font-weight: 400;\">yum<\/span><span style=\"font-weight: 400;\">, or <\/span><span style=\"font-weight: 400;\">zypper<\/span><\/em><span style=\"font-weight: 400;\"><span style=\"font-weight: 400;\">. Once installed, you can start Xvfb on a virtual display. Playwright <a href=\"https:\/\/playwright.dev\/docs\/docker\">Docker image<\/a>\u00a0and GitHub Action have Xvfb pre-installed.\n<p><\/span><\/span><\/li>\n<li style=\"font-weight: 400; text-align: left;\"><span style=\"font-weight: 400;\"><span style=\"font-weight: 400;\"><span style=\"font-weight: 400;\"><strong>Configure Playwright<\/strong>: In your CI\/CD pipeline, configure Playwright to use the virtual display provided by Xvfb.\u00a0This ensures that your Playwright scripts run seamlessly.\n<p><\/span><\/span><\/span><\/li>\n<li><span style=\"font-weight: 400;\"><span style=\"font-weight: 400;\"><span style=\"font-weight: 400;\"><strong>Write and Run Tests<\/strong>: Create Playwright scripts that automate your web interactions. These scripts can be written in JavaScript or TypeScript. Run your tests in the CI\/CD pipeline to ensure that they execute successfully in a headless environment. Playwright launches browsers in headless by default. This can be changed by passing a flag when the browser is launched.<\/span><\/span><\/span>&nbsp;\n<pre><em>\/\/ Works across chromium, firefox and webkit<\/em>\r\n<em>const { chromium } = require('playwright');<\/em>\r\n<em><em>const browser = await chromium.launch({ headless: false });<\/em><\/em>\/\/or In playwright.config file\r\n<em style=\"font-size: 1rem;\">use: { <\/em><em style=\"font-size: 1rem;\">headless: false <\/em><em style=\"font-size: 1rem;\">}<\/em><\/pre>\n<div><\/div>\n<p><span class=\"token plain\"><span class=\"token plain\">It will allow the browser to <span style=\"font-weight: 400;\">run headed browsers<\/span> <span style=\"font-weight: 400;\">without a graphical user interface (GUI) on servers or in CI\/CD environments. <\/span>To run browsers in headed mode with Xvfb, add <code>xvfb-run<\/code> before the test execution node command.<\/span><\/span><\/p>\n<pre><em>xvfb-run <\/em><em>npx playwright test<\/em>\r\n<em>\/\/ or can pass the arguments below\u00a0<\/em><\/pre>\n<div>\n<pre><em><em>xvfb-run --auto-servernum --server-num=1 --server-args='-screen 0, 1920x1080x24' npx playwright test<\/em><\/em><\/pre>\n<p>Here&#8217;s the breakdown of each part of the command:<\/p>\n<ul>\n<li><code>xvfb-run<\/code>: This is the main command that starts a virtual X server using Xvfb and runs the specified graphical application within this virtual X server environment.<\/li>\n<li><code>--auto-servernum<\/code>: This option tells <code>xvfb-run<\/code> to automatically select a free display server number. In your case, it would select the next available server number.<\/li>\n<li><code>--server-num=1<\/code>: This option specifies the server number to use. In your command, it explicitly sets the server number to 1.<\/li>\n<li><code>--server-args='-screen 0, 1920x1080x24'<\/code>: This part specifies additional arguments to be passed to the Xvfb server. In this case, it configures the virtual display with screen 0 to have a resolution of 1920&#215;1080 pixels and a color depth of 24 bits per pixel (which is a typical setting for a full-color display).<\/li>\n<\/ul>\n<p><em><em><span style=\"font-weight: 400;\">5.<\/span><\/em><\/em> <span style=\"font-weight: 400;\"><strong>Monitor and Debug<\/strong>: Set up monitoring and logging to track the performance of your tests in the CI\/CD pipeline. When issues arise, use the logs and debugging tools provided by Playwright to diagnose and fix them.<\/span><\/p>\n<p><strong>Conclusion<\/strong><\/p>\n<p><span style=\"font-weight: 400;\">Incorporating Playwright with CI\/CD and Xvfb into your development workflow can significantly improve the efficiency and reliability of your web automation tasks. By automating browser interactions and running headless tests, you can catch regressions issues early, ensuring that your web applications deliver a seamless user experience.<\/span><\/p>\n<p><span style=\"font-weight: 400;\">So, take the leap into the world of Playwright, CI\/CD, and Xvfb, and unlock the potential of automated web testing and scraping.<\/span><\/p>\n<\/div>\n<\/li>\n<\/ol>\n<div class=\"ap-custom-wrapper\"><\/div><!--ap-custom-wrapper-->","protected":false},"excerpt":{"rendered":"<p>In the fast-paced world of software development, automation has become the key to ensuring the quality and reliability of web applications. When it comes to web testing and scraping, Playwright has emerged as a powerful tool for automation and interactions with web pages across multiple browsers. In this blog, we&#8217;ll delve into the exciting realm [&hellip;]<\/p>\n","protected":false},"author":1645,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"iawp_total_views":1509},"categories":[1818,1185],"tags":[4941,5184,5464,5465],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/58649"}],"collection":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/users\/1645"}],"replies":[{"embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/comments?post=58649"}],"version-history":[{"count":5,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/58649\/revisions"}],"predecessor-version":[{"id":58808,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/58649\/revisions\/58808"}],"wp:attachment":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/media?parent=58649"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/categories?post=58649"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/tags?post=58649"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}