Showing posts with label selenium. Show all posts
Showing posts with label selenium. Show all posts

Sunday, 19 February 2023

Tips to improve Selenium Code


Intro to Selenium

Selenium is an open-source testing framework used for automating web browsers. It allows testers to write scripts in various programming languages such as Java, Python, Ruby, C#, and JavaScript, to automate the testing of web applications. Selenium provides a suite of tools that can be used together or individually, depending on the requirements of the testing process.

The most popular tool in the Selenium suite is Selenium WebDriver, which is used for automating web browsers like Chrome, Firefox, Safari, and Edge. Selenium WebDriver allows testers to interact with web elements, simulate user actions such as clicking, typing, selecting, and submitting forms, and also perform complex actions like drag and drop, and scrolling.

Other tools in the Selenium suite include Selenium IDE (Integrated Development Environment), which is a record and playback tool used for creating automated tests, and Selenium Grid, which allows testers to run their tests in parallel on multiple machines and browsers.

Overall, Selenium is a popular and widely-used tool for web application testing and is highly valued by software testers and developers for its ease of use, versatility, and flexibility. 

Tips to improve Selenium Code

  • Use waits: Selenium provides different types of waits (explicit, implicit, and fluent waits) to synchronize your test script with the web page. This will help your script to wait for the element to load before performing any actions on it. More can be read here
  • Use CSS selectors: CSS selectors are faster than XPath, and they are easier to write and read. You can use browser dev tools to find CSS selectors of the elements you want to interact with. Please read here for more info on CSS Selectors.
  • Use Headless mode: Running Selenium in headless mode means that the browser will run in the background without opening any GUI. This will make your test scripts run faster and consume fewer resources. Read more here about headless mode
  • Use Page Object Model: Page Object Model (POM) is a design pattern that helps to separate the test code from the page-specific code. It makes your code more readable and maintainable. Read more here
  • Use TestNG or JUnit: TestNG and JUnit are popular testing frameworks that provide several features such as assertions, reporting, and parallel execution. Using a testing framework will make your test scripts more robust. More about TestNG and JUnit can be read here and here
  • Use JavaScriptExecutor: Sometimes, Selenium cannot perform some actions that are achievable using JavaScript. In such cases, you can use the JavaScriptExecutor interface to execute JavaScript code in your Selenium script. Read more here
Remember, it is important to write clean and maintainable code. Selenium is a powerful tool, and it requires a good understanding of the web technologies and programming concepts.

ShadowDOM and its handling with Selenium



ShadowDOM

Shadow DOM is a technique used in web development to encapsulate the styling and behavior of a component within its own private DOM tree, separate from the main document DOM tree. It allows developers to create custom elements that can be reused across different web pages or applications, without worrying about the element's styling or behavior interfering with other elements on the page.

When a web component is created using Shadow DOM, the component's internal structure and styling are hidden from the rest of the page. The component can still interact with the rest of the page, but the rest of the page cannot interact with the component's internal structure or styling. This helps to prevent styling conflicts and unintended side effects, and allows components to be easily reused without modification.

The Shadow DOM tree is structured similarly to the regular DOM tree, with elements and nodes arranged in a hierarchical fashion. The main difference is that the Shadow DOM tree is encapsulated within a "shadow host" element in the regular DOM tree, and is not directly accessible or modifiable from outside of the shadow host.

Shadow DOM is widely used in modern web development, and is supported by most modern web browsers, including Chrome, Firefox, and Safari. It is commonly used in conjunction with other web technologies, such as Custom Elements and Web Components, to create reusable UI components and widgets.

Here is an example diagram of a Shadow DOM tree:

+-----------------------------------------------+ | Regular DOM tree | | | | +----------------------+ | | | <html> | | | | | | | | | <body> | | | | | | | | | <my-shadow-host> | | | | (Shadow DOM) | | | | | | | | | #shadow-root | | | | | | | | | <my-shadow-element> | | | | | | | | | </my-shadow-element>| | | | | | | | | <my-other-element> | | | | | | | | | </my-other-element> | | | | | | | | | </my-shadow-host> | | | | | | | | | <regular-element> | | | | | | | | | </regular-element> | | | | | | | | | </body> | | | +----------------------+ | | | +-----------------------------------------------+


In this above example, we have a regular DOM tree that contains a <my-shadow-host> element, which is a web component that uses Shadow DOM to encapsulate its internal structure and styling. The Shadow DOM tree is contained within the #shadow-root element of the Shadow host, and contains two child elements: <my-shadow-element> and <my-other-element>. These elements are encapsulated within the Shadow DOM tree and cannot be accessed directly from the regular DOM tree.

Outside of the Shadow host, we also have a regular DOM element called <regular-element>, which is not part of the Shadow DOM tree and can be accessed directly using standard DOM manipulation techniques.

Handling ShadowDOM with Selenium

In Selenium with Java, you can interact with Shadow DOM elements on a webpage using the WebDriver API. However, since Shadow DOM elements are encapsulated within their own DOM tree, they cannot be accessed directly using the standard WebDriver methods such as findElement or findElements. To interact with Shadow DOM elements in Selenium with Java, you will need to use the executeScript method of the JavascriptExecutor interface to execute JavaScript code that can access the Shadow DOM tree and find the desired elements. Here is an example of how to access a Shadow DOM element using Selenium with Java:

// Find the Shadow host element WebElement shadowHost = driver.findElement(By.cssSelector("my-shadow-host")); // Execute JavaScript to access the Shadow DOM tree WebElement shadowRoot = (WebElement) ((JavascriptExecutor)driver).executeScript("return arguments[0].shadowRoot", shadowHost); // Find the element within the Shadow DOM tree WebElement shadowElement = shadowRoot.findElement(By.cssSelector("my-shadow-element")); // Interact with the Shadow DOM element shadowElement.click();

In this example, my-shadow-host and my-shadow-element are CSS selectors that identify the Shadow host and Shadow element, respectively. The executeScript method is used to execute JavaScript that accesses the Shadow DOM tree and returns the desired element. Once the Shadow element is located, you can interact with it using standard WebDriver methods.



Tips to improve Selenium Code

Intro to Selenium Selenium is an open-source testing framework used for automating web browsers. It allows testers to write scripts in vario...