Companies are increasingly using automation testing to guarantee efficiency and quality as software applications get more sophisticated and the demand for quicker release cycles increases. One of the best tools in this field is Selenium. Selenium automation testers are in high demand right now, and this demand is only expected to increase. Learn automation testing from scratch in this Selenium automation testing tutorial and get started with our Selenium testing course syllabus.
Selenium for Beginners
Selenium is a robust open-source framework for web browser automation. Its main purpose is to test web apps to make sure they work properly.
Key Aspects of Selenium:
- Automates browser interactions: Selenium is capable of simulating a variety of user behaviors on a webpage, including surfing pages, clicking buttons, and completing forms.
- Supports multiple browsers: Chrome, Firefox, Safari, Edge, and other popular browsers are among them.
- Supports multiple programming languages: You can develop test scripts in a variety of languages, including Ruby, JavaScript, Python, C#, and Java.
- Cross-platform: Selenium tests are compatible with Linux, macOS, and Windows, among other operating systems.
Recommended: Selenium Online Course.
Different Components of Selenium
Selenium is a flexible technology that uses automated testing to help guarantee the dependability and quality of web applications. Major components of Selenium are:
Selenium IDE (Integrated Development Environment):
- A browser plugin that lets you record, modify, and replay user interactions as test scripts in Chrome and Firefox.
- Excellent for beginners to quickly construct and execute basic tests.
Selenium WebDriver:
- A strong API that enables you to create more intricate and reliable test scripts for direct browser interaction.
- It uses particular browser drivers (like ChromeDriver and GeckoDriver) to support A number of programming languages.
- Extensively employed for more complex test automation.
Selenium Grid:
- A program that lets you run your tests simultaneously on several computers and web browsers.
- It increases test coverage, saves time, and aids in distributed testing.
Key Features of Selenium:
Some of the key features of Selenium:
- Cross-browser compatibility: Guarantees that your web application functions uniformly in various browsers.
- Multiple language support: the freedom to utilize the programming language of your choice.
- Parallel test execution: This method expedites testing by executing tests concurrently.
- Record and playback (Selenium IDE): The Selenium IDE’s record and playback feature makes it simple to create simple test scripts without knowing any code.
- Integration with testing frameworks: Improves test reporting and organization by working nicely with JUnit, TestNG, and other frameworks.
- Free and open-source: No fees for licensing.
- Large and active community: There are lots of resources and assistance available.
Learn the fundamentals with our Software Testing Training in Chennai.
Environmental Setup for Selenium
Prerequisites:
- Programming Language: Install the programming language of your choice, such as Python or Java.
- Package Manager: To install Selenium libraries, use Maven/Gradle (for Java), pip (for Python), etc.
- Selenium Library: Install the Selenium bindings for the language of your choice using the Selenium Library (Example: pip install selenium for Python).
- Browser Drivers: Get the executable drivers for the browsers you wish to automate, such as GeckoDriver for Firefox and ChromeDriver for Chrome. Verify that they are in the PATH of your system or that your code specifies where they are.
Selenium Web Driver Basics
Selenium learning step by step involves Setting up the environment is the first step, after which you must learn the fundamentals of WebDriver for browser automation, investigate more complex interactions, integrate with testing frameworks such as TestNG or JUnit, embrace the Page Object Model for improved organization, and then use data-driven testing for wider coverage. It also covers selenium programs for beginners.
The main tool for automating web browser interactions is Selenium WebDriver. It gives you the ability to manage a browser from your code. These are the basic concepts:
WebDiver Instance:
Making an instance of the WebDriver for a certain browser is the first step. A new browser session is started as a result.
Example: Python
from selenium import webdriver
# For Chrome
driver = webdriver.Chrome()
# For Firefox
# driver = webdriver.Firefox()
Navigating the Browser:
You can go to websites with WebDriver.
- driver.get(“URL”): opens the current browser window and loads a new webpage.
- driver.back(): The “back” button is clicked.
- driver.forward(): The “forward” button is clicked by driver.forward().
- driver.refresh(): The current page is reloaded.
Locating Web Elements:
You must first find the elements on a web page (buttons, text fields, etc.) in order to interact with them. WebDriver offers a number of “locators”:
- ID: Uses an element’s distinct id attribute to locate it.
- NAME: Uses the name attribute to find elements.
- XPATH: Navigates the HTML structure using XPath expressions. (Strong yet may be harder to read.)
- CSS_SELECTOR: Targets elements using CSS selectors. (Frequently shorter than XPath)
- CLASS_NAME: Uses the CSS class to find items.
- TAG_NAME: Uses an element’s HTML tag (such as or ) to locate it.
- LINK_TEXT: Uses the precise link text to find anchor () components.
- PARTIAL_LINK_TEXT: Finds anchor elements that have the given text in them.
Methods to find elements:
driver.find_element(by=By.ID, value=”element_id”) (finds the first matching element)
driver.find_elements(by=By.CLASS_NAME, value=”class_name”) (finds all matching elements)
You’ll need to import the By class:
from selenium.webdriver.common.by import By
Interacting with Web Elements:
You can take the following actions on an element once you’ve found it:
- element.send_keys(“text”): Text is entered into an input field or text area using it.
- element.click(): Selects a checkbox, button, link, etc.
- element.clear(): To remove text from an input field, use it.
- element.is_selected(): It verifies whether a radio button or checkbox is selected.
- element.get_attribute(“attribute_name”): The value of an attribute (such as href, value) can be obtained using it.
- element.text: Retrieves the element’s visible text.
Browser Management:
You can also control the browser window with WebDriver:
- driver.maximize_window(): It maximizes the browser window.
- driver.minimize_window(): It minimizes the browser window.
- driver.fullscreen(): It sets the browser to full-screen mode.
- driver.close(): It closes the current browser window.
- driver.quit(): It closes all browser windows and ends the WebDriver session.
Grab the basics with our software testing tutorial for beginners.
Advanced WebDriver Interactions in Selenium
Here are the advanced WebDriver interactions in Selenium:
Handling Alerts, Pop-ups, and Modals:
JavaScript alerts, confirmation dialogs, and custom modal windows are frequently used in web applications. To work with these, WebDriver offers the Alert class.
To switch to an alert:
alert = driver.switch_to.alert
Common actions:
- alert.accept(): Clicks the “OK” button.
- alert.dismiss(): Clicks the “Cancel” button (if available).
- alert.send_keys(“text”): Enters text into a prompt alert.
- alert.text: Gets the text of the alert.
Working with Iframes:
You must shift the driver’s focus to the iframe before working with its elements if a web page has any.
Switching to an iframe:
# By index
driver.switch_to.frame(0)
# By name or ID
driver.switch_to.frame(“frame_name_or_id”)
# By WebElement
iframe_element = driver.find_element(By.ID, “iframe_id”)
driver.switch_to.frame(iframe_element)
# After interacting with the iframe, switch back to the main content:
driver.switch_to.default_content()
Executing JavaScript:
JavaScript may occasionally need to be run directly in the browser. This is made possible via WebDriver’s execute_script() method.
Example: Scrolling to the bottom of the page:
driver.execute_script(“window.scrollTo(0, document.body.scrollHeight);”)
Values from the JavaScript execution can also be returned.
Handling Multiple Windows/Tabs:
Web apps have the ability to launch new tabs or windows. You can switch between them with WebDriver.
To get the current window handle:
main_window = driver.current_window_handle
To get all open window handles:
all_windows = driver.window_handles
To switch to a specific window:
for window in all_windows:
if window != main_window:
driver.switch_to.window(window)
break
To close the current window:
driver.close()
To switch back to the original window:
driver.switch_to.window(main_window)
Mouse Actions (using ActionChains):
The ActionChains class is used for more intricate mouse operations, such as drag and drop, hovering, double-clicking, and right-clicking.
Example: Right-clicking an element:
from selenium.webdriver.common.action_chains import ActionChains
from selenium.webdriver.common.by import By
element = driver.find_element(By.ID, “some_element”)
actions = ActionChains(driver)
actions.context_click(element).perform()
Additional actions include move_to_element(), drag_and_drop(source, target), double_click(), and others. Do not forget to call.The final action in the sequence is perform().
Keyboard Actions (using Keys and ActionChains):
In addition to send_keys(), you can also imitate other keyboard inputs, including pressing specific keys (Ctrl, Shift, Alt, Enter, etc.).
Example: Sending Ctrl+A (select all):
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.action_chains import ActionChains
from selenium.webdriver.common.by import By
text_field = driver.find_element(By.ID, “input_field”)
actions = ActionChains(driver)
actions.key_down(Keys.CONTROL).send_keys(‘a’).key_up(Keys.CONTROL).perform()
You can automate more complicated web application scenarios with these sophisticated interactions.
Learn the in-demand programming language with our Python tutorial for beginners.
TestNG/JUnit Framework Integration with Java
TestNG is a robust Java testing framework that works well with Selenium.
Key Features TestNG Adds to Selenium:
- Annotations: TestNG defines test methods and setup/teardown procedures using annotations (such as @Test, @BeforeMethod, @AfterMethod, @BeforeClass, @AfterClass, etc.).
- Test Grouping: Tests can be logically grouped.
- Parameterization: Passing various data sets to your tests is made simple with it.
- Data Providers: A more sophisticated method of supplying tests with external data.
- Parallel Execution: Execute tests in parallel by running them simultaneously.
- Reporting: Test results are automatically reported.
Basic Integration Steps:
Add TestNG Dependency: Include the TestNG dependency in your Gradle or Maven project.
Maven
<dependency>
<groupId>org.testng</groupId>
<artifactId>testng</artifactId>
<version>7.7.1</version> <scope>test</scope>
</dependency>
Gradle:
testImplementation ‘org.testng:testng:7.7.1’ // Use the latest version
Create Test Classes: Use TestNG annotations to write your Selenium test logic within classes.
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.testng.Assert;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Test;
public class MySeleniumTest {
WebDriver driver;
@BeforeMethod
public void setUp() {
// Set up WebDriver (example for Chrome)
System.setProperty(“webdriver.chrome.driver”, “/path/to/chromedriver”); // Update the path
driver = new ChromeDriver();
driver.get(“https://www.google.com”);
}
@Test
public void verifyPageTitle() {
String expectedTitle = “Google”;
String actualTitle = driver.getTitle();
Assert.assertEquals(actualTitle, expectedTitle, “Title mismatch!”);
}
@AfterMethod
public void tearDown() {
if (driver != null) {
driver.quit();
}
}
}
Run Tests: You can use the Maven/Gradle plugins, the TestNG command-line runner, or your IDE to execute TestNG tests.
JUnit Integration (Java)
Another well-liked Java testing framework is JUnit.
Key Features JUnit Adds to Selenium:
- Annotations: JUnit employs annotations such as @Test, @BeforeEach, @AfterEach, @BeforeAll, and @AfterAll, just like TestNG.
- Assertions: Offers strategies for stating anticipated results.
Basic Integration Steps:
Add JUnit Dependency:
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-api</artifactId>
<version>5.11.0-M1</version> <scope>test</scope>
</dependency>
Gradle:
testImplementation ‘org.junit.jupiter:junit-jupiter-api:5.11.0-M1’ // Use the latest version
Create Test Classes:
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import static org.junit.jupiter.api.Assertions.assertEquals;
public class MyJUnitSeleniumTest {
WebDriver driver;
@BeforeEach
void setUp() {
System.setProperty(“webdriver.chrome.driver”, “/path/to/chromedriver”); // Update the path
driver = new ChromeDriver();
driver.get(“https://www.google.com”);
}
@Test
void verifyPageTitle() {
String expectedTitle = “Google”;
String actualTitle = driver.getTitle();
assertEquals(expectedTitle, actualTitle, “Title mismatch!”);
}
@AfterEach
void tearDown() {
if (driver != null) {
driver.quit();
}
}
}
Run Tests: Maven/Gradle plugins or your IDE can be used to execute JUnit tests.
Recommended: Manual Testing Tutorial for Beginners.
Framework Integration in Other Languages (e.g., Python)
The fundamental ideas are the same even when the precise syntax and framework names vary:
- Unit Testing Frameworks: Python and other languages come with built-in unittest and pytest, which offer a framework for creating and executing tests.
- Setup/Teardown: You can specify methods that execute both before and after tests or test groups using these frameworks.
- Assertions: They provide tools to determine if the predicted and actual results match.
Example (Python with pytest):
Install pytest:
pip install pytest
Create a test file (e.g., test_google.py):
from selenium import webdriver
import pytest
@pytest.fixture
def driver():
driver = webdriver.Chrome()
yield driver
driver.quit()
def test_google_title(driver):
driver.get(“https://www.google.com”)
assert “Google” in driver.title
Integrating Selenium with a testing framework offers improved reporting and administration of test execution in addition to an organized and effective method for creating, planning, and executing automated web tests.
Suggested: Manual Testing Training in Chennai.
Page Object Model (POM) in Selenium:
A popular design pattern in test automation for building a more reliable and maintainable test framework is the Page Object Model. In POM, a comparable class known as a Page Object represents each web page of the application being tested.
- Encapsulation: The elements (locators) and actions that can be carried out on a page are encapsulated in each page object.
- Separation of Concerns: It keeps the implementation specifics of each page apart from the test logic. This implies that your test scripts are essentially unaffected by changes to the user interface; all you have to do is update the relevant page object.
- Reusability: Code reusability is encouraged via page objects. A page object’s methods may be called by more than one test case.
How it Works:
You construct a class for every web page in your application.
You define the following in that class:
- Web Element Locators: They are methods for locating elements on a page, such as by using the ID, Name, XPath, and CSS selectors. Typically, the page object class defines these selectors as variables.
- Methods (Page Actions): The actions that can be carried out on the page are represented by these methods (e.g., clicking a button, entering text, receiving text). Usually, these methods work with web elements that are defined in the same class.
Example (Conceptual – Python):
Assume you have a login page with a button to log in, a username field, and a password field.
from selenium.webdriver.common.by import By
from selenium.webdriver.remote.webdriver import WebDriver
class LoginPage:
def __init__(self, driver: WebDriver):
self.driver = driver
self.username_textbox = (By.ID, “username”)
self.password_textbox = (By.ID, “password”)
self.login_button = (By.ID, “loginBtn”)
self.error_message = (By.ID, “error”)
def enter_username(self, username):
self.driver.find_element(*self.username_textbox).send_keys(username)
def enter_password(self, password):
self.driver.find_element(*self.password_textbox).send_keys(password)
def click_login(self):
self.driver.find_element(*self.login_button).click()
def get_error_message(self):
return self.driver.find_element(*self.error_message).text
def login(self, username, password):
self.enter_username(username)
self.enter_password(password)
self.click_login()
# Test case using the LoginPage object
def test_invalid_login(driver):
login_page = LoginPage(driver)
login_page.login(“invalid_user”, “wrong_password”)
assert “Invalid credentials” in login_page.get_error_message()
Benefits of Using POM:
Here are the advantages of using POM:
- Enhanced Maintainability: Modifications to the user interface only need to be made to the relevant page object, not all test scripts.
- Improved Readability: As page interactions are abstracted into page objects, test scripts become clearer and concentrate on the test logic.
- Improved Reusability: You can reuse page objects and associated methods in other test cases.
- Improved Organization: The framework gets more organized and comprehensible.
POM is essentially a best practice that aids in creating a Selenium automation framework that is more scalable and maintainable.
Suggested: Java Training in Chennai.
Data Driven Testing with Selenium
In data-driven testing, the test input and expected output data are read from an external source (such as databases, Excel sheets, CSV files, JSON files, etc.) while the test logic stays the same.
This increases test coverage and decreases code duplication by enabling you to run the same test script repeatedly with various data sources.
Why Use Data-Driven Testing?
- Enhanced Test Coverage: You don’t need to write several test scripts to test different scenarios with different input combinations.
- Better Maintainability: You just need to make one modification to the script, and it will be applied to all data sets, if the test logic changes.
- Improved Organization: Test data and code are kept apart, which facilitates management and comprehension.
Common Methods for Using Selenium for Data-Driven Testing:
Utilizing CSV Documents:
- Read information from value files with commas between them.
- Usually, a test case or a collection of test data is represented by each row in the CSV file.
Utilizing Excel Sheets:
- Read information from files ending in.xls or.xlsx.
- It is possible to use libraries such as openpyxl (for Python) or Apache POI (for Java).
Utilizing JSON Documents:
- Read information from files in JavaScript Object Notation (JSON).
- Ideal for data that is more structured.
Employing Databases:
- Get test data straight out of a database.
Example (Python with pytest and CSV):
Suppose you have the following information in a CSV file called login_data.csv:
username,password,expected_result
valid_user,valid_password,Login Successful
invalid_user,wrong_password,Login Failed
another_invalid,some_other,Login Failed
Here is a Python test script that reads this data and runs login tests using pytest:
import pytest
from selenium import webdriver
from selenium.webdriver.common.by import By
import csv
def get_login_data(file_path):
data = []
with open(file_path, ‘r’) as csvfile:
reader = csv.reader(csvfile)
next(reader) # Skip the header row
for row in reader:
username, password, expected_result = row
data.append((username, password, expected_result))
return data
@pytest.fixture
def driver():
driver = webdriver.Chrome()
yield driver
driver.quit()
@pytest.mark.parametrize(“username, password, expected_result”, get_login_data(“login_data.csv”))
def test_login(driver, username, password, expected_result):
driver.get(“your_login_page_url”) # Replace with your actual login page URL
username_field = driver.find_element(By.ID, “username”)
password_field = driver.find_element(By.ID, “password”)
login_button = driver.find_element(By.ID, “loginBtn”)
result_message_locator = By.ID, “result” # Assuming there’s an element to display the result
username_field.send_keys(username)
password_field.send_keys(password)
login_button.click()
result_element = driver.find_element(*result_message_locator)
assert expected_result in result_element.text
- The login_data.csv file contains the data that get_login_data() retrieves.
- Each row of data from the CSV file is passed as parameters to the test_login method using @pytest.mark.parametrize.
- After performing the login operation with this data, the test_login function asserts the desired result.
Implementation in other languages/frameworks:
- Java with TestNG: The @DataProvider annotation in TestNG allows methods that may read from external files to offer test data in Java.
- Java with JUnit: Data-driven testing tools such as @ParameterizedTest and @CsvSource/@CsvFileSource are also included in JUnit 5.
Recommended: All Software Training Courses
Conclusion
Well done for finishing this Selenium Automation Testing Tutorial! You now have a basic grasp of how to use Selenium WebDriver to automate web browser interactions. Mastering automation is a continuous process, so continue honing your skills, investigating more complex features, and incorporating these ideas into your testing assignments. Utilize our Selenium testing training in Chennai. Have fun with your testing journey!