-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconftest.py
More file actions
185 lines (151 loc) · 5.08 KB
/
conftest.py
File metadata and controls
185 lines (151 loc) · 5.08 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
import datetime
import json
import logging
import pdb
import pytest
import requests
from playwright.sync_api import APIRequestContext, Playwright
from utils.data_loader import get_config, get_credentials
def pytest_addoption(parser):
parser.addoption(
"--env",
action="store",
default="stage", # Default to dev if no env is passed
help="Environment to run tests against: dev, qa, stage, prod"
)
@pytest.fixture(scope="session")
def config(request):
env = request.config.getoption("--env")
with open("data/config.json") as f:
full_config = json.load(f)
if env not in full_config["environments"]:
raise ValueError(f"Invalid environment: {env}")
env_config = full_config["environments"][env]
shared_config = full_config["shared"]
# Merge shared and environment-specific settings
merged_config = {
"env": env,
"base_url": env_config["base_url"],
**shared_config
}
return merged_config
@pytest.fixture(scope="session")
def api_context(playwright: Playwright):
context = playwright.request.new_context(
extra_http_headers={
"Content-Type": "application/json"
}
)
yield context
context.dispose()
@pytest.fixture(scope="session")
def user_obj(api_context: APIRequestContext, config):
credentials = get_credentials()["user_credentials"]["valid_user"]
login_url = config["base_url"] + "auth/login"
payload = {
"userEmail": credentials["userEmail"],
"userPassword": credentials["userPassword"]
}
headers = config["headers"]
response = api_context.post(
login_url,
data=json.dumps(payload),
headers=headers
)
assert response.ok, f"Login failed: {response.status} - {response.text()}"
# pdb.set_trace()
token = response.json().get("token")
assert token, "Token not found in login response"
return response.json()
@pytest.fixture(scope="module")
def latest_order_id(user_obj, config):
headers = {'Authorization': user_obj["token"]}
url = f"{config['base_url']}order/get-orders-for-customer/{user_obj['userId']}"
order_response = requests.get(url, headers=headers)
assert order_response.ok, f"Failed to get order: {order_response.status_code}"
assert order_response.json()['message'] == "Orders fetched for customer Successfully"
return order_response.json()['data'][0]['_id']
@pytest.fixture(scope="session")
def first_product():
return {}
@pytest.hookimpl(tryfirst=True)
def pytest_html_report_title(report):
report.title = "Custom API Test Report"
report.subtitle = "Playwright API Testing with a BDD Framework"
def pytest_metadata(metadata):
metadata.clear()
metadata["Project Name"] = "API Testing"
metadata["Tester"] = "Haris"
metadata["Environment"] = "Staging"
def pytest_configure(config):
now = datetime.datetime.now().strftime("%Y-%m-%d_%H-%M-%S")
report_name = f"report_{now}.html"
config.option.htmlpath = f"reports/{report_name}"
def pytest_html_results_summary(prefix, summary, postfix):
# Add custom CSS styles to the HTML report
prefix.append(
'''
<style>
/* Custom background color */
body {
background-color: #f4f4f9;
font-family: Arial, sans-serif;
}
/* Modify the report title */
h1 {
color: #1a73e8;
text-align: center;
font-size: 2em;
}
/* Hide the default "Report generated on" section */
p:contains("Report generated on") {
# display: none;
}
/* Style for test summary */
.summary td {
padding: 10px;
text-align: center;
}
/* Add a border to the table */
table {
border-collapse: collapse;
width: 100%;
}
table, th, td {
border: 1px solid #ccc;
}
th {
background-color: #f0f0f0;
color: #333;
font-weight: bold;
}
/* Style for passed tests */
.passed {
background-color: #d4edda;
color: #155724;
}
/* Style for failed tests */
.failed {
background-color: #f8d7da;
color: #721c24;
}
/* Customize links in the report */
a {
color: #1a73e8;
text-decoration: none;
}
a:hover {
text-decoration: underline;
}
</style>
<script>
// JavaScript to hide the "Report generated on" section
window.onload = function() {
var reportText = document.querySelector("p");
if (reportText && reportText.innerHTML.includes("Report generated on")) {
reportText.style.display = "none";
}
}
</script>
'''
)