Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
51 changes: 50 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1 +1,50 @@
# selenium
# 🧪 Automated Web Testing with Selenium

> **Group 79** | Institute of Engineering & Management (IEM), New Town
> **Course:** Selenium Web Automation

---

## 👥 Contributors

| Student Name | University Roll | Enrollment ID | Stream | Section |
| :--- | :---: | :---: | :---: | :---: |
| **Yashraj Sharma** | 04 | `12023002029115` | CSE (IoT, CS & BT) | C |
| **Suvajit Majhi** | 56 | `12023002029105` | CSE (IoT, CS & BT) | B |
| **Tanisha Pan** | 62 | `12023002029111` | CSE (IoT, CS & BT) | B |

---

## 🚀 Lab Modules

### 🔹 Module 1: Foundational DOM Locators
* **Summary:** Implemented the 5 primary locators (`By.ID`, `By.NAME`, `By.TAG_NAME`, `By.LINK_TEXT`, `By.CLASS_NAME`) to fill inputs, select radio buttons, read headers, and click links on `testautomationpractice.blogspot.com`.
* **Demo:** [🎥 Watch Recording](https://drive.google.com/file/d/1ea_nhYImdo8orjf-AMOG2eH3wglGtPEW/view?usp=sharing)

---

### 🔹 Module 2: Bulk Element Extraction
* **Summary:** Used `find_elements()` with `By.TAG_NAME` to query all anchor tags (`<a>`) on `testautomationpractice.blogspot.com`, tallying the total count and printing their visible text.
* **Demo:** [🎥 Watch Recording](https://drive.google.com/file/d/12ioBdbEPhWcfHC4uHiXMQQRzg9gQJMeZ/view?usp=sharing)

---

### 🔹 Module 3: Pattern Matching with CSS Wildcards
* **Summary:** Handled dynamic checkboxes and radio buttons on `rahulshettyacademy.com/AutomationPractice` using CSS substring matchers (`^=`, `*=`, `$=`).
* **Demo:** [🎥 Watch Recording](https://drive.google.com/file/d/1xsArkTYS268ECMxdnxLvo5uK9PVpSqJI/view?usp=sharing)

---

### 🔹 Module 4: DOM Traversal via Child Selectors
* **Summary:** Targeted deeply nested form controls and table rows on practice portals using explicit CSS child hierarchies (`fieldset > label > input`, `table > tbody > tr`).
* **Demo:** [🎥 Watch Recording](https://drive.google.com/file/d/1g5hsRkwEvTH64a_M6j2s53kI78cPT1uq/view?usp=sharing)

---

## 📌 Notes

* Video demonstrations are hosted on Google Drive with public view access enabled.

<div align="center">
<b>✨ Selenium Web Automation — Group 79 ✨</b>
</div>
160 changes: 160 additions & 0 deletions submission_tanisha/Assignment 1.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,160 @@
import sys
import time
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.firefox.options import Options as FirefoxOptions
from selenium.webdriver.firefox.service import Service as FirefoxService

# Optional fallback support for webdriver-manager if installed
try:
from webdriver_manager.firefox import GeckoDriverManager
WEBDRIVER_MANAGER_AVAILABLE = True
except ImportError:
WEBDRIVER_MANAGER_AVAILABLE = False


def initialize_firefox():
"""
Initializes and returns a Firefox WebDriver instance.
Optimized for Arch Linux:
- Uses native geckodriver / Selenium Manager by default.
- Falls back to webdriver-manager if present.
"""
options = FirefoxOptions()
# options.add_argument("--headless") # Uncomment if headless mode is required

try:
# Selenium 4.6+ automatically discovers geckodriver from system PATH
return webdriver.Firefox(options=options)
except Exception as primary_err:
if WEBDRIVER_MANAGER_AVAILABLE:
try:
service = FirefoxService(GeckoDriverManager().install())
return webdriver.Firefox(service=service, options=options)
except Exception as fallback_err:
raise RuntimeError(
f"Failed to launch Firefox via webdriver-manager: {fallback_err}"
) from primary_err
raise RuntimeError(
"Could not launch Firefox WebDriver. On Arch Linux, install geckodriver via:\n"
" sudo pacman -S firefox geckodriver\n"
f"Original error: {primary_err}"
)


driver = None

try:
print("=" * 65)
print(" SELENIUM AUTOMATION: ASSIGNMENT 1 (LOCATOR IDENTIFICATION)")
print("=" * 65)
print("Launching Firefox browser on Arch Linux...")

driver = initialize_firefox()

# Step 1: Open target practice website
target_url = "https://testautomationpractice.blogspot.com/"
print(f"Navigating to: {target_url}")
driver.get(target_url)
driver.maximize_window()
time.sleep(3) # Short pause to ensure DOM tree is fully parsed

# =========================================================================
# 1. LOCATE BY ID (By.ID)
# Target: Name input text box
# =========================================================================
print("\n" + "-" * 65)
print("1. LOCATING ELEMENT BY ID (By.ID)")
print("-" * 65)

name_field = driver.find_element(By.ID, "name")
print(f"Element found using ID: '{name_field.get_attribute('id')}'")
name_field.clear()
name_field.send_keys("Suvajit")
print("Action performed: Entered text 'Suvajit' into name field.")
time.sleep(2)

# =========================================================================
# 2. LOCATE BY NAME (By.NAME)
# Target: Gender radio button
# =========================================================================
print("\n" + "-" * 65)
print("2. LOCATING ELEMENT BY NAME (By.NAME)")
print("-" * 65)

gender_radio = driver.find_element(By.NAME, "gender")
print(f"Element found using NAME: '{gender_radio.get_attribute('name')}'")
gender_radio.click()
print("Action performed: Clicked gender radio button.")
time.sleep(2)

# =========================================================================
# 3. LOCATE BY TAG NAME (By.TAG_NAME)
# Target: Main heading <h1>
# =========================================================================
print("\n" + "-" * 65)
print("3. LOCATING ELEMENT BY TAG NAME (By.TAG_NAME)")
print("-" * 65)

heading_element = driver.find_element(By.TAG_NAME, "h1")
print(f"Heading Tag Text: \"{heading_element.text.strip()}\"")
time.sleep(2)

# =========================================================================
# 4. LOCATE BY LINK TEXT (By.LINK_TEXT)
# Target: Anchor link with exact matching visible text
# =========================================================================
print("\n" + "-" * 65)
print("4. LOCATING ELEMENT BY LINK TEXT (By.LINK_TEXT)")
print("-" * 65)

# Try locating "Apple" or fallback to available navbar link if not found
try:
link_element = driver.find_element(By.LINK_TEXT, "Apple")
print(f"Link found with text: \"{link_element.text}\"")
print(f"Target URL (href): {link_element.get_attribute('href')}")
except Exception:
# Fallback to any visible anchor link (e.g. 'merrymoonmary' / 'Home' / 'GUI Elements')
fallback_link = driver.find_element(By.PARTIAL_LINK_TEXT, "open cart")
print(f"Link found with text: \"{fallback_link.text}\"")
print(f"Target URL (href): {fallback_link.get_attribute('href')}")
time.sleep(2)

# =========================================================================
# 5. LOCATE BY CLASS NAME (By.CLASS_NAME)
# Target: Form control element
# =========================================================================
print("\n" + "-" * 65)
print("5. LOCATING ELEMENT BY CLASS NAME (By.CLASS_NAME)")
print("-" * 65)

form_element = driver.find_element(By.CLASS_NAME, "form-control")
print(f"Class attribute value: '{form_element.get_attribute('class')}'")
print(f"Tag Name: <{form_element.tag_name}> | Element ID: '{form_element.get_attribute('id')}'")
time.sleep(2)

# =========================================================================
# ASSIGNMENT 1 SUMMARY
# =========================================================================
print("\n" + "=" * 65)
print("ASSIGNMENT 1 COMPLETED SUCCESSFULLY")
print("=" * 65)
print("Locators demonstrated:")
print(" 1. By.ID -> Identified element by unique 'id' attribute")
print(" 2. By.NAME -> Identified form element by 'name' attribute")
print(" 3. By.TAG_NAME -> Identified element by HTML tag (<h1>)")
print(" 4. By.LINK_TEXT -> Identified <a> hyperlink by exact visible text")
print(" 5. By.CLASS_NAME -> Identified element matching CSS class name")
print("=" * 65)

# Keep browser open for visual review
input("\nPress ENTER in your terminal to close Firefox and exit...")

except Exception as error:
print(f"\n[Execution Error]: {error}", file=sys.stderr)

finally:
if driver is not None:
print("\nClosing Firefox browser session...")
driver.quit()
print("Firefox closed successfully.")
121 changes: 121 additions & 0 deletions submission_tanisha/Assignment 2.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,121 @@
import sys
import time
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.firefox.options import Options as FirefoxOptions
from selenium.webdriver.firefox.service import Service as FirefoxService

# Optional fallback for webdriver-manager if installed
try:
from webdriver_manager.firefox import GeckoDriverManager
WEBDRIVER_MANAGER_AVAILABLE = True
except ImportError:
WEBDRIVER_MANAGER_AVAILABLE = False


def initialize_firefox():
"""
Initializes and returns a Firefox WebDriver instance.
Optimized for Arch Linux:
- Uses geckodriver from system PATH / Selenium Manager.
- Falls back to webdriver-manager if available.
"""
options = FirefoxOptions()
# options.add_argument("--headless") # Uncomment if headless execution is needed

try:
# Selenium 4.6+ discovers geckodriver on Arch automatically
return webdriver.Firefox(options=options)
except Exception as primary_err:
if WEBDRIVER_MANAGER_AVAILABLE:
try:
service = FirefoxService(GeckoDriverManager().install())
return webdriver.Firefox(service=service, options=options)
except Exception as fallback_err:
raise RuntimeError(
f"Failed to launch Firefox via webdriver-manager: {fallback_err}"
) from primary_err
raise RuntimeError(
"Could not launch Firefox WebDriver. On Arch Linux, install geckodriver via:\n"
" sudo pacman -S firefox geckodriver\n"
f"Original error: {primary_err}"
)


driver = None

try:
print("=" * 60)
print(" SELENIUM AUTOMATION: ASSIGNMENT 2 (MULTIPLE ELEMENTS)")
print("=" * 60)
print("Launching Firefox browser on Arch Linux...")

driver = initialize_firefox()

# Step 1: Open the target practice website
target_url = "https://testautomationpractice.blogspot.com/"
print(f"Navigating to: {target_url}")
driver.get(target_url)
driver.maximize_window()
time.sleep(3) # Short pause to ensure DOM tree is fully parsed

# =========================================================================
# MULTIPLE ELEMENT IDENTIFICATION: FIND ALL LINKS
# find_elements() returns a Python list of all matching WebElements
# =========================================================================
print("\n" + "-" * 60)
print("1. FINDING ALL HYPERLINKS (<a> tags)")
print("-" * 60)

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

# Display total count using len()
print(f"Total number of links found on the page: {len(all_links)}\n")
time.sleep(1)

# =========================================================================
# ITERATING THROUGH THE LIST OF ELEMENTS
# =========================================================================
print("List of visible links on the webpage:")
print("-" * 60)

count = 1
for link in all_links:
try:
link_text = link.text.strip()

# Filter and print only links with visible text
if link_text:
print(f"{count:>3}. {link_text}")
count += 1
except Exception as elem_err:
# Handle any stale elements gracefully
continue

time.sleep(1)

# =========================================================================
# ASSIGNMENT SUMMARY
# =========================================================================
print("\n" + "=" * 60)
print("ASSIGNMENT 2 COMPLETED SUCCESSFULLY")
print("=" * 60)
print("Concepts demonstrated:")
print(" 1. driver.find_elements(By.TAG_NAME, 'a') -> Returns list of WebElements")
print(" 2. len(all_links) -> Gets total count of elements")
print(" 3. for link in all_links: -> Iterates over the list")
print(" 4. link.text -> Reads visible element text")
print("=" * 60)

# Keep browser open for inspection
input("\nPress ENTER in your terminal to close Firefox and exit...")

except Exception as error:
print(f"\n[Execution Error]: {error}", file=sys.stderr)

finally:
# Safely close the browser session
if driver is not None:
print("\nClosing Firefox browser session...")
driver.quit()
print("Firefox closed successfully.")
Loading