0% found this document useful (0 votes)
12 views9 pages

st mod 5

Uploaded by

keerthi.is21
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
12 views9 pages

st mod 5

Uploaded by

keerthi.is21
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 9

1)Analyze how Selenium web driver works and also show how it interacts with a browser and executes

the
command.

• Selenium WebDriver is a tool that automates web browsers.


• It acts like a person using a browser, performing tasks like:

• Opening websites
• Clicking links
• Filling forms
• Navigating pages

• WebDriver can also examine web pages to check elements and make decisions based on what it sees.
• Its main use is for automated testing of websites.
• Earlier, regression tests required manual effort by developers or testers to:

• Run scripts manually


• Gather and compile reports manually

• Manual testing was time-consuming and expensive.


• With WebDriver, you can automate script execution and get reports automatically.
• Once set up, running tests repeatedly is cost-effective and quick.

How WebDriver works

WebDriver works in all major browsers and with all major programming languages.

WebDriver has several interacting components:


1. A web browser.
2. A plugin or extension to the browser that lives inside the browser, which itself contains a server that implements the
WebDriver JSON API.
3. A language binding (in our case Java) that makes HTTP requests to that API
When you start a program using WebDriver, it opens the browser.
The browser then starts the WebDriver plugin.
You can send commands (requests) to the plugin to perform actions, like:
• Clicking links
• Typing text
The WebDriver plugin works using the JSON API.
Plugins have been created for all major browsers because they only need to support the JSON API.
To use a browser with WebDriver, you need a client that supports the JSON protocol.
This ensures WebDriver is compatible with:
• All major browsers
• All major programming languages
Why we should Choose Web driver
Runs Anywhere:
• WebDriver can run browsers both locally and remotely with minimal setup changes.
Strong Support:
• Major browser vendors like Firefox and Chrome actively contribute to its development.
Realistic Behavior:
• WebDriver mimics real user actions by using native events, making it more accurate and stable.
Open Source:
• It is free and backed by a strong, helpful community.
Wide Compatibility:
• Works on all major operating systems, including macOS, Windows, and Linux.
• Also supports mobile platforms like Android and iOS.
Future-Proof:
• WebDriver is becoming a W3C standard, ensuring long-term support.
Improved Features:
• Solves problems faced in Selenium 1.0, like file uploads and handling pop-ups.
Concise Syntax:
• Writing code with WebDriver is faster and simpler compared to Selenium 1.0.
2) Illustrate the history and evolution of Selenium also highlight the key milestones and their impact on the
testing community.
Early Development:
1. Selenium is a suite of web testing tools, including:
o Selenium IDE
o Selenium RC
o Selenium WebDriver
o Selenium Grid
2. The first version, Selenium Core, was created by Jason Huggins at ThoughtWorks’ Chicago office.
3. Selenium Core was designed to simulate user actions in Internet Explorer.
4. Unlike other Record/Replay tools, Selenium Core didn’t need extra GUI tools. It only required Java, which
most developers already had.
Selenium IDE:
5. Shinya Kasatani developed Selenium IDE, a Firefox plugin built on Selenium Core.
6. Selenium IDE allowed users to record and replay browser sessions with a graphical interface.
7. Since it was free, it became popular among non-programmers like QAs and business analysts.
Evolution to Selenium RC and Grid:
8. Selenium Core evolved into Selenium RC (Remote Control), allowing automated tests to run on various
machines.
9. Selenium Grid was introduced to run tests on multiple machines simultaneously.
Introduction of Selenium WebDriver:
10. Selenium WebDriver was created by Simon Stewart at ThoughtWorks.
11. It was first presented at Google Test Automation Conference and can be viewed online here.
12. In 2008, Selenium WebDriver was merged into the suite as Selenium 2.0, replacing Selenium RC.
13. WebDriver became the most popular tool because it provides a standardized interface for various browsers.
Deprecation of Selenium RC:
14. Selenium RC was deprecated, and developers provided migration guides to help users switch to WebDriver.
Modern Selenium:
15. Today, “Selenium” typically refers to Selenium WebDriver, the most widely used tool in the suite.
Why the name "Selenium"?
16. Jason Huggins jokingly named it after a competitor called Mercury, saying selenium supplements cure
mercury poisoning.
3) Outline how to locate elements in the web page using CSS selectors and XPath.

CSS Selectors
• CSS Selectors are powerful and concise for locating elements using CSS rules.
• Syntax is inspired by CSS styling rules.
Examples
1. By Tag and ID:
python
Copy code
element = driver.find_element_by_css_selector("input#username")
o Locates an <input> element with the ID username.
2. By Tag and Class:
python
Copy code
element = driver.find_element_by_css_selector("div.form-control")
o Locates a <div> element with the class form-control.
3. By Attribute:
python
Copy code
element = driver.find_element_by_css_selector("input[type='text']")
o Locates an <input> element with the type attribute equal to text.
4. By Hierarchy:
python
Copy code
element = driver.find_element_by_css_selector("div > span")
o Locates a <span> element that is a direct child of a <div>.
Advantages
• Fast and easy to read.
• Supported directly by browsers.
• Shorter syntax compared to XPath.
Limitations
• Cannot traverse back up the DOM (no parent navigation).
XPath

• XPath is a query language for XML-like structures, used to locate elements with precision in HTML.

Examples

1. By Absolute Path:

element = driver.find_element_by_xpath("/html/body/div/form/input")

o Locates the element by traversing the complete DOM hierarchy (not recommended).
2. By Relative Path:

element = driver.find_element_by_xpath("//input[@id='username']")

o Locates an <input> element with the id attribute username.


3. Using Logical Operators:

element = driver.find_element_by_xpath("//input[@type='text' and @name='email']")

o Locates an <input> element with type='text' and name='email'.


4. By Text Content:

element = driver.find_element_by_xpath("//button[text()='Submit']")

o Locates a <button> element containing the text Submit.

5. Using Parent-Child Relationship:

element = driver.find_element_by_xpath("//div[@class='form-group']/input")

o Locates an <input> child element inside a <div> with the class form-group.

4) Illustrate different strategies for Locating Elements on the web page using Selenium web driver
with an examples.

Here’s a clear and structured illustration of various strategies for locating elements on a webpage using
Selenium WebDriver, with examples:

1. By ID

• Locates an element using its id attribute.


• Best for: Unique and fast identification of elements.

Example:

element = driver.find_element_by_id("username")

2. By Class Name
• Targets elements using their class attribute.
• Best for: Elements sharing common styles.

Example:

element = driver.find_element_by_class_name("form-control")

3. By Name

• Locates elements using the name attribute.


• Best for: Form inputs and elements with descriptive name attributes.

Example:

element = driver.find_element_by_name("email")

4. By Tag Name

• Finds elements by their HTML tag.


• Best for: Locating multiple elements like <div>, <a>, or <input>.

Example:

element = driver.find_element_by_tag_name("button")

5. By Link Text

• Locates anchor (<a>) elements using the visible text in the link.
• Best for: Navigation links.

Example:

element = driver.find_element_by_link_text("Home")

6. By Partial Link Text

• Similar to Link Text, but matches only part of the text in the link.
• Best for: Links with dynamic or lengthy text.

Example:

element = driver.find_element_by_partial_link_text("Learn")

7. By CSS Selector
• Uses CSS rules to locate elements.
• Best for: Precise and concise element location.

Examples:

• Locate by ID:

element = driver.find_element_by_css_selector("#username")

• Locate by class:

element = driver.find_element_by_css_selector(".form-control")

• Locate by attribute:

element = driver.find_element_by_css_selector("input[type='email']")

8. By XPath

• Uses XPath expressions to locate elements.


• Best for: Complex DOM structures and hierarchical navigation.

Examples:

• Locate by attribute:

element = driver.find_element_by_xpath("//input[@id='username']")

• Locate by text:

element = driver.find_element_by_xpath("//button[text()='Submit']")

• Locate by parent-child relationship:

element = driver.find_element_by_xpath("//div[@class='form-group']/input")

9. By DOM (JavaScript)

• Executes JavaScript to locate elements.


• Best for: Custom scenarios where other methods fail.

Example:

element = driver.execute_script("return document.querySelector('input#username')")


5)Demonstrate the overview of webdriiver and its unique features compared to tools like selenium
ide.

6) Write and test a program to provide total number of objects present available on

the page SELENIUM IDE:

import unittest

from selenium import webdriver

from selenium.webdriver.common.by import By

class Count(unittest.TestCase):

def setUp(self):

# Set up the WebDriver for Chrome

self.driver = webdriver.Chrome() # Make sure to have the ChromeDriver

installed

self.driver.get("http://www.google.co.in")

self.driver.maximize_window()

def test_count_elements(self):

driver = self.driver

# Get all links on the page

links = driver.find_elements(By.TAG_NAME, "a")

print(f"TOTAL NO OF LINKS = {len(links)}")

# Get all buttons on the page

buttons = driver.find_elements(By.TAG_NAME, "button")

print(f"TOTAL NO OF BUTTONS = {len(buttons)}")


# Get all input fields on the page

input_fields = driver.find_elements(By.TAG_NAME, "input")

print(f"TOTAL NO OF INPUT FIELDS = {len(input_fields)}")

def tearDown(self):

# Stop the WebDriver

self.driver.quit()

if __name__ == "__main__":

unittest.main()

You might also like