|
| 1 | +# 🧪 Selenium Test Automation with Pytest 🚀 |
| 2 | + |
| 3 | +## Introduction ✨ |
| 4 | + |
| 5 | +This project provides a robust and efficient solution for web application testing using Selenium and Pytest. It's designed to help you write clean, maintainable, and scalable automated tests. We leverage `webdriver-manager` for streamlined driver management, and Pytest fixtures for optimized test setup and teardown. The project also includes a GitHub Actions workflow for continuous integration. |
| 6 | + |
| 7 | +--- |
| 8 | +## 📂 Project Structure |
| 9 | + |
| 10 | +Here's how the project is organized: |
| 11 | + |
| 12 | +├── conftest.py # ⚙️ Pytest configuration and shared fixtures (e.g., setup). |
| 13 | +├── test_example.py # 🧪 Example test cases for web application testing. |
| 14 | +├── pytest.ini # 📝 Optional: Pytest configuration file. |
| 15 | +├── README.md # 📖 Project documentation (this file!). |
| 16 | +└── .github/workflows/test01.yml # 🤖 GitHub Actions workflow for CI. |
| 17 | +* **`conftest.py`**: |
| 18 | + * Contains Pytest fixtures, most importantly the `setup` fixture. |
| 19 | + * The `setup` fixture handles: |
| 20 | + * Browser initialization (e.g., Chrome, Firefox). |
| 21 | + * WebDriver management using `webdriver-manager`. |
| 22 | + * Browser teardown (closing the browser after tests). |
| 23 | + * Uses session-level scope for the `setup` fixture to efficiently manage browser instances across the entire test session. |
| 24 | +* **`test_example.py`**: |
| 25 | + * Contains example test cases using Pytest. |
| 26 | + * Demonstrates how to use the `setup` fixture to interact with a web browser. |
| 27 | + * Includes test functions for verifying page titles, and can be expanded to test other web elements and interactions. |
| 28 | +* **`pytest.ini`**: |
| 29 | + * Optional Pytest configuration file. |
| 30 | + * Useful for customizing Pytest behavior, such as: |
| 31 | + * Setting default command-line options. |
| 32 | + * Configuring test discovery. |
| 33 | + * Disabling output capture.**`.github/workflows/test01.yml`**: |
| 34 | + * GitHub Actions workflow file. |
| 35 | + * Defines a workflow named "test01" that is triggered manually. |
| 36 | + * The workflow sets up a Python environment, installs dependencies, and runs the Pytest test suite. |
| 37 | +* **`README.md`**: |
| 38 | + * This file! 📖 |
| 39 | + * Provides project documentation, usage instructions, and a description of the directory structure. |
| 40 | + |
| 41 | +--- |
| 42 | +## 🚀 Getting Started |
| 43 | + |
| 44 | +> NOTE:- Not using [webdriver-manager](https://pypi.org/project/webdriver-manager/) right now as their workflows are failing with webdriver since some months. Check [here](https://github.com/SergeyPirogov/webdriver_manager/actions/workflows/test.yml) for more info. |
| 45 | +
|
| 46 | +### Prerequisites |
| 47 | + |
| 48 | +Before you begin, ensure you have the following installed: |
| 49 | + |
| 50 | +* **Python:** 3.x is required. |
| 51 | +* **Selenium:** The Selenium WebDriver library. |
| 52 | +* **Pytest:** The Pytest testing framework. |
| 53 | +* **WebDriver Manager:** For managing browser drivers. |
| 54 | + |
| 55 | +Install the necessary packages using pip: |
| 56 | + |
| 57 | +```bash |
| 58 | +pip install selenium pytest webdriver-manager |
| 59 | +``` |
| 60 | +--- |
| 61 | +🏃♀️ Running the Tests |
| 62 | +Navigate to the project directory: |
| 63 | +```commandline |
| 64 | +cd your_project_directory |
| 65 | +``` |
| 66 | + |
| 67 | +Run all tests: |
| 68 | +To execute all tests in the project, simply use: |
| 69 | +```commandline |
| 70 | +pytest |
| 71 | +``` |
| 72 | + |
| 73 | +This command will discover and run all test functions in test_example.py (and any other files following Pytest's naming conventions), utilizing the setup fixture defined in conftest.py. |
| 74 | +See print statements (Disable output capturing): |
| 75 | +By default, Pytest captures standard output (stdout). To see print statements in your console, use the **-s** or **--capture=no flag**: |
| 76 | + |
| 77 | +```commandline |
| 78 | +pytest -s |
| 79 | +``` |
| 80 | + |
| 81 | +or |
| 82 | +```commandline |
| 83 | +pytest --capture=no |
| 84 | +``` |
| 85 | + |
| 86 | +Run a single test file: To run only the tests in a specific file: |
| 87 | +```commandline |
| 88 | +pytest test_example.py |
| 89 | +``` |
| 90 | + |
| 91 | +Run a specific test function: To run a single test function: |
| 92 | +```commandline |
| 93 | +pytest test_example.py::TestExample::test_title |
| 94 | +``` |
| 95 | +--- |
| 96 | +🤖 Continuous Integration with GitHub Actions |
| 97 | +The project includes a GitHub Actions workflow _(.github/workflows/test01.yml)_ for _continuous integration (CI)_. This workflow automates the testing process whenever you make changes to your repository. |
| 98 | + |
| 99 | +Workflow Details |
| 100 | +* Name: test01 |
| 101 | +* Trigger: The workflow is triggered manually using workflow_dispatch. |
| 102 | +* Jobs: The workflow has a single job - test01: |
| 103 | +* Runs on Ubuntu. |
| 104 | + |
| 105 | +Steps: |
| 106 | +* Checks out the repository code using actions/checkout@v3. |
| 107 | +* Sets up a Python 3.10 environment using actions/setup-python@v4. |
| 108 | +* Installs project dependencies from requirements.txt using pip. |
| 109 | +* Runs the Pytest test suite using pytest -rA. The -rA flag ensures that all test results are printed. |
| 110 | + |
| 111 | +How to Use the Workflow |
| 112 | +* Commit the workflow file: Ensure that the .github/workflows/test01.yml file is committed to your GitHub repository. |
| 113 | +* Run the workflow: |
| 114 | +* Go to your repository on GitHub. |
| 115 | +* Click on the "Actions" tab. |
| 116 | +* Find the "test01" workflow in the list on the left. |
| 117 | +* Click the "Run workflow" button. |
| 118 | +* GitHub Actions will then execute the workflow, running your Pytest tests in a virtual environment. The results of the test run will be displayed in the GitHub Actions console. |
| 119 | + |
| 120 | +--- |
| 121 | +🎯 Key Concepts |
| 122 | +Pytest Fixtures: |
| 123 | +* We use Pytest fixtures to manage the setup and teardown of the Selenium WebDriver. |
| 124 | +* The setup fixture ensures that a browser instance is initialized before the tests run and is closed after the tests finish. |
| 125 | +* Fixtures promote code reusability and maintainability. |
| 126 | +* Using scope="session" in the setup fixture optimizes browser usage, creating the browser instance only once per test session. |
| 127 | +* Selenium WebDriver: |
| 128 | +* Selenium WebDriver is used to automate web browser interactions. |
| 129 | +* It allows us to control the browser programmatically, simulating user actions like navigating to pages, clicking elements, and entering text. |
| 130 | +* WebDriver Manager: |
| 131 | +* webdriver-manager simplifies the process of managing browser drivers (e.g., chromedriver, geckodriver). |
| 132 | +* It automatically downloads the correct driver version for your browser, eliminating the need for manual driver installation and updates. |
| 133 | +* Test Assertions: |
| 134 | +* Test functions include assertions (e.g., assert ...) to verify that the actual results match the expected outcomes. |
| 135 | +* Assertions are crucial for determining whether a test has passed or failed. |
| 136 | +* Modular Design: |
| 137 | +* The project is structured into separate files (conftest.py, |
| 138 | + |
| 139 | +--- |
| 140 | + |
| 141 | +# Author |
| 142 | + |
| 143 | +[Nitin Kumar](https://linkedin.com/in/nitin30kumar/) |
0 commit comments