<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>Forem: HeadlessTesting</title>
    <description>The latest articles on Forem by HeadlessTesting (@headlesstesting).</description>
    <link>https://forem.com/headlesstesting</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F355297%2F6467abbd-ad76-416e-baaa-e8f6a32f3fd2.png</url>
      <title>Forem: HeadlessTesting</title>
      <link>https://forem.com/headlesstesting</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://forem.com/feed/headlesstesting"/>
    <language>en</language>
    <item>
      <title>Screen recording with Playwright</title>
      <dc:creator>HeadlessTesting</dc:creator>
      <pubDate>Wed, 03 Jun 2020 15:48:54 +0000</pubDate>
      <link>https://forem.com/headlesstesting/screen-recording-with-playwright-5dm0</link>
      <guid>https://forem.com/headlesstesting/screen-recording-with-playwright-5dm0</guid>
      <description>&lt;p&gt;When you run headless tests with Playwright, the browser will run the test without showing any browser window (UI).&lt;/p&gt;

&lt;p&gt;The &lt;em&gt;advantage&lt;/em&gt; of headless testing is that it is much faster to run tests than for example with Selenium.&lt;br&gt;
One &lt;em&gt;disadvantage&lt;/em&gt; however is that it makes it harder to debug any problems, since you can not record the UI into a video file.&lt;/p&gt;

&lt;p&gt;If you're looking to record videos of your browser sessions, then you can use a NPM package called &lt;code&gt;playwright-video&lt;/code&gt;. This package uses the DevTools protocol (more specifically &lt;code&gt;Page.startScreencast&lt;/code&gt;) to capture screencastFrame's and glue these together with &lt;code&gt;ffmpeg&lt;/code&gt;.&lt;/p&gt;
&lt;h2&gt;
  
  
  Installation
&lt;/h2&gt;

&lt;p&gt;To start, please install the necessary NPM packages:&lt;br&gt;
&lt;code&gt;npm i -D playwright-video ffmpeg-static&lt;/code&gt;&lt;/p&gt;
&lt;h2&gt;
  
  
  Running your first test
&lt;/h2&gt;

&lt;p&gt;Now you can run your first test with video. The example below will open a remote browser session in the &lt;a href="https://headlesstesting.com"&gt;HeadlessTesting&lt;/a&gt; cloud and send the video frames back. &lt;code&gt;playwright-video&lt;/code&gt; will then glue these frames together into a video file and save it on your disk.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const pw = require('playwright-core');
const { saveVideo } = require('playwright-video');

(async () =&amp;gt; {
  const browser = await pw.chromium.connect({
    wsEndpoint: 'wss://chrome.headlesstesting.com?token=[YOUR-TOKEN]&amp;amp;browserVersion=dev',
  });
  const context = await browser.newContext();
  const page = await context.newPage();

  await saveVideo(page, '/tmp/video.mp4');

  await page.goto('https://headlesstesting.com/');
  await page.screenshot({ path: 'screenshot.png' });

  await browser.close();
})();
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;This basic example will open a Chrome browser in the HeadlessTesting.com cloud, open a tab and go to our webpage, and then finally take a screenshot. It will save a video recording of these actions in a file called &lt;code&gt;/tmp/video.mp4&lt;/code&gt; on your disk.&lt;/p&gt;

&lt;h2&gt;
  
  
  Integration with Jest and Mocha
&lt;/h2&gt;

&lt;p&gt;This can be integrated in your favourite test runner as well. Simply add the logic to your before/after hooks to capture a video:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;let capture
beforeEach(async function() {
    this.timeout(35000)
    browser = await pw.chromium.connect({
        wsEndpoint: 'wss://chrome.headlesstesting.com?token=[YOUR-TOKEN]&amp;amp;browserVersion=dev',
    })
    context = await browser.newContext()
    page = await context.newPage()
    capture = await saveVideo(page, `${this.currentTest.title.replace(/ +/g, '_')}.mp4`)
})
afterEach(async function() {
    await capture.stop()
})
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



</description>
      <category>playwright</category>
      <category>headless</category>
      <category>automation</category>
      <category>screencast</category>
    </item>
    <item>
      <title>Headless Testing with Playwright and Jest</title>
      <dc:creator>HeadlessTesting</dc:creator>
      <pubDate>Mon, 11 May 2020 09:53:38 +0000</pubDate>
      <link>https://forem.com/headlesstesting/headless-testing-with-playwright-and-jest-5co3</link>
      <guid>https://forem.com/headlesstesting/headless-testing-with-playwright-and-jest-5co3</guid>
      <description>&lt;p&gt;Playwright is a NodeJS package which can be used to automate Chrome, Firefox, Edge and Safari browsers in a headless manner.&lt;/p&gt;

&lt;p&gt;It is developed by the same team that created Puppeteer at Google and is now actively developed at Microsoft.&lt;/p&gt;

&lt;p&gt;The advantage of using Headless Testing is that it uses a real browser, but is much faster to run your tests than for example Selenium.&lt;/p&gt;

&lt;p&gt;The combination of Jest, which is a NodeJS test runner framework maintained by Facebook and Playwright makes for a very robust Headless Testing toolkit.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Installing jest-playwright&lt;/strong&gt;&lt;br&gt;
There's a &lt;code&gt;jest-playwright&lt;/code&gt; NPM library which makes it easy to get started with Jest and Playwright.&lt;/p&gt;

&lt;p&gt;The package comes in combination with a &lt;code&gt;jest-playwright-preset&lt;/code&gt; which you can then use as a preset for Jest in &lt;code&gt;jest.config.js&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;To install these packages, please enter this command in your terminal:&lt;br&gt;
&lt;code&gt;npm install -D jest jest-playwright-preset playwright&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Once you've included the Playwright preset, you can either run the tests on a browser on your computer, or connect it to an online browser grid.&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;module.exports = {
  rootDir: '.',
  testTimeout: 20000,
  testMatch: [
    '/*.spec.js'
  ],
  preset: 'jest-playwright-preset'
}&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;&lt;strong&gt;Configuring jest-playwright&lt;/strong&gt;&lt;br&gt;
To connect your tests to a Headless Browser grid, please specify the &lt;a href="https://github.com/microsoft/playwright/blob/master/docs/api.md#browsertypeconnectoptions"&gt;connectBrowserApp&lt;/a&gt; setting in a file called &lt;code&gt;jest-playwright.config.js&lt;/code&gt;.&lt;/p&gt;



&lt;pre&gt;&lt;code&gt;module.exports = {
    connectBrowserApp: {
        wsEndpoint: 'wss://chrome.headlesstesting.com/?token=[YOUR-API-TOKEN]&amp;amp;browserVersion=dev'
    }
}&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;This will instruct Jest Playwright to start and use a headless Chrome browser on the HeadlessTesting.com grid.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Running your first test with jest-playwright&lt;/strong&gt;&lt;br&gt;
Now you are ready to run your first test with Jest and Playwright.&lt;br&gt;
To get started, please create a new test file &lt;code&gt;sample.spec.js&lt;/code&gt;:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;describe('Google', () =&amp;gt; {
  beforeAll(async () =&amp;gt; {
    await page.goto('https://google.com')
  })

  it('should display google text on page', async () =&amp;gt; {
    await expect(page).toMatch('google')
  })
})&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;You can now run this sample test:&lt;br&gt;
&lt;code&gt;$ jest&lt;/code&gt;&lt;br&gt;
Depending on whether you want to run this test locally or on a cloud platform, the test will start a headless browser and navigate to Google, finally verifying if the word google appears on the page.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;expect-playwright&lt;/strong&gt;&lt;br&gt;
There's also a NPM package available called &lt;code&gt;expect-playwright&lt;/code&gt;. This is a Jest utility matcher for Playwright. It exposes various matchers, making it easier and cleaner to write certain assertions in your test scripts.&lt;/p&gt;

&lt;p&gt;To get started, you can install the library:&lt;br&gt;
&lt;code&gt;npm install -D expect-playwright&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Now you can use various matchers:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;await expect(page).toHaveText("h1", "HeadlessTesting.com")&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;The various matchers include:&lt;/p&gt;

&lt;ul class="list"&gt;
&lt;li&gt;toHaveSelector&lt;/li&gt;
&lt;li&gt;toHaveText&lt;/li&gt;
&lt;li&gt;toEqualText&lt;/li&gt;
&lt;li&gt;toEqualValue&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Great Combo&lt;/strong&gt;&lt;br&gt;
Jest and Playwright is a great combination to do Headless testing. Both frameworks are under active development and keep getting better.&lt;/p&gt;

</description>
      <category>headless</category>
      <category>playwright</category>
      <category>jest</category>
    </item>
    <item>
      <title>Headless Testing with Puppeteer</title>
      <dc:creator>HeadlessTesting</dc:creator>
      <pubDate>Wed, 25 Mar 2020 15:28:35 +0000</pubDate>
      <link>https://forem.com/headlesstesting/headless-testing-with-puppeteer-bbo</link>
      <guid>https://forem.com/headlesstesting/headless-testing-with-puppeteer-bbo</guid>
      <description>&lt;p&gt;Puppeteer is a popular open-source library, built to control a headless instance of the Chrome and Chromium browser.&lt;/p&gt;

&lt;p&gt;Puppeteer is used to automate any action you can perform in the browser. You can use it to automate flows, take screenshots and generate PDFs.&lt;br&gt;
It is compatible with Chrome, Firefox and Microsoft Edge (Chromium).&lt;/p&gt;
&lt;h2&gt;
  
  
  Why should you use Puppeteer?
&lt;/h2&gt;

&lt;p&gt;There are multiple reasons why you should want to use Puppeteer.&lt;br&gt;
Some of the more popular reasons include:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Generating PDFs&lt;/li&gt;
&lt;li&gt;Taking screenshots&lt;/li&gt;
&lt;/ul&gt;
&lt;h3&gt;
  
  
  Generating PDFs
&lt;/h3&gt;

&lt;p&gt;Let's say you want to generate a PDF of a specific webpage, for example an invoice that your customer can download. Instead of setting up a third-party tool like wkhtmltopdf, you can choose Puppeteer.&lt;/p&gt;

&lt;p&gt;Generating PDFs through the Chromium browser will deliver a pixel-perfect PDF.&lt;br&gt;
Puppeteer provides &lt;a href="https://github.com/puppeteer/puppeteer/blob/v2.1.1/docs/api.md#pagepdfoptions"&gt;multiple different options&lt;/a&gt; to customise the PDF file, ranging from orientation and scale to margins and formats.&lt;/p&gt;
&lt;h3&gt;
  
  
  Taking screenshots
&lt;/h3&gt;

&lt;p&gt;Puppeteer makes it easy to take screenshots from any web page. There are some options to customise the screenshot, including quality, clipping and other formatting options.&lt;/p&gt;
&lt;h2&gt;
  
  
  Why should I use Puppeteer for Headless Testing?
&lt;/h2&gt;

&lt;p&gt;Yes, it's true that there are alternative solutions, such as Selenium. But we believe there's a couple of reasons why you should go for &lt;a href="https://headlesstesting.com"&gt;Headless Testing&lt;/a&gt; with Puppeteer:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Puppeteer uses the DevTools Protocol, this is a protocol built into Chrome/Chromium and optimised for both accuracy and performance.&lt;/li&gt;
&lt;li&gt;Headless is enabled by default. Contrary to Selenium, running tests in headless mode is a lot faster, because the browser does not have to do any visible rendering during the test.&lt;/li&gt;
&lt;li&gt;Selenium WebDriver uses the HTTP protocol to send and receive commands between the client and the node. Puppeteer uses a WebSocket tunnel, which 
means less latency and faster tests.&lt;/li&gt;
&lt;/ul&gt;
&lt;h2&gt;
  
  
  How to run tests with Puppeteer
&lt;/h2&gt;

&lt;p&gt;A popular package to run automated tests with Puppeteer is called Jest-Puppeteer.&lt;br&gt;
This open-source package allows you to use Jest as a test runner with Puppeteer.&lt;/p&gt;

&lt;p&gt;If you include a package called expect-puppeteer, you can write automated tests in a clean way, see this example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;expect-puppeteer&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;

&lt;span class="nx"&gt;describe&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Google&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nx"&gt;beforeAll&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;async&lt;/span&gt; &lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="nx"&gt;page&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;goto&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;https://google.com&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
  &lt;span class="p"&gt;})&lt;/span&gt;

  &lt;span class="nx"&gt;it&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;should display "google" text on page&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="k"&gt;async&lt;/span&gt; &lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="nx"&gt;expect&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;page&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="nx"&gt;toMatch&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;google&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
  &lt;span class="p"&gt;})&lt;/span&gt;
&lt;span class="p"&gt;})&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;This will launch a browser on your local machine and run the test in headless mode. You will receive a test status back from Jest.&lt;/p&gt;

&lt;h3&gt;
  
  
  Running Headless Tests in the Cloud
&lt;/h3&gt;

&lt;p&gt;The next step is to run all the tests you've created with a CI (Continuous Integration) system.&lt;/p&gt;

&lt;p&gt;You could use a cloud provider such as &lt;a href="https://headlesstesting.com"&gt;HeadlessTesting&lt;/a&gt; to have your Puppeteer scripts connect with the browsers in our grid. Then you can run multiple tests in parallel, drastically shortening the total test duration of your test suite.&lt;/p&gt;

</description>
      <category>puppeteer</category>
      <category>chrome</category>
      <category>jest</category>
      <category>testing</category>
    </item>
  </channel>
</rss>
