Tuesday, January 14, 2025

Migrating a Selenium 2 Project to Selenium 4: A Comprehensive Guide

A few years ago, I developed a set of regression tests using Selenium 2.53.0 and TestNG 6.9.9. These scripts served their purpose well at the time. Recently, I needed to reuse these scripts for a project, but to my surprise, they no longer functioned as expected.

Upon investigation, I found that the project was outdated due to deprecated Selenium dependencies and API changes introduced in newer versions. To ensure compatibility and leverage the latest features of Selenium, I decided to migrate the project to Selenium 4. Below, I outline the steps I took during this migration process.


Steps for Conversion

1. Update Selenium Dependency in pom.xml

The first step was to replace the outdated Selenium 2 dependency with the latest Selenium 4 version in the Maven pom.xml file. I updated it as follows:

<dependency>
    <groupId>org.seleniumhq.selenium</groupId>
    <artifactId>selenium-java</artifactId>
    <version>4.21.0</version>
</dependency>

<dependency>
    <groupId>org.testng</groupId>
    <artifactId>testng</artifactId>
    <version>7.10.2</version>
    <scope>test</scope>
</dependency>
  • Why TestNG?
    Since my original project was built with TestNG for test management, I also updated the TestNG dependency to the latest version (7.10.2).

After updating the pom.xml file, I ran the following Maven command to ensure all dependencies were downloaded and updated:

mvn clean install

This step ensured that my project had the latest Selenium and TestNG libraries required for Selenium 4 compatibility.


2. Refactor the Code to Align with Selenium 4 Changes

Selenium 4 introduced several changes and improvements over Selenium 2. As part of the migration process, I reviewed and refactored the code to address these updates. Below are the key changes:

a. WebDriver Initialization

In Selenium 4, initializing WebDriver no longer requires manually setting the system property for the driver path. The updated approach simplifies this process, allowing you to directly instantiate the WebDriver.

  • Before (Selenium 2):
    WebDriver driver = new ChromeDriver();
    System.setProperty("webdriver.chrome.driver", "//path//of//the//driver");
    driver.get(baseUrl);
    
  • After (Selenium 4):
    WebDriver driver = new ChromeDriver();
    driver.get(baseUrl);
    
b. Modernizing WebDriver Initialization

In Selenium 2, WebDriver initialization was simpler but less flexible. Selenium 4 introduced a more robust and modular way to configure WebDriver instances using Service and Options.

  • Before (Selenium 2):

    WebDriver driver = new ChromeDriver();
    
  • After (Selenium 4):

    import org.openqa.selenium.chrome.ChromeDriver;
    import org.openqa.selenium.chrome.ChromeOptions;
    import org.openqa.selenium.chrome.ChromeDriverService;
    
    ChromeDriverService service = new ChromeDriverService.Builder()
        .usingDriverExecutable(new File("path/to/chromedriver"))
        .build();
    
    ChromeOptions options = new ChromeOptions();
    options.addArguments("--start-maximized");
    
    WebDriver driver = new ChromeDriver(service, options);
    

This approach is not only compliant with Selenium 4 but also enables more granular control over browser configurations.

c. Updating Deprecated Methods

In Selenium 2, methods like findElementByClassName and findElementByCssSelector were commonly used but are now deprecated in Selenium 4. I replaced these methods with the updated By locators.

  • Before (Selenium 2):
    WebElement element = driver.findElementByClassName("example-class");
    
  • After (Selenium 4):
    WebElement element = driver.findElement(By.className("example-class"));
    

Similarly, other deprecated methods like findElementById, findElementByXPath, etc., were replaced using the By class:

  WebElement element = driver.findElement(By.id("example-id"));
  WebElement element = driver.findElement(By.xpath("//div[@id='example']"));
d. Actions and Interactions

Selenium 4 enhanced the Actions API, making it more reliable for complex interactions. I refactored existing code using the updated Actions API:

  • Before (Selenium 2):

    Actions actions = new Actions(driver);
    actions.moveToElement(element).perform();
    
  • After (Selenium 4):

    Actions actions = new Actions(driver);
    actions.moveToElement(element).clickAndHold().build().perform();
    
e. Implicit and Explicit Waits

The WebDriverWait API was also improved in Selenium 4. I explored some of the new features.

  • Before (Selenium 2):

    WebDriverWait wait = new WebDriverWait(driver, 10);
    WebElement element = wait.until(ExpectedConditions.presenceOfElementLocated(By.id("example-id")));
    
  • After (Selenium 4):

    WebDriverWait wait = new WebDriverWait(driver, Duration.ofSeconds(10));
    WebElement element = wait.until(ExpectedConditions.presenceOfElementLocated(By.id("example-id")));
    

3. Implementing Additional Selenium 4 Features

After refactoring for compatibility, I explored some of the new features introduced in Selenium 4, such as:

  • Relative Locators:
    Selenium 4 introduced relative locators, which simplify finding elements relative to other elements:

    WebElement element = driver.findElement(RelativeLocator.with(By.tagName("input")).above(anotherElement));
    
  • DevTools Protocol (CDP):
    Selenium 4 allows direct interaction with Chrome DevTools Protocol for advanced debugging and network control:

    driver.executeCdpCommand("Network.enable", new HashMap<>());
    

4. Testing and Debugging

After making all the changes, I thoroughly tested the scripts to ensure they ran as expected. During this process, I:

  • Fixed minor syntax errors caused by deprecated methods.
  • Addressed any performance issues by adding efficient waits and optimizing browser configurations.
  • Verified that all regression tests passed successfully in Selenium 4.

Final Outcome

By following the above steps, I successfully converted my Selenium 2 project to Selenium 4. The migration not only resolved the issues with running the scripts but also allowed me to leverage Selenium 4's new features for better stability, performance, and maintainability.

Sunday, December 11, 2016

API Monitoring and Testing

Application Programming Interfaces (APIs) testing is different than other testing as GUI is not available and this won't concentrate on look and feel of the application. API testing involves testing APIs directly. It enables communication and data exchange between two separate software systems and as part of the end-to-end transactions exercised during integration testing.

API testing commonly includes testing REST APIs or SOAP web services with JSON or XML message payloads being sent over HTTP, HTTPS, JMS. Since APIs lack a GUI, API testing is performed at the message layer.

Summary - RESTful vs SOAP Web Service

SOAP:
--XML based protocol
--Uses WSDL for communication between consumer and provider
--Does not return human readable result
--Transfer over HTTP, SMTP, FTP, etc.
--Javascript can call SOAP but difficult to implement
--Performance is not great compared to REST
--SOAP web services can be tested through programs or software such as Soap UI.


REST:
--Architectural style protocol
--Uses XML or JSON to send and receive data
--Result is readable which is just plain XML or JSON
--Transfer is over HTTP only
--Easy to call from Javascript
--Performance is better than SOAP. Amazon and Google are moving their APIs from SOAP to REST.
--REST can be easily tested through CURL command, Browsers and extensions such as Chrome Postman.


There are multiple API testing tools available
  • SoapUI
  • Runscope
  • Postman (Chrome Extension)
  • Advanced REST Client (Chrome Extension)
  • Insomnia REST Client (Chrome Extension)
Suppose, there is a checkbox in user registration form and the system using API function which receives input as date range and return a list of users that was registered with checkbox marked at that time interval.

Input: Date interval (e.g, fromdate=2016-12-01&todate=2016-12-15)
Output: list of users registered that time interval with checkbox marked

First of all, install any of the API testing tools and here i will be using SoapUI / Postman / Insomnia / Advanced REST Client.

Download SoapUI from here according to your machine. Other Insomnia / Advanced REST Client can installed from google chrome extensions.


1. Open SOAP UI application


2. Create REST project and put request URL

 
3. Authenticate using Username and Password

Authentication is stated as the act of confirming the identity of API consumer. After authenticated, they are usually authorized to get access to desired APIs
  • Authentication is used to determine who the user of an API is.
  • Authorization is used to determine what resources the identified user has access to. 

There are multiple standards and technologies available for authenticating users, for example;
  • Form-based - Web/HTML based authentication that commonly uses HTTP cookies.
  • Basic/Digest - Uses HTTP headers to identify users.
  • OAuth 1.x/2


4. Submit the request



 See image from Insomnia



Now you can move to advanced things :)



Wednesday, November 30, 2016

What should consider before implement Test Automation

Suppose, you are working in a project as manual tester and you are enjoying the working. One day, your QA manager share client's thinking and they are expecting to integrate Automation Testing in the projects as a regression test. So, your manager requested you to plan for Automation Testing. Now it's your turn how you can manage and plan for automation efficiently.

Before implement test automation, you have to face some of the questions and have to consider some of the factors whether it is best fit for the current system or not.
  • Are you thinking of 100% automation?
  • Is your application is stable enough to automate further testing work?
  • Automate testing procedure when you have lot of regression work
  • Think about skilled resources
  • Identify what test cases to automate
    • Repetitive tasks
    • Stable enough application
    • Complex calculation
    • Which require regular set up of environment
    • Things which are difficult for human
    • Task requiring multiple data sets
    • Apply common sense!

Tuesday, November 29, 2016

Test Automation Introduction

As a career of software tester, we all need to know the basic understanding of software test automation. There are good numbers of tools (Selenium, Telerik Test Studio, Robotium, QTP, TestComplete) to do the automation and there are multiple language (Java, C-Sharp, Python, Ruby) support.

Here, I will go further with Selenium and the language is Java. Selenium is a software testing framework for web applications. There are multiple components (Selenium IDE, Selenium RC, Selenium WebDriver, Selenium Grid) of Selenium. First one, Selenium IDE which is implemented as a Firefox Add-On, and it allows recording, editing, and debugging tests. The work around of Selenium IDE is not vast. It is less maintained and is only compatible with Selenium RC, which is deprecated product right now.

Selenium WebDriver (Selenium 2.0) implemented as a successor of Selenium RC. It has browser-specific browser driver, which launch the browser, sends commands, and retrieves results from browser application. Selenium 2.0 is supported in Python, Ruby, Java, and C#. So, we can write code according to our familiar languages.

Happy Coding! :)

Migrating a Selenium 2 Project to Selenium 4: A Comprehensive Guide

A few years ago, I developed a set of regression tests using Selenium 2.53.0 and TestNG 6.9.9 . These scripts served their purpose well at ...