Table of Contents
3.1 Introduction to Selenium
Selenium is an open-source automation tool that is primarily used for automating web applications for testing purposes. It allows testers and developers to interact with web elements just as users do in browsers, such as clicking buttons, filling out forms, or navigating between pages. Selenium supports various programming languages such as Java, Python, C#, JavaScript, etc. Among these, Java is one of the most commonly used languages due to its robust ecosystem and vast community support.
Selenium has four main components:
- IDE: A Chrome and Firefox add-on used for recording and playing back scripts.
- Grid: A tool that enables parallel test execution across different browsers and machines.
- WebDriver: The most important component, it communicates directly with the browser to automate tasks.
- RC (Remote Control): Deprecated, and replaced by WebDriver.
Selenium WebDriver, being more efficient and modern than the earlier Selenium RC, has become the industry standard for automated testing of web applications.
3.2 WebDriver Architecture
The Selenium WebDriver architecture is composed of multiple layers, each responsible for carrying out specific tasks. The key components are:
- Selenium Client Libraries: These are language-specific bindings that enable interaction with the Selenium API. In our case, we will use the Java bindings.
- JSON Wire Protocol: This acts as the bridge between client libraries and the browser drivers. It communicates via RESTful API using JSON format.
- Browser Drivers: Each browser (Chrome, Firefox, Edge, Safari) has its own WebDriver (e.g., ChromeDriver, GeckoDriver for Firefox). These drivers are responsible for controlling the browsers based on the commands they receive from the JSON Wire Protocol.
- Browsers: The WebDriver interacts directly with the browser (e.g., Chrome, Firefox) to automate web actions.
When you run a test, the Selenium script sends the commands to the browser driver, which processes these commands through the JSON Wire Protocol. The browser driver then executes the actions on the browser and sends back the results.
3.3 Setting Up Selenium WebDriver Environment
To get started with WebDriver in Java using Eclipse, follow these steps:
- Install Java: Ensure you have JDK installed on your machine. You can download it from Oracle’s official website.
- Install Eclipse IDE: Download and install Eclipse IDE for Java Developers from the Eclipse website.
- Download Selenium WebDriver JARs: Visit Selenium’s official website and download the necessary JAR files for Selenium WebDriver.
- Set Up a New Project in Eclipse:
- Open Eclipse.
- Create a new Java project.
- Add the Selenium WebDriver JAR files to the project’s build path.
- Download Browser Drivers:
- For Chrome, download chromedriver.exe from ChromeDriver downloads.
- For Firefox, download geckodriver.exe from GeckoDriver downloads.
3.4 Locators
Locators are used to find and interact with web elements (like buttons, input fields, etc.) on a web page. Locators are essential to identify elements in a web page’s Document Object Model (DOM). Selenium provides several ways to locate elements, each with its use case depending on the structure and complexity of the page.
Locating web elements in Selenium is a crucial step for interacting with the web application. Selenium WebDriver provides various ways to find elements:
- ID
- Description: Locates an element by its id attribute, which is unique for every element on the page.
- Usage: driver.findElement(By.id(“element_id”))
- Example:
driver.findElement(By.id("username")).sendKeys("myUsername");
- Name
- Description: Locates an element by its name attribute.
- Usage: driver.findElement(By.name(“element_name”))
- Example:
driver.findElement(By.name("email")).sendKeys("example@example.com");
- Class Name
- Description: Locates elements by their class attribute. This can be used when multiple elements share the same class.
- Usage: driver.findElement(By.className(“element_class”))
- Example:
driver.findElement(By.className("submit-button")).click();
- Tag Name
- Description: Locates elements by their tag name (like input, button, div, etc.).
- Usage: driver.findElement(By.tagName(“element_tag”))
- Example:
driver.findElement(By.tagName("a")).click(); // Clicks the first link on the page
- Link Text
- Description: Locates a hyperlink (<a> tag) element by its exact visible text.
- Usage: driver.findElement(By.linkText(“exact_link_text”))
- Example:
driver.findElement(By.linkText("Login")).click();
- Partial Link Text
- Description: Locates a hyperlink by a partial match of its visible text.
- Usage: driver.findElement(By.partialLinkText(“partial_text”))
- Example:
driver.findElement(By.partialLinkText("Sign")).click(); // Clicks a link that contains "Sign" in its text
- CSS Selector
- Description: Locates elements using CSS selectors, offering flexibility and precision by targeting classes, IDs, attributes, etc.
- Usage: driver.findElement(By.cssSelector(“css_selector”))
- Example:
driver.findElement(By.cssSelector(".form .input-field")).sendKeys("text");
- XPath
- Description: XPath is a powerful way to locate elements using their XML path. It supports both relative and absolute paths.
- Usage: driver.findElement(By.xpath(“xpath_expression”))
- Example:
driver.findElement(By.xpath("//input[@name='password']")).sendKeys("myPassword");
3.5 Handling Web Elements
3.5.1 Text Boxes, Buttons, Checkboxes, and Radio Buttons
You can interact with these elements by using simple WebDriver commands.
- Text Boxes: Enter text using sendKeys().
driver.findElement(By.id("textboxID")).sendKeys("Input text");
- Buttons: Click buttons using click().
driver.findElement(By.id("buttonID")).click();
- Checkboxes & Radio Buttons: Use click() to check or uncheck.
driver.findElement(By.id("checkboxID")).click();
3.5.2 Dropdowns and Select Elements
For handling dropdowns, Selenium provides the Select class to interact with <select> tags.
Select dropdown = new Select(driver.findElement(By.id("dropdownID")));
dropdown.selectByVisibleText("OptionText");
3.5.3 Frames and Windows
To interact with elements inside frames or different browser windows, WebDriver provides methods to switch between them.
- Frames: Use switchTo().frame() to interact with elements inside a frame.
driver.switchTo().frame("frameName");
- Windows: To switch between browser windows, use switchTo().window();
driver.switchTo().window("windowName");
3.6 Working with WebDriver Commands
3.6.1 Browser Commands
- Open a URL:
driver.get("https://www.example.com");
- Get the Title of the Page:
String title = driver.getTitle();
- Close the Browser:
driver.close(); OR driver.quit();
3.6.2 Navigation Commands
Selenium allows you to navigate within the browser’s history.
- Back:
driver.navigate().back();
- Forward:
driver.navigate().forward();
- Refresh:
driver.navigate().refresh();
3.6.3 Waits in Selenium (Implicit and Explicit Waits)
Selenium provides waiting mechanisms to synchronize tests with the state of the web page.
- Implicit Wait: Applies globally to all elements.
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
- Explicit Wait: Waits for a specific condition to occur.
WebDriverWait wait = new WebDriverWait(driver, 10);
wait.until(ExpectedConditions.visibilityOfElementLocated(By.id("elementID")));
3.7 Handling Alerts and Pop-ups
It provides methods to handle browser alerts and pop-ups.
- Handling Alerts:
Alert alert = driver.switchTo().alert();
alert.accept(); // To accept the alert
alert.dismiss(); // To dismiss the alert
3.8 Taking Screenshots
Taking screenshots during test execution is useful for debugging. You can capture screenshots using TakesScreenshot.
File screenshot = ((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE);
FileUtils.copyFile(screenshot, new File("path/to/save/screenshot.png"));
3.9 Automation of Web Forms
Web forms can be automated using WebDriver commands. You can fill out forms, click the submit buttons, and validate form submissions. For example:
driver.findElement(By.name("username")).sendKeys("your_username");
driver.findElement(By.name("password")).sendKeys("your_password");
driver.findElement(By.name("submit")).click();
3.10 Debugging and Logging in Selenium Scripts
Debugging is essential to ensure your scripts work as expected. In Eclipse, you can set breakpoints and step through your code to inspect variables and logic.
- Logging: Use libraries like Log4j or Java’s built-in logging to log steps in your automation process. This helps in tracking the flow of execution and identifying issues.
Example:
Logger logger = Logger.getLogger("MyLogger");
logger.info("Starting the test case execution");
Conclusion
Selenium WebDriver is a powerful tool for automating web applications, and when combined with Java and Eclipse, it provides a robust framework for testing. By following this guide, you can now set up a Selenium environment, locate web elements, interact with them, handle complex scenarios like alerts, pop-ups, and multiple windows, and automate forms with ease.
To learn more about Advance Concept SDET