In this section, we will guide you through writing your first Selenium script. This script will automate a simple task in a web browser, allowing you to see Selenium in action. By the end of this lesson, you will have a basic understanding of how to set up and execute a Selenium test.
Key Concepts
- Selenium WebDriver: A tool for automating web application testing, allowing you to control a browser programmatically.
- WebDriver Interface: The main interface to control the browser.
- Basic Script Structure: Understanding the components of a Selenium script.
Prerequisites
Before you start, ensure you have:
- Installed Selenium WebDriver (covered in the previous section).
- A basic understanding of Java programming (or the language you are using with Selenium).
Writing Your First Selenium Script
Step 1: Set Up Your Project
- Create a New Project: Open your IDE (e.g., IntelliJ IDEA, Eclipse) and create a new Java project.
- Add Selenium Libraries: Ensure that the Selenium WebDriver libraries are added to your project. This can be done by adding the Selenium JAR files to your project's build path.
Step 2: Write the Script
Below is a simple Selenium script that opens a browser, navigates to a website, and prints the page title.
import org.openqa.selenium.WebDriver; import org.openqa.selenium.chrome.ChromeDriver; public class FirstSeleniumScript { public static void main(String[] args) { // Set the path to the chromedriver executable System.setProperty("webdriver.chrome.driver", "path/to/chromedriver"); // Initialize a new ChromeDriver instance WebDriver driver = new ChromeDriver(); // Navigate to a website driver.get("https://www.example.com"); // Get the title of the page String pageTitle = driver.getTitle(); // Print the title of the page System.out.println("Page title is: " + pageTitle); // Close the browser driver.quit(); } }
Explanation of the Code
- System.setProperty: This line sets the system property for the ChromeDriver, which is necessary to control the Chrome browser.
- WebDriver driver = new ChromeDriver(): This initializes a new instance of the ChromeDriver, which opens a new browser window.
- driver.get("https://www.example.com"): This command navigates the browser to the specified URL.
- driver.getTitle(): This retrieves the title of the current page.
- driver.quit(): This closes the browser and ends the WebDriver session.
Step 3: Run the Script
- Compile and Run: Compile your Java program and run it. You should see a browser window open, navigate to the specified URL, and then close after printing the page title to the console.
Practical Exercise
Exercise: Modify the script to navigate to a different website and print both the page title and the current URL.
Solution:
import org.openqa.selenium.WebDriver; import org.openqa.selenium.chrome.ChromeDriver; public class FirstSeleniumScript { public static void main(String[] args) { System.setProperty("webdriver.chrome.driver", "path/to/chromedriver"); WebDriver driver = new ChromeDriver(); // Navigate to a different website driver.get("https://www.google.com"); // Get the title of the page String pageTitle = driver.getTitle(); // Get the current URL String currentUrl = driver.getCurrentUrl(); // Print the title and URL of the page System.out.println("Page title is: " + pageTitle); System.out.println("Current URL is: " + currentUrl); driver.quit(); } }
Common Mistakes and Tips
- Incorrect Path to WebDriver: Ensure the path to the WebDriver executable is correct.
- Browser Compatibility: Make sure the WebDriver version matches the browser version.
- Driver Not Closed: Always use
driver.quit()
to close the browser and free up resources.
Conclusion
Congratulations! You've written and executed your first Selenium script. This foundational knowledge will be crucial as you progress to more complex automation tasks. In the next section, we will delve deeper into understanding the WebDriver interface and its capabilities.
Test Automation with Selenium
Module 1: Introduction to Test Automation
- What is Test Automation?
- Benefits of Test Automation
- Overview of Selenium
- Setting Up Your Environment
Module 2: Getting Started with Selenium
- Introduction to Selenium WebDriver
- Installing Selenium WebDriver
- First Selenium Script
- Understanding WebDriver Interface
Module 3: Locating Web Elements
- Introduction to Locators
- Using ID and Name Locators
- XPath and CSS Selectors
- Advanced Locator Strategies
Module 4: Interacting with Web Elements
- Performing Actions on Web Elements
- Handling Dropdowns and Checkboxes
- Working with Alerts and Pop-ups
- Managing Browser Windows and Frames
Module 5: Synchronization in Selenium
Module 6: Test Frameworks and Selenium
- Introduction to TestNG
- Setting Up TestNG with Selenium
- Creating TestNG Test Cases
- Data-Driven Testing with TestNG
Module 7: Advanced Selenium Concepts
Module 8: Selenium Grid and Parallel Testing
- Introduction to Selenium Grid
- Setting Up Selenium Grid
- Running Tests in Parallel
- Cross-Browser Testing
Module 9: Continuous Integration and Selenium
- Introduction to Continuous Integration
- Integrating Selenium with Jenkins
- Automating Test Execution
- Reporting and Logging