ST Notes Unit 3n4
ST Notes Unit 3n4
What Is Selenium
Evaluate the Key components of Selenium with advantages of using Selenium
Selenium is a versatile open-source framework primarily used for automating web browsers. It allows developers
and testers to write scripts that simulate user interactions, such as clicking buttons, entering text, and navigating
web pages, to ensure web applications function correctly across different browsers and platforms.
1. Selenium WebDriver: The core component that provides APIs for controlling web browsers programmatically.
It supports multiple programming languages, including Java, Python, C#, and JavaScript.
2. Selenium IDE (Integrated Development Environment): A browser extension that allows users to record and
playback user interactions with web applications, facilitating the creation of test scripts without extensive
coding knowledge.
3. Selenium Grid: A tool that enables the execution of Selenium tests on multiple machines and browsers
simultaneously, supporting parallel test execution and cross-browser testing.
1. Cross-Browser Compatibility: Selenium supports various browsers, including Chrome, Firefox, Safari, and
Edge, allowing tests to be executed across different browser environments.
2. Multi-Language Support: Test scripts can be written in several programming languages, such as Java, Python,
C#, and JavaScript, providing flexibility based on the team's expertise.
3. Integration with Testing Frameworks: Selenium can be integrated with tools like TestNG, JUnit, and PyTest for
efficient test management and reporting.
4. Open-Source and Community Support: Being open-source, Selenium has a large community that contributes
to its continuous improvement, offering a wealth of resources, tutorials, and plugins
Selenium IDE is a browser extension (available for Chrome and Firefox) that allows testers to:
• Record their interactions with a web application (like clicking buttons, filling forms)
• Playback those actions as automated tests
• Edit the test scripts using a simple UI
• Export tests to code in languages like JavaScript
Key Features:
• Record & Playback: Great for creating quick tests without needing to write code.
• Command Editor: Modify or enhance recorded commands.
• Assertions: Check if specific conditions (like element visibility) are met.
• Control Flow: Support for loops and conditional logic.
• Plugins: Extend functionality (like exporting to Selenium WebDriver scripts).
• Cross-browser support (via Selenium Grid if needed).
1. Record/Playback Functionality
• Recording: Selenium IDE can record user interactions with a web application, such as clicking buttons,
typing in text fields, and navigating between pages. This allows users to create test scripts without writing
any code.
• Playback: After recording, you can play back the interactions to ensure that the web application behaves
as expected. This is useful for regression testing.
This is the main panel in Selenium IDE where all the recorded or written test commands are displayed in a tabular
format. The test case panel is divided into three main columns:
• Command: The specific action or operation that Selenium will perform (e.g., click, type, assert).
• Target: The target element that the command operates on (e.g., an HTML element like a button, input
field, etc.).
• Value: The data associated with the command (e.g., the text to type in an input field or the URL to visit).
Selenium IDE allows you to organize multiple test cases into a test suite. A Test Suite is a collection of test cases
that can be executed together. The Test Suite panel helps you manage and execute these test cases in an organized
manner.
4. Command Toolbar
This toolbar contains essential buttons for interacting with Selenium IDE:
5. Log Panel
The log panel displays the output of the test execution, including any errors or successes. It helps with debugging
and provides a detailed report of the actions performed during the test.
6. Breakpoints
Breakpoints are markers that can be set at a specific step in a test case. When the test execution reaches a
breakpoint, it pauses, allowing the user to inspect the state of the application and test variables. This is useful for
debugging.
Selenium IDE supports the use of extensions to add additional functionality, such as connecting with other tools,
integrating with CI/CD pipelines, or adding custom commands.
Users can write custom commands or install plugins to extend the functionality of Selenium IDE. These plugins
may allow you to interact with web applications in ways that the default set of commands does not support.
You can configure several settings within Selenium IDE, such as:
• Timeouts: Define the time Selenium IDE waits for elements before throwing an error.
• Base URL: Set a base URL that all relative URLs are appended to.
After recording or creating tests, you can export your test scripts to other programming languages (like Java,
Python, JavaScript, etc.) to run with Selenium WebDriver. This makes Selenium IDE a good starting point for writing
more complex automated tests.
The command editor lets you manually add, edit, or modify commands in the test cases. This is useful if you need
to fine-tune a recorded script or add commands that weren't captured during recording.
Chrome:
Firefox:
Getting Started
After installation, click the Selenium IDE icon in your browser toolbar.
1. Start Small-Don’t try to automate everything at once. Start with simple processes and expand from there.
2. Know the Process Well First-Automating a broken or inefficient process will only speed up the mess.
Optimize it first, then automate.
3. Define Clear Goals-Know what you want to achieve with automation — saving time, reducing errors,
increasing scalability, etc.
4. Ensure Error Handling-Automation should include steps for what happens when something fails — logs,
alerts, retries, etc.
5. Maintain Transparency-Keep track of what’s automated, how it works, and what it's affecting.
Documentation and audit logs are key.
6. Security First-Automation often has elevated privileges or access — make sure it’s secure and follows best
practices (e.g., don't hard-code credentials).
7. Measure and Optimize-Use metrics to evaluate if automation is working as intended. Refine
3. Record a Test
• After naming your project, click “Record a new test in a new project.”
• Give your test a name.
• Enter the base URL of the site you want to test (e.g., https://example.com).
• The browser will open a new tab — this is where the recording begins.
5. Stop Recording
• When you're done, go back to the Selenium IDE tab and click “Stop recording.”
• You’ll now see a list of all the recorded steps.
6. Play Back the Test
• Click the Play button ( ) to run the test and watch it replicate your steps.
• You can also step through the test or debug it using the controls.
Save the project to your local machine (.side file format) for future use
If you're using Selenium (in Python, I assume), and you want to assert that certain items are on the page, you can
do it by locating elements and checking their presence or contents.
Here’s a simple example to assert that specific items are visible on the page:
Updating A Test To Verify Items On The Page Adding Selenium IDE Comments USING PYTHON CODE
Develop a procedure for updating a test to verify items such as logo, logi-button, sign-up link using Selenium
IDE commands
You're using Python with Selenium WebDriver, but you want to add Selenium IDE-style comments (i.e., descriptive
comments that explain each action, like you'd see in Selenium IDE scripts). Let’s walk through an example.
• A logo
• A “Login” button
• A “Sign Up” link
try:
# Open the target webpage
driver.get("https://example.com") # Replace with your actual URL
# Verify the logo is displayed
# Comment: assert that the logo element is present on the page
logo = driver.find_element(By.ID, "site-logo")
assert logo.is_displayed(), "Logo is not displayed"
# Verify the Login button is visible
# Comment: assert that the Login button is present and visible
login_button = driver.find_element(By.ID, "login-button")
assert login_button.is_displayed(), "Login button is not visible"
# Verify the Sign Up link exists
# Comment: assert that the Sign Up link is present on the page
sign_up_link = driver.find_element(By.LINK_TEXT, "Sign Up")
assert sign_up_link.is_displayed(), "Sign Up link is not displayed"
print("All items verified successfully!")
finally:
# Close the browser
driver.quit()
Develop a procedure for handling multiple windows in Selenium with an example code segment
Steps For Handling Multiple Windows In Selenium:
Open a new window or tab (This could be triggered by clicking a link/button that opens a new window).
Get the window handles (Each window/tab has a unique handle that allows you to switch between them).
Switch between windows using the window handles.
Perform actions on the new window/tab (e.g., clicking, reading data).
Close the window and switch back to the original window.
Example Code:
from selenium import webdriver
import time
# Set up the webdriver (this example uses Chrome)
driver = webdriver.Chrome()
# Open the website
driver.get('https://www.example.com')
# Get the window handle for the current window
main_window_handle = driver.current_window_handle
# Assume a link or button opens a new window
driver.find_element_by_link_text('Click here to open a new window').click()
# Wait for the new window to open (you may need to adjust this wait depending on the page load time)
time.sleep(2)
# Get all window handles
window_handles = driver.window_handles
# Switch to the new window (not the main window)
for handle in window_handles:
if handle != main_window_handle:
driver.switch_to.window(handle)
break
Key Points:
driver.window_handles gives a list of window handles.
driver.switch_to.window(window_handle) is used to switch to a specific window.
driver.close() closes the current window.
driver.quit() will close all open windows and terminate the WebDriver session
SOFTWARE TESTING – UNIT-4
Locating Elements By ID Using Selenium
In Selenium, locating elements by ID is one of the most common and efficient methods. You can use the
find_element_by_id method to locate an element using its ID attribute.
Develop a procedure for locating elements by ID in Selenium with an example code
Example in Python using Selenium:
from selenium import webdriver
# Initialize the webdriver (Chrome in this case)
driver = webdriver.Chrome()
# Navigate to a URL
driver.get("https://example.com")
# Locate an element by its ID
element = driver.find_element_by_id("elementID")
# Perform actions on the element (e.g., click)
element.click()
# Close the browser
driver.quit()
Steps:
1. Import the necessary modules: You'll need webdriver from selenium to interact with the browser.
2. Initialize WebDriver: In the example, Chrome is used, but you can replace webdriver.Chrome() with any
browser you want to use (like webdriver.Firefox() or webdriver.Edge()).
3. Use find_element_by_id: The method find_element_by_id("elementID") finds the element on the page
with the specified ID.
4. Perform actions on the element: After locating the element, you can perform actions such as clicking,
sending keys (typing), or extracting text.
5. Close the browser: After your interaction, driver.quit() will close the browser window.
6. Important Notes: Unique ID: The ID should be unique for each element on the page. If the ID is not unique,
the find_element_by_id method will return the first element that matches the ID.
7. Selenium 4 Update: In Selenium 4, the method to find elements has changed. Instead of using
find_element_by_id(), you now use find_element() with a By selector:
Finding IDs of elements on the page with Fire bug using Selenium
Create a procedure to find the IDs of elements on a page using Firebug and Selenium
To find the IDs of elements on a page using Firebug and Selenium, you can follow these steps:
Step 1: Inspect Element using Firebug
• Open Firebug in your browser (typically in Firefox).
• You can press F12 to open the Developer Tools in Firefox.
• In older versions of Firefox, Firebug can be installed as an add-on, but the Developer Tools are now built
into modern versions of Firefox.
• Navigate to the Elements Tab: Once Firebug is open, go to the "Inspector" or "Elements" tab. This will
allow you to inspect the HTML structure of the webpage.
• Find the Element: Hover over the elements in the page or search for the element in the HTML structure
to see its ID. Right-click the element in the browser window and click "Inspect Element" to highlight it in
the Firebug/Developer Tools panel.
• Check the ID: Once the element is highlighted in the Inspector tab, look for the id attribute in the HTML
of the selected element.
Step 2: Use Selenium to Interact with the Element
Now that you know the ID of the element, you can use Selenium to interact with it. Here’s a sample Python code
using Selenium WebDriver to find elements by their ID:
from selenium import webdriver
# Set up the WebDriver (make sure to replace with the path to your browser driver)
driver = webdriver.Chrome(executable_path="path_to_your_chromedriver")
# Open the website
driver.get('http://yourwebsite.com')
# Find the element by ID
element = driver.find_element_by_id('element_id')
# Interact with the element (example: clicking)
element.click()
# Close the browser
driver.quit()
Step 3: Using XPath or CSS Selectors (If No ID)
If the element doesn’t have an ID, you can use XPath or CSS selectors to find it.
For example:
# Find an element using XPath
element = driver.find_element_by_xpath("//button[text()='Submit']")
# Or using CSS selector
element = driver.find_element_by_css_selector("button.submit-class")
Step 4: Automate Element Identification
If you need to identify and print the IDs of all elements on the page dynamically, you can use the following code:
from selenium import webdriver
# Set up the WebDriver
driver = webdriver.Chrome(executable_path="path_to_your_chromedriver")
# Open the page
driver.get('http://yourwebsite.com')
# Find all elements on the page
elements = driver.find_elements_by_xpath('//*')
# Loop through each element and print its ID (if it has one)
for element in elements:
id_attribute = element.get_attribute('id')
if id_attribute:
print(f"Element ID: {id_attribute}")
# Close the browser
driver.quit()
Develop a python code to locate elements on a webpage based on the id attribute. Analyse the use of important
commands used.
Python Example with Selenium:
First, ensure you have the Selenium package installed:
pip install selenium
Then, you can use this code to find an element by ID:
from selenium import webdriver
# Initialize the WebDriver (make sure you have a compatible driver for your browser)
driver = webdriver.Chrome()
# Open the desired webpage
driver.get('https://www.example.com')
# Find an element by its ID
element = driver.find_element_by_id('element_id') # Replace 'element_id' with the actual ID
Explanation:
• webdriver.Chrome() initializes a Chrome browser session (you can replace this with any browser driver
you need, e.g., webdriver.Firefox() for Firefox).
• driver.get('https://www.example.com') navigates to the URL you specify.
• driver.find_element_by_id('element_id') finds the element with the specified ID. Replace 'element_id'
with the actual ID attribute of the element you want to find.
In this example, we will perform a drag-and-drop operation from one element to another.
from selenium import webdriver
from selenium.webdriver.common.action_chains import ActionChains
from selenium.webdriver.common.by import By
import time
# Initialize the WebDriver (using Chrome in this case)
driver = webdriver.Chrome(executable_path='path/to/chromedriver') # Update with your path
driver.get('https://your-website.com') # Update with your URL
# Find the source and target elements
source_element = driver.find_element(By.ID, 'source_id') # Update with the source element
target_element = driver.find_element(By.ID, 'target_id') # Update with the target element
# Create an ActionChains object
actions = ActionChains(driver)
# Perform the drag and drop operation
actions.drag_and_drop(source_element, target_element).perform()
# Pause to see the result (optional)
time.sleep(2)
# Close the browser
driver.quit()
Notes:
drag_and_drop_by_offset: This method moves the element by a specific pixel offset, defined by x and y.
drag_and_drop: This method moves the element from a source element to a target element.
You can adjust the time.sleep() value as needed or use explicit waits to handle timing better.
To find elements by name using Selenium, you can use the find_element_by_name or find_elements_by_name
methods. These methods allow you to locate an element or a list of elements based on their name attribute.
from selenium import webdriver
# Start the WebDriver (make sure you have the appropriate driver for your browser)
driver = webdriver.Chrome()
# Navigate to the desired webpage
driver.get('http://example.com')
# Find the element by its 'name' attribute
element = driver.find_element_by_name('element_name')
# Do something with the element, for example, print its text
print(element.text)
# Close the browser
driver.quit()
Explanation:
webdriver.Chrome(): Initializes the Chrome WebDriver. You can replace it with any other browser, like
webdriver.Firefox(), depending on what browser you're using.
driver.get(): Navigates to the webpage you want to interact with.
find_element_by_link_text(): Finds the anchor element (<a>) that has the specified visible link text.
click(): Clicks on the link once it's located.
To find multiple elements that match a given XPath expression, use the find_elements method:
from selenium import webdriver
from selenium.webdriver.common.by import By
# Initialize the WebDriver
driver = webdriver.Chrome()
# Open the webpage
driver.get("https://www.example.com")
# Find all elements matching the XPath
elements = driver.find_elements(By.XPATH, "//tagname[@attribute='value']")
# Iterate through the elements and perform actions
for element in elements:
print(element.text) # Example action: printing the text content
# Close the browser
driver.quit()
This script retrieves all elements that match the specified XPath and iterates through them to perform
desired actions.
Selenium WebDriver
Selenium WebDriver is a powerful tool for automating web application testing. It allows you to simulate user
interactions with a web browser, making it possible to perform automated tests on web applications. Selenium
WebDriver supports multiple programming languages such as Java, Python, C#, JavaScript, and Ruby, and it is
compatible with a wide range of browsers (Chrome, Firefox, Safari, Internet Explorer, etc.).
Language Support: Selenium WebDriver provides bindings for several programming languages, including:
• Java
• Python
• C#
• JavaScript (Node.js)
• Ruby
This allows developers to write test scripts in the programming language they are most comfortable with.
1. Direct Interaction with the Browser: Unlike Selenium’s older version, Selenium RC (Remote Control),
WebDriver communicates directly with the browser, making it faster and more reliable. It does not require
a server to execute commands, which reduces complexity and increases performance.
2. Support for Multiple Platforms: Selenium WebDriver can be used to automate tests on different operating
systems, including Windows, macOS, and Linux.
3. Dynamic Content Handling: WebDriver can interact with elements on the page that may change
dynamically (AJAX-based applications, JavaScript-heavy pages, etc.).
It can wait for elements to appear or for certain conditions to be met (e.g., element visibility or page load), making
it more suited for modern web applications.
Element Locators: WebDriver allows elements to be identified using different types of locators:
• ID
• Name
• Class Name
• Tag Name
• CSS Selectors
• XPath
Example Code:
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.common.keys import Keys
import time
Key Components:
WebDriver: The main interface for controlling the browser. In the example above, webdriver.Chrome() starts the
Chrome browser.
By Locators: Selenium uses various methods to locate elements on the page. For example:
• By.NAME
• By.ID
• By.CLASS_NAME
• By.XPATH
• By.CSS_SELECTOR
Actions: You can interact with elements such as clicking buttons, sending keystrokes, or scrolling.
Waiting: Use time.sleep() or better yet, WebDriverWait to wait for elements to be ready before interacting with
them.
Advanced Usage:
Explicit Waits:
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC