<?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 Rajat on Medium]]></title>
        <description><![CDATA[Stories by Rajat on Medium]]></description>
        <link>https://medium.com/@rajat_bhardwaj?source=rss-af571ccbd0b2------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/0*9lKUftzYaTiLHjW7</url>
            <title>Stories by Rajat on Medium</title>
            <link>https://medium.com/@rajat_bhardwaj?source=rss-af571ccbd0b2------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Sun, 06 Dec 2026 15:33:22 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@rajat_bhardwaj/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[Playwright’s Playbook: Conquering ShadowDOM Elements with Ease]]></title>
            <link>https://medium.com/helpshift-engineering/playwrights-playbook-conquering-shadowdom-elements-with-ease-35b65bfb8008?source=rss-af571ccbd0b2------2</link>
            <guid isPermaLink="false">https://medium.com/p/35b65bfb8008</guid>
            <category><![CDATA[playwright-automation]]></category>
            <category><![CDATA[playwright-test]]></category>
            <category><![CDATA[shadow-dom]]></category>
            <category><![CDATA[web-automation]]></category>
            <dc:creator><![CDATA[Rajat]]></dc:creator>
            <pubDate>Tue, 20 Jun 2023 05:29:56 GMT</pubDate>
            <atom:updated>2023-06-20T09:21:26.763Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*cbthKW3xT4S0ymaz8usTPw.png" /></figure><p>In the ever-evolving world of web development, ensuring that your website or application works seamlessly across different browsers and platforms is crucial. In this fast-paced digital landscape, ShadowDOM has emerged as a powerful tool for encapsulating and isolating components, providing developers with enhanced control over their web applications.</p><p>However, when it comes to automating interactions within the ShadowDOM, it can often be a daunting task. So let’s explore the significance of ShadowDOM, understand its unique challenges, and dive deep into the capabilities of Playwright that make automating ShadowDOM a breeze.</p><h3>What is a Shadow DOM?</h3><p>Before we delve into the automation aspect, let’s grasp some fundamentals. Shadow DOM is one of the implementations of the OOPS principle of Encapsulation in an HTML document or DOM. It is a web standard that allows encapsulation of HTML, CSS, and JavaScript code within a specific element on a webpage, creating an isolated and scoped subtree of the DOM.</p><p>In traditional web development, CSS styles and JavaScript code can easily affect and modify elements throughout the entire document, leading to potential conflicts and unintended consequences. Shadow DOM addresses this issue by providing a way to create a boundary around a specific element and its descendants, isolating them from the rest of the document.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*Oljbuw491hr3fcfmGJohMQ.png" /></figure><p>By utilizing Shadow DOM, web developers can create reusable components that encapsulate their own HTML structure, CSS styles, and JavaScript behavior, without worrying about conflicting with or being affected by other parts of the webpage.</p><h3>But, How does ShadowDOM impact Automation Testing?</h3><p>While ShadowDOM brings many benefits to web development, it presents unique challenges when it comes to automation. Traditional automation frameworks often struggle to interact with elements residing within ShadowDOM boundaries. Locating, inspecting, and manipulating these encapsulated elements requires a different approach.</p><p>Here are a few key points to consider while interacting with and validating the contents of shadow DOM elements:</p><p><strong>1. Element Identification:</strong> Shadow DOM encapsulates its elements, making them hidden from the main DOM tree. Automation frameworks need to have specific methods or techniques to locate and interact with shadow DOM elements. This may involve using specialized selectors or functions that can traverse the shadow DOM boundary.</p><p><strong>2. Accessing Shadow DOM:</strong> Automation tools may provide specific APIs or methods to access the shadow DOM content. For example, Playwright offers the `shadowRoot()` method, which allows you to obtain the root element of the shadow DOM subtree. Once you have access to the shadow root, you can interact with its elements using regular DOM manipulation methods.</p><p><strong>3. Handling Encapsulation:</strong> Shadow DOM’s encapsulation ensures that styles and behavior within a shadow DOM subtree do not affect or get affected by external styles or scripts. When automating tests, it’s important to consider the encapsulation boundaries and ensure that interactions with shadow DOM elements are performed within their intended context.</p><p><strong>4. Validation and Assertions:</strong> Verification and assertion of shadow DOM elements may require specific techniques. For example, you might need to validate the contents of shadow DOM elements by accessing their properties or attributes through specific methods provided by the automation framework.</p><p>In short, automation testing with Shadow DOM requires understanding the unique characteristics of shadow DOM elements and utilizing the features and APIs provided by automation frameworks to interact with and validate them accurately.</p><h3>Now, how do we solve this?</h3><p>Enter <a href="https://playwright.dev/java/">Playwright</a>, the versatile automation library, which empowers testers to effortlessly navigate and manipulate the ShadowDOM with ease. Playwright is able to access elements in the open shadow DOM. Playwright’s css and text locators pierce shadow DOM by default, which mean, you do not need explicit JS code to access the shadow DOM, which is in the case of Selenium. One thing to note here is that Xpaths do not pierce shadow DOM.</p><p>Another important thing to remember is that both Playwright and Selenium cannot access the elements inside the <strong>closed shadow DOM</strong>.</p><p>Let’s delve deeper into the common scenarios which deal with interacting with the elements inside a ShadowDOM. <br>We’ll be using <a href="https://selectorshub.com/xpath-practice-page/">SelectorsHub Practice page</a> as the AUT since it has all of the complex scenarios we are looking for.</p><h3>Scenario 1 : Element inside an open Shadow Root</h3><p>This is a very common scenario where the element is directly under a ShadowDOM on a page. Let’s consider the input box with the label UserName and placeholder text enter name on the SelectorsHub practise page, as shown in the image below.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*0ZJJ5kOnhIhHGEmBGtjA8A.png" /><figcaption>Page — DOM → ShadowDOM → elements</figcaption></figure><p>As we know Playwright css engine pierces the shadowDOM by default, so to input value to the placeholder:</p><ul><li>We will write css selector of the div tag for where the parent shadow root of the input tag starts (shown in the image above), which is: #userName.</li><li>The css selector of the parent shadow root will be followed by css selector of its child input field, which is: #kils.</li></ul><p>So the resulting code to enter text to the input field would be:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*PSrxXihfQpQ4KQWt5-yzNQ.png" /></figure><p>After running the code, we see that the code runs successfully and the element receives the text input.</p><h3>Scenario 2: Element inside multiple open Shadow Roots (Nested Shadow Roots)</h3><p>We can also encounter case where there are nested shadow roots — which means, one shadow root is inside of another shadow root. In that case too, Playwright can easily pierce the inner shadow DOM and get to the elements.</p><p>Let’s consider the input box with the label UserName and placeholder text Enter pizza name on the SelectorsHub practise page, as shown in the image below.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*LHXbUBmXKrc7rQq3-NtRRA.png" /><figcaption>Page — DOM → ShadowDOM → ShadowDOM → elements</figcaption></figure><p>As we know Playwright css engine easily pierce through the shadowDOM, we’ll follow the same approach as above:</p><ul><li>We will write css selector of the div tag for where the parent shadow root of the input tag starts (shown in the image above), which is: #userName.</li><li>It will be followed by css selector of the child input field which is inside another shadow root, which is: #pizza.</li></ul><p>So the resulting code to enter text to the input field would be:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*_-Z76JJY9Kd6KFRSOOklHQ.png" /></figure><p>We see that the code runs like a charm and the element receives the text input.</p><h3>Scenario 3: Element inside an iFrame inside an open Shadow Root</h3><p>While working with the enterprise products, we encounter many complex scenarios where many automation frameworks fail to work.</p><p>Another such scenario is where the element is inside an iframe, which is further embedded inside an open shadow root. Let’s consider the input box with the label Snacks and placeholder text Do you love tea on the SelectorsHub <a href="https://selectorshub.com/shadow-dom-in-iframe/">practise page for shadowDOM in iFrames</a>, as shown in the image below.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*tM6Qc7qqlhwybnMcPrXjQw.png" /><figcaption>Page — DOM → iFrame → ShadowDOM → elements</figcaption></figure><p>How to handle this scenario, Let’s find out!!</p><p>If we try to access elements inside an iframe → inside a shadow root, just like we did in the previous scenario, the code will throw an error. By default, Playwright css engine will pierce the shadow root DOM but will not search elements inside iframe or closed root.</p><p>To get the element inside the iframe:</p><ul><li>We will first have to go to the iframe and then search element inside the iframe. So, we’ll use the framelocator method to get to the iframe with id pact.</li><li>Then using the css selector of the parent shadowRoot (#snacktime ), traverse to its child input field (#tea) and then fill it with required text – and voila, the code works !!</li></ul><p>The resulting code would be:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*3aWZy93FYvJZnn8f4fts0A.png" /></figure><h3>Scenario 4: Element inside a closed Shadow Root</h3><p>Until now, we have covered the scenarios where Playwright can directly or indirectly pierce the shadow root, since it is an open state shadow DOM.</p><p>Now, if the element is inside a closed shadow DOM, playwright css engine will not be able to search it. Let’s consider the input box with the label Password and placeholder text enter passwordon the SelectorsHub practise page, as shown in the image below.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*I6c_RpYi56HiX8L6Eg1JJw.png" /><figcaption>Page — DOM → ShadowDOM (closed) → element</figcaption></figure><p>Following the approach as in Scenario-1, the resulting code would be:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*tA1i10kghbm_O1nXzuq6xA.png" /></figure><p>Since Playwright engine cannot access the element, page.locator methods are of no use here. The required element would not be found and the code will throw an error.</p><p>Hence, we cannot automate scenarios related to closed shadow root using Playwright.</p><h3>Conclusion</h3><p>As web development continues to evolve, the importance of robust and efficient web automation becomes increasingly apparent. Now that we have covered the complex scenarios w.r.t Shadow DOM and their handling using Playwright, I hope you’re feeling empowered and ready to tackle more such complexities in automation.</p><p>By leveraging Playwright’s rich features and capabilities, developers and QA engineers can confidently tackle the challenges posed by modern web applications.</p><p>So, go ahead, embrace the power of Playwright, and automate the unautomatable!</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=35b65bfb8008" width="1" height="1" alt=""><hr><p><a href="https://medium.com/helpshift-engineering/playwrights-playbook-conquering-shadowdom-elements-with-ease-35b65bfb8008">Playwright’s Playbook: Conquering ShadowDOM Elements with Ease</a> was originally published in <a href="https://medium.com/helpshift-engineering">helpshift-engineering</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Reliable Visual Validation using Playwright]]></title>
            <link>https://medium.com/helpshift-engineering/reliable-visual-validation-using-playwright-8a53f772f367?source=rss-af571ccbd0b2------2</link>
            <guid isPermaLink="false">https://medium.com/p/8a53f772f367</guid>
            <category><![CDATA[visual-testing]]></category>
            <category><![CDATA[playwright-community]]></category>
            <category><![CDATA[js]]></category>
            <category><![CDATA[automation-testing]]></category>
            <dc:creator><![CDATA[Rajat]]></dc:creator>
            <pubDate>Mon, 16 May 2022 06:58:17 GMT</pubDate>
            <atom:updated>2022-05-16T06:58:17.732Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*zedH656LW6lDP2TkjKEB6w.png" /><figcaption>What you see is what you get</figcaption></figure><p>Today, web development has become very fast. Developers are shipping complex and UI heavy features at a very fast pace. Web apps are becoming increasingly sophisticated and run across wide range of devices — phones, tablets, desktops. This increase in velocity and target puts immense pressure on the testing process to make sure no bug gets missed.</p><p>While writing automation scenarios, a tester needs to be very careful to include all the validations and assertions to ensure utmost quality of the application under test. It is essential to ensure that no UI component on the web page is missed and there is a better and effective way to ensure this — Visual Validation.</p><h3><strong>What is Visual Validation?</strong></h3><p>Visual validation/testing, in simplest terms, is verifying that everything appears correct, or as intended.</p><p>“Appear” is an important thing to note here, because visual testing is all about verifying what the user sees. If we wanted to expand the definition for more clarity, it is:</p><ul><li><strong>Making sure that everything that we want on a page of a website is actually there.</strong></li><li><strong>Making sure that everything we don’t want to be there is not.</strong></li><li><strong>Making sure that the visible output of the website is as intended by the businesses and for the user.</strong></li></ul><p>In Web testing, visual testing is the process of detecting and reviewing visual UI changes of the web page. It is a quality assurance activity that specifically focuses on ensuring that web page’s UI appears as intended for the end user.</p><p>Visual Validation can be incredibly powerful: event the slightest of the UI changes can verify easily, ensuring quality — at least in theory. In practice, visual validation can be slow, flaky and hard to maintain.</p><p>Today we are talk about Visual Validation using <a href="https://github.com/microsoft/playwright">Playwright</a> — a powerful framework for efficient visual validation testing. Playwright is a <strong>cross-browser automation </strong>library to write tests that are <strong>fast, reliable and capable</strong>.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*hHKkWQ_ajLFMk-coSLI6wg.png" /></figure><h3>Getting started with Playwright</h3><p>Playwright Test was created specifically to accommodate the needs of the end-to-end testing. It does everything we would expect from the regular test runner, and more. Playwright test allows to:</p><ul><li>Run tests across all browsers.</li><li>Execute tests in parallel.</li><li>Enjoy context isolation out of the box.</li><li>Capture videos, screenshots and other artifacts on failure.</li><li>Integrate your POMs as extensible fixtures.</li></ul><h3><strong>Installation</strong></h3><h4>* Using init command</h4><p>The easiest way to get started with Playwright Test is to run the init command.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*gNrQgPihYLd2pZ8_F0CfnQ.png" /></figure><p>This will create a configuration file, optionally add examples, a GitHub Action workflow and a first test example.spec.ts</p><h4>* Manually<a href="https://playwright.dev/docs/1.20/intro#manually">​</a></h4><p>Add dependency and install browsers.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*0kYFDlP1BoNT1bkrwqmq4w.png" /></figure><p>We can optionally install only selected browsers, see <a href="https://playwright.dev/docs/1.20/cli#install-browsers">install browsers</a> for more details.</p><h3>Visual Comparison</h3><p>Playwright framework provides the ability to produce and compare UI elements visually. Playwright Test makes use of <a href="https://github.com/mapbox/pixelmatch">pixelmatch</a> library for pixel-by-pixel matching.</p><p>When test is run for the first time, it will take a screenshot of the current page and save it as a baseline screenshot (or golden image). Subsequent tests will take new screenshots and compare these with the baseline.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*ywzmfldFGuFfUyiemuPOgQ.png" /></figure><p>When we run above for the first time, test will fail with following error:</p><pre>Error: example.spec.ts-snapshots/example-test-1-chromium-darwin.png is missing in snapshots, writing actual.</pre><p>That’s because there was no golden image yet. This method will take a bunch of screenshots and save them in the project repository. It is now ready to perform the visual comparison.</p><p>When test run command is executed again, as follows:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*wrN2saKTxCxoFVrhEcYECg.png" /></figure><p>Playwright will use the page object and call the screenshot method to save a screenshot. It will then use the pixelmatch library to compare both of these screenshots for visual differences.</p><p>Sometimes, we might need to update the golden images, for example when the page has changed. We can do this with the --update-snapshots flag.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*WnO1m8gDzx0dWcw8MDk7tw.png" /></figure><p>We can <a href="https://playwright.dev/docs/test-assertions#screenshot-assertions-to-match-snapshot">pass various options</a> to finetune the pixel matching in our screenshots. If we want to share the default value among all the tests in the project, we can specify it in the playwright config, either globally or per project:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*hEDYCQ59q6JvQPc_QV_GEQ.png" /></figure><h3>Comparing binary or text formats</h3><p>Apart from screenshots, we can use expect(value).toMatchSnapshot(snapshotName) to compare binary and text formats payloads. Playwright Test auto-detects the content type and uses the appropriate comparison algorithm. This allows us to compare DOM elements for identical text content. For example, we can write tests where we match the text contents of a DOM element with a pre-defined text which we know will not change.</p><p>An example of such a test case is located below:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*h7RR8vVHt6Txu-iQhPrc-Q.png" /></figure><p>Here we take the text from the .login_title DOM element and compare it to the text inside the login.txt file.</p><h3>Conclusion</h3><p>The web platform is more capable than ever before and is continuously evolving. Hence, Visual regression testing is becoming a popular trend among QA and developer teams. Playwright offers its own, built-in visual regression testing solution through Playwright Test.</p><p>With Playwright’s visual comparison testing, we can perform visual validations in a fast, reliable and efficient manner.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=8a53f772f367" width="1" height="1" alt=""><hr><p><a href="https://medium.com/helpshift-engineering/reliable-visual-validation-using-playwright-8a53f772f367">Reliable Visual Validation using Playwright</a> was originally published in <a href="https://medium.com/helpshift-engineering">helpshift-engineering</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
    </channel>
</rss>