๐๐ก๐๐ญ ๐ข๐ฌ ๐ญ๐ก๐ ๐๐จ๐๐ฎ๐ฆ๐๐ง๐ญ ๐๐๐ฃ๐๐๐ญ ๐๐จ๐๐๐ฅ (๐๐๐)?
The Document Object Model (DOM) is a programmatic representation of a web page (HTML). It allows developers to modify and update web pages using JavaScript, making them interactive.
This is what enables users to:
- Click buttons
- Fill forms
- See dynamic content appear and disappear
โน๏ธ When a user interacts with a website, JavaScript updates the DOM behind the scenes to reflect those changes without refreshing the whole page.
๐๐ข๐ฆ๐ฉ๐ฅ๐ ๐๐ฑ๐๐ฆ๐ฉ๐ฅ๐:
When a user clicks "Add to Cart" on an e-commerce site, the cart icon updates and a message appears. That's JavaScript updating the DOM to show those changes instantly.
๐๐จ๐ฐ ๐ญ๐จ ๐๐ข๐๐ฐ ๐๐ง๐ ๐๐ง๐ฌ๐ฉ๐๐๐ญ ๐ญ๐ก๐ ๐๐๐
Opening the DOM Inspector
- Right-click anywhere on a webpage
- Select "Inspect"
- Look at the "Elements" tab content
๐๐ง๐๐๐ซ๐ฌ๐ญ๐๐ง๐๐ข๐ง๐ ๐ญ๐ก๐ ๐๐๐ ๐๐ญ๐ซ๐ฎ๐๐ญ๐ฎ๐ซ๐
You'll see all the HTML elements organized in a tree structure:
- Elements can be expanded or collapsed to view their contents
- Each element has parent, child, and sibling relationships
- The structure reflects the nesting of HTML tags
โน๏ธ When you right-click and inspect a specific element, the Elements tab automatically highlights that exact element in the tree.
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
๐ข ๐๐ก๐๐ญ ๐ข๐ฌ ๐ญ๐ก๐ ๐๐๐ ๐๐ง๐ ๐๐ก๐ฒ ๐๐ก๐จ๐ฎ๐ฅ๐ ๐๐ ๐๐ง๐ ๐ข๐ง๐๐๐ซ๐ฌ ๐๐๐ซ๐?
The Document Object Model (๐๐๐) is the bridge between a test automation code and the web pages you're testing.
Testing frameworks like Playwright rely on DOM manipulation to execute automated tests:
- Element Location: Frameworks use CSS selectors and XPath to find elements in the DOM
- Action Simulation: Clicks, text input, and navigation all happen through DOM interaction
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
๐ ๐๐ก๐ ๐๐ ๐๐ฎ๐ญ๐จ๐ฆ๐๐ญ๐ข๐จ๐ง ๐๐จ๐ซ๐ค๐๐ฅ๐จ๐ฐ:
๐๐ญ๐๐ฉ ๐: Identify Target Elements
Open Developer Tools (F12) and inspect the elements you need to interact with:
โฉ Find unique identifiers (IDs, classes, attributes)
โฉ Identify selectors that won't break easily (avoid dynamic IDs or complex nested paths)
๐๐ญ๐๐ฉ ๐: Write a Test Code
Translate findings into a test automation code.
For example, to click a login button:
</>โโโโโโโโโโโโโโโโโโโโโโโโ</>
๐ฉ๐๐ ๐.๐ฅ๐จ๐๐๐ญ๐จ๐ซ(โ๐๐ฎ๐ญ๐ญ๐จ๐ง#๐ฅ๐จ๐ ๐ข๐งโ).๐๐ฅ๐ข๐๐ค()
</>โโโโโโโโโโโโโโโโโโโโโโโโ</>
This tells the framework: "Find the button element with ID 'login' in the DOM and click it."
๐๐ญ๐๐ฉ ๐: Framework Execution
Behind the scenes, the testing framework:
โฉ Uses JavaScript to interact with the DOM elements for the selector
โฉ Waits for the element to be ready (visible, enabled, etc.)
โฉ Performs the requested action by triggering DOM events
๐๐ฑ๐๐ฆ๐ฉ๐ฅ๐: ๐๐จ๐ ๐ข๐ง ๐
๐ฅ๐จ๐ฐ
The DOM looks like this:
</>โโโโโโโโโโโโโโโโโโโโโโโโ</>
<๐๐จ๐ซ๐ฆ ๐๐ฅ๐๐ฌ๐ฌ=โ๐ฅ๐จ๐ ๐ข๐ง-๐๐จ๐ซ๐ฆโ>
โ โ <๐ข๐ง๐ฉ๐ฎ๐ญ ๐ข๐=โ๐ฎ๐ฌ๐๐ซ๐ง๐๐ฆ๐โ ๐ญ๐ฒ๐ฉ๐=โ๐ญ๐๐ฑ๐ญโ />
โ โ <๐ข๐ง๐ฉ๐ฎ๐ญ ๐ข๐=โ๐ฉ๐๐ฌ๐ฌ๐ฐ๐จ๐ซ๐โ ๐ญ๐ฒ๐ฉ๐=โ๐ฉ๐๐ฌ๐ฌ๐ฐ๐จ๐ซ๐โ />
โ โ <๐๐ฎ๐ญ๐ญ๐จ๐ง ๐ข๐=โ๐ฅ๐จ๐ ๐ข๐ง-๐๐ญ๐งโ>๐๐จ๐ ๐ข๐ง</๐๐ฎ๐ญ๐ญ๐จ๐ง>
</๐๐จ๐ซ๐ฆ>
</>โโโโโโโโโโโโโโโโโโโโโโโโ</>
Python Playwright Test Code for those elements would be:
</>โโโโโโโโโโโโโโโโโโโโโโโโ</>
๐ฉ๐๐ ๐.๐ฅ๐จ๐๐๐ญ๐จ๐ซ(โ#๐ฎ๐ฌ๐๐ซ๐ง๐๐ฆ๐โ).๐๐ข๐ฅ๐ฅ(โ๐ญ๐๐ฌ๐ญ๐ฎ๐ฌ๐๐ซ@๐๐ฑ๐๐ฆ๐ฉ๐ฅ๐.๐๐จ๐ฆโ)
๐ฉ๐๐ ๐.๐ฅ๐จ๐๐๐ญ๐จ๐ซ(โ#๐ฉ๐๐ฌ๐ฌ๐ฐ๐จ๐ซ๐โ).๐๐ข๐ฅ๐ฅ(โ๐๐๐๐ฎ๐ซ๐๐๐๐ฌ๐ฌ๐๐๐โ)
๐ฉ๐๐ ๐.๐ฅ๐จ๐๐๐ญ๐จ๐ซ(โ#๐ฅ๐จ๐ ๐ข๐ง-๐๐ญ๐งโ).๐๐ฅ๐ข๐๐ค()
</>โโโโโโโโโโโโโโโโโโโโโโโโ</>
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
๐ฃ ๐๐ซ๐จ ๐๐ข๐ฉ ๐๐จ๐ซ ๐๐ ๐๐ฎ๐ญ๐จ๐ฆ๐๐ญ๐ข๐จ๐ง ๐๐ง๐ ๐ข๐ง๐๐๐ซ๐ฌ
Before writing test code, verify a selector works in the Browser Console (Developer Tools).
Open the Console and type:
</>โโโโโโโโโโโโโโโโโโโโโโโโ</>
๐๐จ๐๐ฎ๐ฆ๐๐ง๐ญ.๐ช๐ฎ๐๐ซ๐ฒ๐๐๐ฅ๐๐๐ญ๐จ๐ซ(โ๐๐ฎ๐ญ๐ญ๐จ๐ง#๐ฅ๐จ๐ ๐ข๐ง-๐๐ญ๐งโ)
</>โโโโโโโโโโโโโโโโโโโโโโโโ</>
If it finds the element, the selector is good to be used in the test.
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
๐๐๐ค๐๐๐ฐ๐๐ฒ:
โ
The DOM is a programmatic representation of HTML
โ
Testing frameworks use the DOM to find and interact with elements
โ
"Elements" tab in Developer Tools allows to inspect and understand DOM structure
๐.๐. ๐ฉ ๐๐ ๐ฒ๐จ๐ฎ ๐ก๐๐ฏ๐๐งโ๐ญ ๐ฐ๐๐ญ๐๐ก๐๐ ๐ข๐ญ ๐ฒ๐๐ญ, ๐ฒ๐จ๐ฎ๐ซ ๐ง๐๐ฑ๐ญ ๐ฌ๐ญ๐๐ฉ ๐ข๐ฌ ๐ญ๐ก๐ ๐
๐๐๐ ๐-๐ฉ๐๐ซ๐ญ โ๐๐๐ง๐ฎ๐๐ฅ ๐๐ โ ๐๐๐๐โ ๐ฐ๐จ๐ซ๐ค๐ฌ๐ก๐จ๐ฉ,ย ๐ ๐ฌ๐ก๐จ๐ซ๐ญ ๐ฆ๐ข๐ง๐ข-๐๐จ๐ฎ๐ซ๐ฌ๐ ๐ญ๐ก๐๐ญ ๐ ๐ข๐ฏ๐๐ฌ ๐ฒ๐จ๐ฎ ๐ญ๐ก๐ ๐๐ฎ๐ฅ๐ฅ ๐ซ๐จ๐๐๐ฆ๐๐ฉ ๐ญ๐จ ๐๐๐๐จ๐ฆ๐ข๐ง๐ ๐ ๐ฆ๐ข๐-๐ฅ๐๐ฏ๐๐ฅ ๐๐๐๐ ๐๐ง๐ ๐ฉ๐๐ฌ๐ฌ๐ข๐ง๐ ๐ข๐ง๐ญ๐๐ซ๐ฏ๐ข๐๐ฐ๐ฌ.