├── .gitignore ├── index.html ├── index.js ├── tests.py └── README.md /.gitignore: -------------------------------------------------------------------------------- 1 | .vscode/ -------------------------------------------------------------------------------- /index.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | Index Page 4 | 5 | 6 | 7 |
8 |

0

9 | 10 | 11 |
12 | -------------------------------------------------------------------------------- /index.js: -------------------------------------------------------------------------------- 1 | document.addEventListener('DOMContentLoaded', () => { 2 | let counter = 0; 3 | 4 | document.querySelector('#increase').onclick = () => { 5 | counter++; 6 | document.querySelector('#counter').innerHTML = counter; 7 | } 8 | 9 | document.querySelector('#decrease').onclick = () => { 10 | counter--; 11 | document.querySelector('#counter').innerHTML = counter; 12 | } 13 | }) 14 | 15 | 16 | -------------------------------------------------------------------------------- /tests.py: -------------------------------------------------------------------------------- 1 | import os 2 | import pathlib 3 | import unittest 4 | 5 | from selenium import webdriver 6 | 7 | def file_uri(filename): 8 | return pathlib.Path(os.path.abspath(filename)).as_uri() 9 | 10 | driver = webdriver.Chrome('path/to/the/chrome/webdrover') 11 | 12 | class WebpageTests(unittest.TestCase): 13 | 14 | def test_title(self): 15 | driver.get(file_uri('index.html')) 16 | self.assertEqual(driver.title, 'Index Page') 17 | 18 | def test_increase(self): 19 | driver.get(file_uri('index.html')) 20 | increase = driver.find_element_by_id('increase') 21 | increase.click() 22 | self.assertEqual(int(driver.find_element_by_tag_name('h1').text), 1) 23 | 24 | def test_decrease(self): 25 | driver.get(file_uri('index.html')) 26 | decrease = driver.find_element_by_id('decrease') 27 | decrease.click() 28 | self.assertEqual(int(driver.find_element_by_tag_name('h1').text), -1) 29 | 30 | def test_multiple_increase(self): 31 | driver.get(file_uri('index.html')) 32 | increase = driver.find_element_by_id('increase') 33 | for i in range(3): 34 | increase.click() 35 | self.assertEqual(int(driver.find_element_by_tag_name('h1').text), 3) 36 | 37 | if __name__ == '__main__': 38 | unittest.main() -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Testing with Selenium 2 | 3 | ## Introduction 4 | This project demonstrates automated testing using Selenium WebDriver. It includes test scripts for web applications to ensure functionality, performance, and usability. 5 | 6 | ## Features 7 | - Automated UI testing using Selenium 8 | - Support for multiple browsers (Chrome, Firefox, Edge) 9 | - Integration with pytest for test execution 10 | - Data-driven testing using CSV or JSON files 11 | - Headless browser testing support 12 | - Logging and reporting using Allure 13 | 14 | ## Prerequisites 15 | - Python 3.x installed 16 | - Browser drivers installed (ChromeDriver, GeckoDriver, etc.) 17 | - Required Python packages installed 18 | 19 | ## Installation 20 | Clone the repository and install dependencies: 21 | 22 | ```sh 23 | # Clone the repository 24 | git clone https://github.com/your-username/testing-with-selenium.git 25 | cd testing-with-selenium 26 | 27 | # Create a virtual environment (optional but recommended) 28 | python -m venv venv 29 | source venv/bin/activate # On Windows use `venv\Scripts\activate` 30 | 31 | # Install dependencies 32 | pip install -r requirements.txt 33 | ``` 34 | 35 | ## Configuration 36 | Ensure that the appropriate web driver is placed in the system PATH or specify its location in the script. 37 | 38 | ## Running Tests 39 | Execute tests using pytest: 40 | 41 | ```sh 42 | pytest tests/ 43 | ``` 44 | 45 | Run tests with detailed output: 46 | 47 | ```sh 48 | pytest -v --html=report.html --self-contained-html 49 | ``` 50 | 51 | Run tests in headless mode: 52 | 53 | ```sh 54 | pytest --headless 55 | ``` 56 | 57 | ## Writing Your Own Tests 58 | 1. Create a new test file in the `tests/` folder. 59 | 2. Import `pytest` and `selenium`. 60 | 3. Write test functions using `pytest` format. 61 | 4. Use `setup` and `teardown` methods for driver initialization. 62 | 63 | Example: 64 | 65 | ```python 66 | import pytest 67 | from selenium import webdriver 68 | 69 | def test_google_search(): 70 | driver = webdriver.Chrome() 71 | driver.get("https://www.google.com") 72 | assert "Google" in driver.title 73 | driver.quit() 74 | ``` 75 | 76 | ## Reporting 77 | Generate an HTML report: 78 | 79 | ```sh 80 | pytest --html=report.html 81 | ``` 82 | 83 | --------------------------------------------------------------------------------