Introduction to Selenium
Overview
Selenium is a powerful open-source automation framework used for web application testing It provides a suite of tools and libraries for automating web browsers across different platforms
The development of Selenium began in 2004 as a project by Jason Huggins It was initially designed as an internal tool at ThoughtWorks to automate testing for a web application Later, Selenium evolved into a full-fledged open-source framework with contributions from various developers and organizations.
Key Features of Selenium
Selenium WebDriver is a crucial component of the Selenium framework It is a programming interface that allows interaction with web browsers and enables the automation of web-based tasks WebDriver supports multiple programming languages and provides a rich set of functions for interacting with web elements, navigating through web pages, and executing actions such as clicking buttons, entering text, and handling alerts
Selenium IDE (Integrated Development Environment) is a record-and-playback tool that allows users to create and execute automation scripts without the need for programming knowledge It provides a graphical user interface (GUI) for recording user interactions and generating test scripts
Selenium Grid is a distributed testing framework that enables parallel execution of tests on multiple machines and browsers simultaneously It allows for efficient test execution across different environments and configurations, enhancing scalability and reducing testing time
Selenium RC (Remote Control) has been a leading testing framework of the Selenium project for a long time It is the first automation testing tool for the web that allows users to use their preferred programming language Starting from version 2.25.0, RC can support the following programming languages: Java, C#, PHP, Python, Perl, Ruby.
Selenium Supported Browsers
Selenium supports Mozilla Firefox as one of the major browsers for test automation It provides a Firefox-specific driver that facilitates interaction with Firefox instances
Selenium also supports Google Chrome for automation The ChromeDriver, a separate executable, is used as the driver for controlling Chrome instances
Figure 8 - Selenium with Microsoft Edge
With the introduction of the Chromium-based Microsoft Edge, Selenium now supports automation on this browser as well The EdgeDriver serves as the interface between Selenium WebDriver and the Edge browser.
Selenium Integration with Programming Languages
Java is one of the most popular programming languages used for Selenium automation Selenium WebDriver provides robust Java bindings that allow developers to write test scripts using Java
Python is another widely adopted language for Selenium Selenium WebDriver offers a comprehensive Python API that simplifies test script creation and execution
For those working with the NET framework, Selenium provides C# bindings, enabling the development of test automation scripts using C#.
Benefits of Using Selenium WebDriver
Selenium WebDriver allows testing on multiple browsers, ensuring compatibility across different platforms and versions
With Selenium WebDriver, test scripts can be written in various programming languages, providing flexibility for teams with different language preferences
1.5.3 Enhanced Interactions with Web Elements
WebDriver offers a wide range of methods to interact with web elements, enabling actions like clicking, typing, selecting options, and handling complex scenarios
Selenium WebDriver can execute tests in parallel, which significantly reduces the overall testing time and increases efficiency
WebDriver seamlessly integrates with popular test frameworks, such as JUnit and TestNG, allowing for efficient test management and reporting.
Conclusion
In this chapter, we provided an overview of Selenium, including its history and key components We focused on Selenium WebDriver as a crucial part of the framework, discussing its features, supported browsers, integration with programming languages, and the benefits it offers for web application testing
Selenium is widely recognized as a reliable and robust automation framework for web testing With Selenium WebDriver, developers have a powerful tool at their disposal to automate interactions with web browsers and perform various tasks seamlessly Its support for multiple programming languages, such as Java, Python, and C#, enables teams to work with their preferred language
By utilizing Selenium WebDriver, testers can achieve cross-browser compatibility, ensuring that their web applications function correctly on different browsers and platforms The extensive set of functions provided by WebDriver allows for efficient and precise interaction with web elements, enabling actions like clicking buttons, entering text, and handling alerts
Furthermore, Selenium WebDriver supports parallel execution, allowing for faster testing cycles and improved efficiency Its integration with popular test frameworks like JUnit and TestNG enhances test management and reporting capabilities
In the following chapters, we will delve deeper into the practical aspects of using Selenium WebDriver for automation, exploring various techniques, best practices, and advanced features to empower testers in their web application testing endeavors.
Install and use Selenium
Installation Requirements
To use Selenium WebDriver, you need:
- A web browser that supports automation (e.g., Chrome, Firefox, Edge, or Safari)
- A programming language that you know (e.g., Java, Python, C#, or Ruby)
- An IDE that supports your programming language (e.g., Eclipse, Visual Studio Code, PyCharm, or IntelliJ IDEA)
To install Selenium WebDriver, you need to:
- Check your web browser version and download the corresponding driver from the Selenium website
- Place the driver in a directory that is in your system’s PATH environment variable or add a new directory to your PATH
- Test if the driver works by typing its name in a command prompt
- Install the programming language and IDE of your choice from their official websites.
Installation Selenium WebDriver
Selenium WebDriver installation process is completed in four basic steps:
- Download and Install Java 8 or higher version
- Download and configure Eclipse or any Java IDE of your choice
- Download Selenium WebDriver Java Client
Once you have downloaded and installed the latest version of Java, you need to set path or configure the environment variables in your system Refer the link given below to understand how we can set path and configure environment variables in Java
2.2.2 Download and Configure Eclipse IDE
Open URL: https://www.eclipse.org/downloads/
Click on the "Download Packages" link (you can also download the IDE directly from the
"downloads page" of Eclipse official website, but we will recommend you to navigate through the download packages section and get "Eclipse IDE for Java Developers")
It will redirect you to the "Download Packages" section Scroll down through the webpage and click on "Eclipse IDE for Java Developers"
Go to the Download Links section and click on "Windows 64-bit" You can also select other options to download based on the operating system you are currently working on
Figure 13 - Default interface of Eclipse IDE
2.2.3 Download Selenium WebDriver Java Client
Open URL: https://docs.seleniumhq.org/download/
It will redirect you to the "downloads page" of Selenium official website
Scroll down through the web page and locate Selenium Client & WebDriver Language Bindings
Figure 14 - Download link of Java Client Driver
To configure Selenium WebDriver with Eclipse, you can follow these steps:
- Launch Eclipse and create a workspace for your Selenium projects A workspace is a folder that contains all your project files and settings
- Create a new Java project in Eclipse by selecting File > New > Java Project Give your project a name and click Finish
- Create a new package under your project by right-clicking on it and selecting New > Package Give your package a name and click Finish
- Create a new class under your package by right-clicking on it and selecting New > Class Give your class a name and click Finish
- Add Selenium WebDriver JAR files to your project by right-clicking on it and selecting Properties > Java Build Path > Libraries > Add External JARs Browse to the location where you extracted the Selenium WebDriver ZIP file and select all the JAR files Click OK to add them to your project
- Write your Selenium WebDriver code in the class that you created You can use the examples from the official Selenium documentation1 or other online resources23 to get started
Write and run simple test cases with Selenium WebDriver
To create your first Selenium automation test script, you need to follow these steps:
- Launch your IDE and create a new project for your Selenium tests Add the Selenium WebDriver library to your project as a dependency
- Create a new class or file for your test case and import the necessary libraries (e.g., Selenium WebDriver, testing framework, etc.)
- Declare a WebDriver variable to manage the connection with the browser Instantiate the WebDriver variable with the browser driver of your choice
- Use the get() method of the WebDriver variable to open the URL of the web page you want to test
- Use the findElement() method of the WebDriver variable to locate the web elements you want to interact with by using a locator strategy (e.g., By.id, By.name, By.xpath, etc.)
- Use the methods of the WebElement object returned by the findElement() method to interact with the web elements (e.g., click(), sendKeys(), getText(), etc.)
- Use the assertions of your testing framework (e.g., JUnit, TestNG, etc.) to verify the expected results of your test case
- Use the quit() method of the WebDriver variable to close the browser and end the session.
Conclution
Selenium WebDriver is a powerful tool that allows you to interact with web elements and perform various actions on them You have also learned how to use different locators, waits, assertions and exceptions to handle different scenarios in web testing
Some tips and tricks that can help you use Selenium WebDriver more effectively in complex test cases are:
- Use TestNG or other testing frameworks to organize your tests, run them in parallel, pass parameters and generate reports
- Use DataProvider method to provide data sets for your tests and run them with different values
- Use switchTo() method to handle pop-up windows, alerts, frames and tabs12
- Use dynamic locators such as XPath or CSS selectors to handle dynamic content or web elements that change frequently
- Use explicit waits or FluentWait class to handle synchronization issues and wait for specific conditions to be met
- Use Page Object Model (POM) design pattern to create reusable and maintainable code by separating the web elements and the test logic
- Use Actions class or JavaScriptExecutor interface to perform advanced user interactions such as drag and drop, mouse hover, scrolling etc
Using Selenium to test Lazada website
Introduction to Lazada
Lazada is an e-commerce website that operates in several Southeast Asian countries, including Vietnam, Malaysia, Thailand, and Indonesia It serves as a platform connecting buyers and sellers, offering a wide range of products such as electronics, fashion, beauty, home appliances, and more
With a user-friendly interface and convenient shopping features, Lazada provides a seamless online shopping experience for its customers Users can browse through various categories, search for specific items, read product reviews, and compare prices before making a purchase Lazada also offers secure payment options and reliable delivery services to ensure customer satisfaction
Lazada has gained popularity due to its extensive product selection, competitive prices, and frequent promotions It collaborates with both local and international brands, offering customers a diverse range of choices Additionally, Lazada frequently organizes flash sales and special events, allowing customers to enjoy even greater discounts on their favorite products
In recent years, Lazada has expanded its services to include Lazada Wallet, a digital payment method that provides users with a convenient and secure way to make purchases Lazada also offers a mobile app, allowing users to shop on-the-go and stay updated with the latest deals and promotions
Overall, Lazada is a leading e-commerce platform in Southeast Asia, providing a convenient and enjoyable online shopping experience for millions of customers.
Test Scenario
Test Scenario Description Test Cases
2 Search for a product with no results
3 Search for a product with filtering criteria
1 Successful login with valid login credentials
2 Unsuccessful login with invalid login credentials
3 Login using social media account
TS-03 Add products to shopping cart
1 Add a product to the shopping cart from the search results page
2 Add a product to the shopping cart from the product listings page
3 Add a product to the shopping cart from the filtered product listings page
1 Successful logout from the account
2 Logout when not logged into an account
3 Logout while performing other activities on the website
TS-05 Remove a product from the shopping cart
1 Successfully remove a product from the shopping cart
2 Remove a product from the shopping cart when the cart is empty
3 Remove a product from the shopping cart when there are multiple items in the cart
TS-06 Update the quantity of a product in the shopping cart
1 Successfully update the quantity of a product in the shopping cart
2 Update the quantity of a product in the shopping cart when the cart is empty
3 Update the quantity of a product in the shopping cart when there are multiple items in the cart
2 Make a purchase without logging in
3 Make a purchase with a quantity of products exceeding the available inventory
Test Case
1 Open https://www.lazada. vn/
Redirect to SRP with product
Search for a product with no results
1 Open https://www.lazada. vn/
Redirect to SRP with message: " Search No Result"
Search for a product with filtering criteria
1 Open https://www.lazada. vn/
Redirect to SRP with product
Successful login with valid login credentials
1 Open https://www.lazada. vn/
Unsuccessful login with invalid login credentials
1 Open https://www.lazada. vn/
Login using social media account
1 Open https://www.lazada. vn/
Add a product to the shopping cart from the search results page
1 Open https://www.lazada. vn/
4 Click on "Add to cart" button
Add a product to the shopping cart from the product listings page
1 Open https://www.lazada. vn/
4 Click on "Add to cart" button
Add a product to the shopping cart from the filtered product listings page
1 Open https://www.lazada. vn/
4 Click on "Add to cart" button
Successful logout from the account
1 Open https://www.lazada. vn/
TC-011 Logout when Not 1 Open Not found
17 not logged into an account logged https://www.lazada. vn/
Logout while performing other activities on the website
1 Open https://www.lazada. vn/
Successfully remove a product from the shopping cart
1 Open https://www.lazada. vn/
Remove a product from the shopping cart when the cart is empty
1 Open https://www.lazada. vn/
Remove a product from the shopping cart when there are multiple items in the cart
1 Open https://www.lazada. vn/
Successfully update the quantity of a product in the shopping cart
1 Open https://www.lazada. vn/
Update the quantity of a product in the shopping cart when the cart is empty
1 Open https://www.lazada. vn/
Update the quantity of a product in
1 Open https://www.lazada. vn/
Successful update product the shopping cart when there are multiple items in the cart cart 2 Access to cart
1 Open https://www.lazada. vn/
2 Access and perform a Checkout process
Make a purchase without logging in
1 Open https://www.lazada. vn/
2 Access and perform a Checkout process
Requires login and redirect to Login page
Make a purchase with a quantity of products exceeding the available inventory
& product on the cart with quantity exceeds quantity in stock
1 Open https://www.lazada. vn/
2 Access and perform a Checkout process