If you’re new to test automation, Selenium is one of the most popular tools to get started with. It allows you to automate browser actions, making it easier to test web applications efficiently. In this blog, we’ll walk through how to write your very first Selenium test script step-by-step.
What is Selenium?
Selenium is an open-source framework used for automating web browsers. It supports multiple programming languages like Java, Python, C#, and JavaScript, and works with all major browsers including Chrome, Firefox, and Edge.
Using Selenium, you can simulate user interactions like clicking buttons, entering text, and navigating between pages—all automatically.
Prerequisites
Before writing your first Selenium test, make sure you have the following installed:
-
Java Development Kit (JDK) (if you choose Java)
-
An Integrated Development Environment (IDE) like IntelliJ IDEA or Eclipse
-
Selenium WebDriver libraries (which you can add as dependencies)
-
A browser driver executable (e.g., ChromeDriver for Chrome)
Setting Up Your Project
- Create a new Java project in your IDE.
- Add Selenium WebDriver to your project dependencies.
pom.xml
:<dependency><groupId>org.seleniumhq.selenium</groupId>
<artifactId>selenium-java</artifactId>
<version>4.10.0</version>
- Download the browser driver.
- Set the path to your driver in your test script.
Writing Your First Selenium Test Script
Let’s create a basic script that:
-
Launches the Chrome browser
-
Navigates to a website
-
Prints the page title
-
Closes the browser
Code Breakdown
chromedriver.exe
driver.get("https://www.example.com");
Navigates to the specified URL.
System.out.println("Page title is: " + driver.getTitle());
Prints the page title to the console—great for confirming your test is working.
driver.quit();
Closes the browser and ends the WebDriver session.
Common Issues to Watch For
- Driver Path Incorrect: Make sure the path to
chromedriver.exe
is valid and uses double backslashes (\\
) on Windows. - Browser Compatibility: The version of ChromeDriver should match your installed version of Chrome.
- Permissions: If you're on macOS or Linux, ensure the driver file has execution permissions.
chromedriver.exe
is valid and uses double backslashes (\\
) on Windows.Next Steps
Now that you’ve written your first test, here are a few ideas to build on:
-
Learn about different locators: ID, name, XPath, CSS selectors
-
Interact with elements:
click()
,sendKeys()
, etc. -
Use explicit waits to handle dynamic content
-
Organize tests using frameworks like JUnit or TestNG
-
Run tests in headless mode (without opening the browser window)
Final Thoughts
Here are some prerequisites for automation using Selenium, especially if you're getting started with writing test scripts:
✅ 1. Basic Knowledge Requirements
-
Programming Language Proficiency
You must be comfortable with at least one programming language that Selenium supports:-
Java (most commonly used)
-
Python
-
C#
-
JavaScript
-
Ruby
-
Kotlin
-
-
HTML and CSS
Understand the basics of HTML structure and CSS selectors to locate and interact with web elements. -
Basic JavaScript Understanding
JavaScript knowledge helps when dealing with dynamic web elements or executing custom JS using Selenium.
✅ 2. Environment Setup
-
Install Java Development Kit (JDK) (if using Java)
-
Install an IDE like:
-
IntelliJ IDEA
-
Eclipse
-
VS Code (for Python/JavaScript)
-
-
Add Selenium Libraries to your project:
-
Download Selenium WebDriver libraries (JARs)
-
Use Maven/Gradle (for Java) to manage dependencies
-
-
Install Browser Drivers:
-
ChromeDriver for Google Chrome
-
GeckoDriver for Mozilla Firefox
-
EdgeDriver for Microsoft Edge
-
✅ 3. Testing Framework (Optional but Recommended)
To structure and run your tests efficiently:
-
Java: TestNG or JUnit
-
Python: PyTest or unittest
-
C#: NUnit or MSTest
-
JavaScript: Mocha or Jasmine
✅ 4. Web Browser
At least one browser installed for test execution:
-
Chrome
-
Firefox
-
Edge
-
Safari
✅ 5. Additional Tools & Concepts (Advanced)
-
Build Tools: Maven or Gradle (for Java)
-
Version Control: Git
-
CI/CD Tools: Jenkins, GitHub Actions, etc.
-
Understanding of Wait Mechanisms: Implicit, Explicit, Fluent waits
-
Knowledge of Locators: ID, Name, XPath, CSS Selector, ClassName, etc.
Comments
Post a Comment