Skip to content

Commit e2d99f6

Browse files
committed
First commit
0 parents  commit e2d99f6

File tree

4 files changed

+197
-0
lines changed

4 files changed

+197
-0
lines changed

.github/workflows/test01.yml

+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
# .github/workflows/test01.yaml
2+
name: test01
3+
on:
4+
workflow_dispatch:
5+
jobs:
6+
test01:
7+
runs-on: ubuntu-latest
8+
steps:
9+
- name: Check out this repo
10+
uses: actions/checkout@v3
11+
#Setup Python
12+
- name: Set up Python
13+
uses: actions/setup-python@v4
14+
with:
15+
python-version: '3.10'
16+
# - name: Install chrome
17+
# run: wget https://dl.google.com/linux/direct/google-chrome-stable_current_amd64.deb
18+
- name: Install the necessary packages
19+
run: |
20+
python -m pip install --upgrade pip
21+
if [ -f requirements.txt ]; then pip install -r requirements.txt; fi
22+
- name: Run the PytTest script
23+
run: pytest -rA
24+

README.MD

+143
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,143 @@
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/)

conftest.py

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
import pytest
2+
3+
from selenium.webdriver.chrome.options import Options
4+
from selenium import webdriver
5+
from webdriver_manager.chrome import ChromeDriverManager
6+
from selenium.webdriver.chrome.service import Service as ChromeService
7+
8+
9+
@pytest.fixture
10+
def setup():
11+
option = Options()
12+
option.add_argument('--headless')
13+
14+
driver = webdriver.Chrome(options=option)
15+
# driver = webdriver.Chrome(service=ChromeService(ChromeDriverManager().install()), options=option)
16+
17+
yield driver
18+
driver.quit()

test_nitsanon.py

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
import pytest
2+
3+
4+
@pytest.mark.usefixtures("setup")
5+
class TestExample:
6+
def test_title(self, setup):
7+
setup.get("http://nitsanon.epizy.com/?i=1")
8+
assert setup.title == "NITSANON", "Wrong title detected"
9+
10+
def test_title_myGithub(self, setup):
11+
setup.get("https://github.com/nitinkumar30")
12+
print("===========\n\n", setup.title, "\n\n===========")

0 commit comments

Comments
 (0)