Skip to content

Commit

Permalink
Add environment-based configuration using ENV variable for different …
Browse files Browse the repository at this point in the history
…environments (test, staging, production)

To set env before run

$env:ENV="test"  # On PowerShell (Windows)
export ENV=test  # On Linux/macOS
  • Loading branch information
md-sikder committed Nov 17, 2024
1 parent c84bbfa commit 17c61a0
Show file tree
Hide file tree
Showing 5 changed files with 52 additions and 4 deletions.
7 changes: 6 additions & 1 deletion .github/workflows/ci-cd.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,13 @@ jobs:
- name: Install dependencies
run: pip install -r requirements.txt

- name: Set environment variable (Test)
run: echo "ENV=test" >> $GITHUB_ENV # Set the default environment to 'test'

- name: Run tests
run: pytest --maxfail=1 --disable-warnings -q --html=report.html
run: |
echo "Running tests in $ENV environment"
pytest --maxfail=1 --disable-warnings -q --html=report.html
- name: Upload test report
uses: actions/upload-artifact@v3
Expand Down
15 changes: 15 additions & 0 deletions config.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import yaml
import os


class Config:
# Set the default environment (can be overridden)
ENV = os.getenv('ENV', 'test') # Default to 'test' if not specified

with open("config/config.yaml", "r") as file:
config = yaml.safe_load(file)

# Dynamically select the environment
BASE_URL = config[ENV]["base_url"]
USERNAME = config[ENV]["username"]
PASSWORD = config[ENV]["password"]
14 changes: 14 additions & 0 deletions config/config.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
test:
base_url: "https://rahulshettyacademy.com/loginpagePractise/"
username: "rahulshettyacademy"
password: "learning"

staging:
base_url: " https://rahulshettyacademy.com/"
username: "staginguser"
password: "stagingpassword"

production:
base_url: "https://www.example.com"
username: "produser"
password: "prodpassword"
9 changes: 9 additions & 0 deletions logs/test_log.log
Original file line number Diff line number Diff line change
@@ -1,3 +1,12 @@
2024-11-17 13:28:41,304 - INFO - url opened successfully
2024-11-17 13:31:45,047 - INFO - url opened successfully
2024-11-17 13:31:45,172 - INFO - username and password set successfully
2024-11-17 14:13:53,035 - INFO - url opened successfully
2024-11-17 14:13:53,166 - INFO - username and password set successfully
2024-11-17 14:14:12,540 - INFO - url opened successfully
2024-11-17 14:14:12,667 - INFO - username and password set successfully
2024-11-17 14:16:00,281 - INFO - https://rahulshettyacademy.com/loginpagePractise/ url opened successfully
2024-11-17 14:16:00,413 - INFO - username and password set successfully
2024-11-17 14:22:02,722 - INFO - https://rahulshettyacademy.com/loginpagePractise/ url opened successfully
2024-11-17 14:22:02,852 - INFO - username and password set successfully
2024-11-17 14:23:55,012 - INFO - https://rahulshettyacademy.com/ url opened successfully
11 changes: 8 additions & 3 deletions tests/test_login.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
from selenium.webdriver.chrome.options import Options
from pages.login import LoginPage # Assuming the LoginPage is in login_page.py
from utils.logger import logger # Importing the logger utility
from config import Config


def test_login():
Expand All @@ -20,10 +21,14 @@ def test_login():
login_page = LoginPage(driver)

# Open the page and perform actions
driver.get("https://rahulshettyacademy.com/loginpagePractise/")
logger.info("url opened successfully")
# driver.get("https://rahulshettyacademy.com/loginpagePractise/")

login_page.login("rahulshettyacademy", "learning")
base_url = Config.BASE_URL
driver.get(base_url)
logger.info(f"{base_url} url opened successfully")

login_page.login(Config.USERNAME, Config.PASSWORD)
# login_page.login("rahulshettyacademy", "learning")
logger.info("username and password set successfully")

# Check if login was successful
Expand Down

0 comments on commit 17c61a0

Please sign in to comment.