<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Serhii Bryt on Medium]]></title>
        <description><![CDATA[Stories by Serhii Bryt on Medium]]></description>
        <link>https://medium.com/@sergeybrit?source=rss-9caef9e909e0------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/0*1T-ikkFbFCaBDrZt.</url>
            <title>Stories by Serhii Bryt on Medium</title>
            <link>https://medium.com/@sergeybrit?source=rss-9caef9e909e0------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Tue, 17 Nov 2026 06:31:19 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@sergeybrit/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[Selenium: How to make screenshots]]></title>
            <link>https://medium.com/@sergeybrit/selenium-how-to-make-screenshots-7798d6bef2ef?source=rss-9caef9e909e0------2</link>
            <guid isPermaLink="false">https://medium.com/p/7798d6bef2ef</guid>
            <dc:creator><![CDATA[Serhii Bryt]]></dc:creator>
            <pubDate>Thu, 11 Apr 2024 16:17:02 GMT</pubDate>
            <atom:updated>2024-04-11T16:17:02.148Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/889/1*-yauD1PxgsdiOJQMqnmu1w.jpeg" /></figure><p>Hello all. <br>I’m Serhii — test automation engineer with more then 13 years of experience in test automation area.<br>In this article I’ll share with you how to make screenshots within Selenium in different ways.</p><h3>Viewport screenshots.</h3><p>For <strong>Chrome</strong> and <strong>Firefox</strong> browsers you just need to cast <strong>Webdriver</strong> instance to <strong>TakesScreenshot</strong> interface.</p><pre>((TakesScreenshot) driver).getScreenshotAs(OutputType.BYTES)</pre><p>Depends on <strong>OutputType</strong> it will return <strong>byte[]</strong>, <strong>File</strong> instance or <strong>Base64</strong> encoded String. After that you just need to save this object to file</p><pre>FileUtils.writeByteArrayToFile(new File(&quot;files/chromeScreenShotTest.png&quot;), screenshotAs);</pre><h3>Document (Full page) screenshot with Firefox driver and CDP API</h3><p><strong>Firefox driver</strong> has such method in its API. It will look like this</p><pre>((FirefoxDriver) driver).getFullPageScreenshotAs(OutputType.BYTES)</pre><p>This will return value depends on <strong>OutputType</strong> parameter. And you can easily save it to file.</p><p>Unfortunately <strong>Chrome driver </strong>doesn’t have such method, but we can use <strong>DevTools(CDP) API</strong> for this.</p><p>We should go through several steps:<br>1. Initialize driver</p><pre>WebDriver driver = new ChromeDriver();</pre><p>2. Initialize <strong>DevTools </strong>and start DevTools session</p><pre>DevTools devTools = ((HasDevTools) driver).getDevTools();<br>devTools.createSession();</pre><p>3. Make screenshot of the full document</p><pre>String screenshot = devTools.send(Page.captureScreenshot(<br>        Optional.of(Page.CaptureScreenshotFormat.PNG),<br>        Optional.empty(),<br>        Optional.empty(),<br>        Optional.empty(),<br>        Optional.of(true),<br>        Optional.empty()<br>));</pre><p>In this case variable <strong>screenshot </strong>contains Base64 encoded string. <br>After you should just decode this string and save file.</p><pre>byte[] decoded = Base64.getDecoder().decode(screenshot.getBytes());<br>FileUtils.writeByteArrayToFile(new File(&quot;files/chromeFullScreenShotTest.png&quot;), decoded);</pre><h3>Screenshot (full or viewport) with BiDi API</h3><p>For Chrome and Firefox drivers the steps will be the same because BiDi API is the universal API which should be supported for all browsers.</p><p><strong>Viewport screenshot</strong></p><p>You should do several simple steps:<br>1. Initialize Webdriver (e.g. Firefox)</p><pre>FirefoxOptions firefoxOptions = new FirefoxOptions();<br>firefoxOptions.setCapability(&quot;webSocketUrl&quot;, true);<br><br>driver = new FirefoxDriver(firefoxOptions);</pre><p>Capability <strong>webSocketUrl </strong>must be initialized. This will switch on BiDi protocol.</p><p>2. Navigate somewhere</p><p>3. Initialize <strong>BrowsingContext</strong> and make screenshot</p><pre> BrowsingContext browsingContext = new BrowsingContext(driver, driver.getWindowHandle());<br> String screenshot = browsingContext.captureScreenshot();</pre><p>This piece of code will return Base64 encoded string. After that you can save it.</p><pre>FileUtils.writeByteArrayToFile(new File(&quot;files/firefoxScreenShotWithBiDi.png&quot;), <br>                                        Base64.getDecoder().decode(screenshot.getBytes()));</pre><p><strong>Full page screenshot</strong></p><p>First 2 steps are the same as in previous case.</p><p>3. We should initialize <strong>BrowsingContext </strong>and make screenshot using <strong>CaptureScreenshotParameters </strong>instance as parameter.</p><pre>BrowsingContext browsingContext = new BrowsingContext(driver, driver.getWindowHandle());<br> <br>CaptureScreenshotParameters captureScreenshotParameters = new CaptureScreenshotParameters();<br>captureScreenshotParameters.imageFormat(&quot;image/jpeg&quot;);<br>captureScreenshotParameters.origin(CaptureScreenshotParameters.Origin.DOCUMENT);<br><br>String screenshot = browsingContext.captureScreenshot(captureScreenshotParameters);</pre><p>This piece of code will make full page screenshot in JPEG format and return Base64 encoded string.<br>After that you can save it.</p><pre>FileUtils.writeByteArrayToFile(new File(&quot;files/firefoxFullScreenShotWithBiDi.jpeg&quot;), Base64.getDecoder().decode(screenshot.getBytes()));</pre><h3>Conclusion</h3><p>Let’s summarize.</p><p>Now we have many ways how to make a screenshot with Selenium. It’s up to you what way to choose.</p><p>Happy testing!</p><h3>P.S.</h3><p>Useful links:<br>1. <a href="https://www.selenium.dev/">Selenium</a><br>2. <a href="https://www.selenium.dev/documentation/webdriver/bidirectional/webdriver_bidi/browsing_context">BrowsingContext official docs</a><br>3. <a href="https://www.selenium.dev/documentation/webdriver/bidirectional/chrome_devtools/">CDP officisl docs</a> <br>4. <a href="https://chromedevtools.github.io/devtools-protocol/">CDP specification</a> <br>5. <a href="https://chromedevtools.github.io/devtools-protocol/tot/Page/#method-6.%20captureScreenshot">CDP captureScreenshot specification</a> <br>6. <a href="https://w3c.github.io/webdriver-bidi/">BiDi specification</a><br>7. <a href="https://w3c.github.io/webdriver-bidi/#command-browsingContext-captureScreenshot">BrowsingContext captureScreenshot specification</a><br>8. <a href="https://github.com/britka/Selenium-Screenshots-Example">Examples for this article</a></p><p><a href="https://www.linkedin.com/in/serhii-bryt/">My Linkedin</a><br><a href="https://github.com/britka/playwrightium">Playwrightium Project</a></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=7798d6bef2ef" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>