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.



JUnit 5 vs TestNG




JUnit 5 and TestNG are two of the most popular testing frameworks for Java applications, so let's compare them based on some key features:


Feature

JUnit 5

TestNG

Annotations

JUnit 5 provides a comprehensive set of annotations for test lifecycle, assertions, and assumptions

TestNG supports both JUnit 3 and JUnit 4 annotations, as well as its own annotations for test configuration, test groups, and more

Assertions

JUnit 5 provides a built-in assertion library

TestNG also provides a built-in assertion library, but it supports custom assertions as well

Test Execution

JUnit 5 allows tests to be executed in parallel with the @Execution(ExecutionMode.CONCURRENT) annotation

TestNG supports parallel test execution out-of-the-box

Test Configuration

JUnit 5 allows test configuration through annotations, properties, and YAML files

TestNG allows test configuration through annotations, XML files, and command-line parameters

Dependency Injection

JUnit 5 includes support for dependency injection with the @ExtendWith annotation

TestNG also supports dependency injection with the @Inject and @Guice annotations

Parameterized Tests

JUnit 5 allows for parameterized tests with the @ParameterizedTest annotation

TestNG also supports parameterized tests with its built-in data providers, as well as custom data providers

Reporting

JUnit 5 generates simple, text-based reports by default, but custom reports can be generated with additional frameworks

TestNG generates detailed HTML reports with customizable output

Compatibility

JUnit 5 is compatible with Java 8 or later

TestNG is compatible with Java 5 or later


Both JUnit 5 and TestNG are powerful and feature-rich testing frameworks, but there are some differences in their approach and capabilities. JUnit 5 is more modern and modular, with support for features like dependency injection and parameterized tests, but it may require additional frameworks to support more advanced use cases. TestNG, on the other hand, provides more out-of-the-box support for parallel testing, data-driven testing, and reporting, but it may have a steeper learning curve for those new to the framework.


Ultimately, the choice between JUnit 5 and TestNG will depend on the specific needs of your project and the testing requirements.


TestNG and JUnit


TestNG and JUnit are two popular testing frameworks for Java applications.

TestNG is a testing framework inspired by JUnit and NUnit, designed to cover a broader range of test categories than JUnit. It supports many advanced features such as parallel testing, test configuration through XML or Java annotations, test prioritization, and data-driven testing using data providers. TestNG also provides detailed HTML reports of test results, making it easier to analyze and debug test failures.

JUnit, on the other hand, is a simple, lightweight testing framework that supports both JUnit 3 and JUnit 4 annotations. It provides basic support for writing and running unit tests, including assertions, test fixtures, and test suites. It is widely used and has a large and active community.

Which framework is better depends on the specific needs of your project. TestNG is a more powerful and feature-rich framework that supports a wider range of test categories and advanced features. If you need to perform more complex tests such as integration tests, end-to-end tests, or data-driven tests, TestNG may be a better choice. However, if you need a simple, lightweight framework for unit testing, JUnit may be a better fit.

Ultimately, both TestNG and JUnit are powerful and widely used testing frameworks, and the choice between them will depend on the specific requirements of your testing needs.

Feature

TestNG

JUnit

Annotations

Supports both JUnit 3 and JUnit 4 annotations as well as its own

Supports JUnit 3 and JUnit 4 annotations

Parallel tests

Allows tests to be run in parallel across multiple threads or processes

Can run tests in parallel but requires additional frameworks or custom code

Test configuration

Supports test configuration through XML or Java annotations

Configuration done through Java annotations or external configuration files

Data-driven testing

Provides built-in support for data-driven testing using data providers

Supports data-driven testing but requires additional frameworks or custom code

Test dependencies

Allows you to define test dependencies and control the order of test execution

Does not have built-in support for test dependencies

Test prioritization

Supports test prioritization based on priority attribute or groups

Supports test prioritization through annotations or by manually ordering the test methods

Test reports

Generates detailed HTML reports of test results with customizable output

Generates simple text-based reports by default, but can be extended with additional frameworks

Intro to TestNG

TestNG is a testing framework for Java applications that was introduced in 2004. It is inspired by JUnit and NUnit, but offers several advanced features and capabilities beyond those frameworks.

Some of the key features of TestNG include:

  1. Test Configuration: TestNG allows developers to configure tests through XML files or Java annotations, providing a flexible and easy-to-use configuration system.
  2. Test Suites: TestNG allows developers to group tests together into test suites, which can be executed together as a single unit.
  3. Data-Driven Testing: TestNG supports data-driven testing, allowing developers to run the same test with different sets of data.
  4. Parallel Testing: TestNG supports parallel test execution, allowing developers to run tests in parallel to reduce overall testing time.
  5. Annotations: TestNG provides a comprehensive set of annotations for test lifecycle, assertions, and assumptions.
  6. Dependency Injection: TestNG supports dependency injection through the use of the @Inject and @Guice annotations.
  7. Custom Report Generation: TestNG generates detailed HTML reports of test results, allowing developers to quickly identify and diagnose test failures.

Overall, TestNG is a powerful testing framework that offers a range of advanced features and capabilities beyond those provided by JUnit. Its support for test configuration, data-driven testing, and parallel test execution make it a popular choice for testing Java applications. TestNG also has a large and active community of developers, making it easy to find resources and support when working with the framework.


Intro to JUnit 5



JUnit 5 is the latest version of the popular JUnit testing framework for Java applications. It was released in 2017 and provides a number of new features and improvements over previous versions.

Some of the key features of JUnit 5 include:

  • Modular Architecture: JUnit 5 is built with a modular architecture, allowing for more flexible and customizable testing. Test classes, lifecycle methods, and extensions are all part of different modules that can be combined and customized as needed.
  • Improved Extensions Model: JUnit 5 introduces a new extensions model, which allows developers to extend the framework and add custom functionality. This new model is more flexible and powerful than the previous extension model, allowing for more complex use cases.
  • Parameterized Tests: JUnit 5 provides built-in support for parameterized tests, allowing developers to run the same test with different inputs. This can simplify testing and make it easier to cover different scenarios.
  • Nested Tests: JUnit 5 allows for nested tests, allowing developers to group related tests together and create a more organized test suite.
  • Assertions Improvements: JUnit 5 provides a new assertAll() method that allows developers to group multiple assertions together and see all failures at once.
  • Dynamic Tests: JUnit 5 provides built-in support for dynamic tests, allowing developers to generate tests at runtime based on input data.
Overall, JUnit 5 is a powerful and flexible testing framework that provides a number of new features and improvements over previous versions. It is widely used and supported by a large community of developers, making it a popular choice for testing Java applications.

 

HTTPS vs HTTP


HTTP (Hypertext Transfer Protocol) and HTTPS (Hypertext Transfer Protocol Secure) are both protocols used for communication between web servers and clients (such as web browsers). The main difference between the two is the added layer of security provided by HTTPS.

HTTP is the original protocol used for communication between web servers and clients. It sends data in plain text, which means that anyone with access to the network can potentially read the information being transmitted, including sensitive data such as login credentials, credit card numbers, and personal information.

HTTPS, on the other hand, adds an extra layer of security by encrypting the data being transmitted using an SSL/TLS (Secure Sockets Layer/Transport Layer Security) certificate. This means that any data sent between the server and the client is encrypted and therefore much harder for an attacker to intercept or decipher.

When a website uses HTTPS, it also adds an extra layer of verification to ensure that the website being accessed is the one it claims to be. This is done through the use of digital certificates, which are issued by trusted third-party certificate authorities (CAs) and provide authentication for the website.

In summary, HTTPS is a more secure version of HTTP that adds encryption and verification to protect against eavesdropping and impersonation. It is especially important for websites that handle sensitive information such as login credentials or financial transactions. 



HTTP

HTTPS

Security

Sends data in plain text

Adds encryption to protect data

Protocol

Standard protocol for web browsing

Secure version of HTTP protocol

Port

Port 80

Port 443

Certificate

No digital certificate is required

SSL/TLS certificate is required for encryption

Authentication

No authentication is provided

Provides authentication through SSL/TLS certificate

Use case

Suitable for websites with no sensitive information

Suitable for websites that require secure transmission of sensitive information, such as financial transactions, logins, and personal information


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...