Skip to content

Commit 739e3e6

Browse files
committed
add initialization of pages in BaseTest with fixture
1 parent e4148fa commit 739e3e6

9 files changed

Lines changed: 136 additions & 109 deletions

File tree

.vscode/launch.json

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
{
2+
"version": "0.2.0",
3+
"configurations": [
4+
{
5+
"name": "🐞 Debug Pytest",
6+
"type": "python",
7+
"request": "launch",
8+
"program": "${workspaceFolder}/venv/bin/pytest",
9+
"args": [
10+
"Ultimateqa/tests", // ← הנתיב לתיקיית הטסטים שלך
11+
"-n", "auto", // ← להרצה מקבילית אם xdist מותקן
12+
"--maxfail=3", "--tb=short"
13+
],
14+
"console": "integratedTerminal",
15+
"justMyCode": false,
16+
"envFile": "${workspaceFolder}/.env",
17+
"env": {
18+
"PYTHONPATH": "${workspaceFolder}",
19+
"PYTHONBREAKPOINT": "1" // ← מאפשר לך לשים נקודות שבירה בקוד שלך
20+
}
21+
22+
}
23+
]
24+
}

.vscode/settings.json

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
{
2+
"playwright.reuseBrowser": true,
3+
"python.pythonPath": "ForcePoint/venv/bin/python",
4+
"python.testing.pytestEnabled": true,
5+
"python.testing.pytestArgs": [
6+
"Ultimateqa/ui_tests",
7+
"api_tests",
8+
]
9+
}

Ultimateqa/pages/SubPages/section_sub_page.py

Lines changed: 10 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -5,28 +5,19 @@
55
from sub_page_buttons import SubPageButtons
66

77
class SectionSubPage:
8-
def setup(self, page: Page, goto):
9-
# Add the project path to sys.path
10-
sys.path.append('/')
11-
12-
# Load environment variables
13-
load_dotenv()
14-
15-
# Retrieve the base URL from environment variables
16-
base_url = os.getenv("BASE_URL")
17-
18-
# Navigate to the base URL
19-
goto(base_url)
20-
21-
# Initialize the sub-page buttons object
22-
self.sub_page_buttons = SubPageButtons(page)
8+
def __init__(self,page: Page):
9+
self.page=page
10+
self.name_element =self.page.locator('#et_pb_contact_name_0')
11+
self.email_element = self.page.locator('#et_pb_contact_email_0')
12+
self.message_element = self.page.locator('#et_pb_contact_message_0')
13+
self.contact_submit_button= self.page.locator('.et_pb_contact_submit')
2314

2415
def fill_contact_form(self, name: str, email: str, message: str):
2516
# Fill the contact form fields
26-
self.page.fill('#et_pb_contact_name_0', name)
27-
self.page.fill('#et_pb_contact_email_0', email)
28-
self.page.fill('#et_pb_contact_message_0', message)
17+
self.name_element.fill(name)
18+
self.email_element.fill(email)
19+
self.message_element.fill(message)
2920

3021
def submit_contact_form(self):
3122
# Submit the contact form
32-
self.page.click('.et_pb_contact_submit')
23+
self.contact_submit_button.click()

Ultimateqa/pages/SubPages/sub_page_social_media.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,3 +5,4 @@ class SubPageSocialMedia:
55
def __init__(self, page: Page):
66
self.page = page
77
self.section = page.query_selector('.et_pb_row.et_pb_row_4')
8+

Ultimateqa/ui_tests/base_test.py

Lines changed: 11 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,14 @@
1-
import sys
2-
import os
31
import pytest
4-
from dotenv import load_dotenv
5-
from playwright.sync_api import Page
2+
from Ultimateqa.pages.SubPages.sub_page_buttons import SubPageButtons
3+
from Ultimateqa.pages.SubPages.sub_page_social_media import SubPageSocialMedia
4+
from Ultimateqa.pages.SubPages.sub_page_forms import SubPageForms
65

7-
class TestBase:
8-
@pytest.fixture(scope="function")
9-
def setup(self,page: Page):
6+
class BaseTest:
7+
@pytest.fixture(autouse=True)
8+
def setup(self, page, base_url_fe):
109
self.page = page
11-
load_dotenv()
12-
self.base_url = os.getenv("BASE_URL")
13-
yield
14-
15-
# Load environment variables
16-
17-
# Retrieve the base URL from environment variables
18-
19-
# Teardown code
10+
self.base_url_fe = base_url_fe
11+
self.sub_page_buttons = SubPageButtons(self.page)
12+
self.sub_page_social_media = SubPageSocialMedia(self.page)
13+
self.sub_page_forms = SubPageForms(self.page)
14+
self.page.goto(self.base_url_fe)

Ultimateqa/ui_tests/test_fe.py

Lines changed: 44 additions & 73 deletions
Original file line numberDiff line numberDiff line change
@@ -1,75 +1,46 @@
11
import pytest
2-
from playwright.sync_api import Page
3-
from Ultimateqa.pages.SubPages.sub_page_buttons import SubPageButtons
4-
from Ultimateqa.pages.SubPages.sub_page_social_media import SubPageSocialMedia
5-
from Ultimateqa.pages.SubPages.sub_page_forms import SubPageForms
62
import Ultimateqa.ui_tests.consts as consts
7-
8-
9-
def test_count_buttons(page: Page, base_url_fe):
10-
page.goto(base_url_fe)
11-
sub_page_buttons = SubPageButtons(page)
12-
13-
count_of_buttons = sub_page_buttons.count_buttons()
14-
15-
assert count_of_buttons == consts.NUN_OF_BUTTONS_TO_FIND, \
16-
f"Found: {count_of_buttons} buttons, expected: {consts.NUN_OF_BUTTONS_TO_FIND}"
17-
18-
count_all_buttons_using_css = sub_page_buttons.all_buttons.count()
19-
assert count_all_buttons_using_css == consts.NUN_OF_BUTTONS_TO_FIND, \
20-
f"Found: {count_of_buttons} buttons, expected: {consts.NUN_OF_BUTTONS_TO_FIND}"
21-
22-
def test_count_buttons_using_css(page: Page, base_url_fe):
23-
page.goto(base_url_fe)
24-
sub_page_buttons = SubPageButtons(page)
25-
26-
count_all_buttons_using_css = sub_page_buttons.all_buttons.count()
27-
assert count_all_buttons_using_css == consts.NUN_OF_BUTTONS_TO_FIND, \
28-
f"Found: {count_all_buttons_using_css} buttons, expected: {consts.NUN_OF_BUTTONS_TO_FIND}"
29-
30-
31-
def test_verify_href_link(page: Page, base_url_fe):
32-
# load_dotenv()
33-
# base_url = os.getenv("BASE_URL")
34-
page.goto(base_url_fe)
35-
sub_page_social_media = SubPageSocialMedia(page)
36-
37-
facebook_buttons = sub_page_social_media.section.query_selector_all('a[title="Follow on Facebook"]')
38-
39-
for button in facebook_buttons:
40-
href = button.get_attribute('href')
41-
assert href == consts.SOCIAL_MEDIA_LINK, f"href is: {href}, expected: {consts.SOCIAL_MEDIA_LINK} "
42-
43-
@pytest.mark.parametrize('name, email, message, exercise_result, message_after_submit', [
44-
('Haim Moshe', 'haim.moshe@gmail.com', 'I am singer', None, 'Thanks for contacting us'),
45-
('Haim Cohen', 'adam@gmail.com', 'I am Adam', None, 'Thanks for contacting us'),
46-
('Asaf Levi', 'asaf@gmail.com', 'I am Asaf', '0', 'You entered the wrong number in captcha.'),
47-
])
48-
def test_forms(page: Page, name, email, message, exercise_result, message_after_submit, base_url_fe):
49-
#load_dotenv()
50-
#base_url = os.getenv("BASE_URL")
51-
page.goto(base_url_fe)
52-
sub_page_social_forms = SubPageForms(page)
53-
54-
sub_page_social_forms.set_name(name)
55-
sub_page_social_forms.set_email(email)
56-
sub_page_social_forms.set_message(message)
57-
58-
if exercise_result != '0':
59-
exercise_result = sub_page_social_forms.extract_and_calculate_result()
60-
61-
#page.pause()
62-
63-
sub_page_social_forms.set_result(result=str(exercise_result))
64-
sub_page_social_forms.click_submit()
65-
66-
67-
68-
if exercise_result != '0':
69-
response_message = sub_page_social_forms.get_submit_response()
70-
else:
71-
response_message = sub_page_social_forms.get_submit_fail_response()
72-
73-
assert response_message == message_after_submit
74-
75-
3+
from Ultimateqa.ui_tests.base_test import BaseTest
4+
5+
class TestUI(BaseTest):
6+
7+
def test_count_buttons(self):
8+
count_of_buttons = self.sub_page_buttons.count_buttons()
9+
assert count_of_buttons == consts.NUN_OF_BUTTONS_TO_FIND, \
10+
f"Found: {count_of_buttons} buttons, expected: {consts.NUN_OF_BUTTONS_TO_FIND}"
11+
12+
def test_count_buttons_using_css(self):
13+
count_all_buttons_using_css = self.sub_page_buttons.all_buttons.count()
14+
assert count_all_buttons_using_css == consts.NUN_OF_BUTTONS_TO_FIND, \
15+
f"Found: {count_all_buttons_using_css} buttons, expected: {consts.NUN_OF_BUTTONS_TO_FIND}"
16+
#One link of facebook is not equal
17+
# Expected 'https://www.facebook.com/Ultimateqa1/'
18+
# Actual :'https://www.facebook.com/Ultimateqa1'
19+
def test_verify_href_link(self):
20+
facebook_buttons = self.sub_page_social_media.page.query_selector_all('a[title="Follow on Facebook"]')
21+
for button in facebook_buttons:
22+
href = button.get_attribute('href')
23+
assert href == consts.SOCIAL_MEDIA_LINK, f"href is: {href}, expected: {consts.SOCIAL_MEDIA_LINK} "
24+
25+
@pytest.mark.parametrize('name, email, message, exercise_result, message_after_submit', [
26+
('Haim Moshe', 'haim.moshe@gmail.com', 'I am singer', None, 'Thanks for contacting us'),
27+
('Haim Cohen', 'adam@gmail.com', 'I am Adam', None, 'Thanks for contacting us'),
28+
('Asaf Levi', 'asaf@gmail.com', 'I am Asaf', '0', 'You entered the wrong number in captcha.'),
29+
])
30+
def test_forms(self, name, email, message, exercise_result, message_after_submit):
31+
self.sub_page_forms.set_name(name)
32+
self.sub_page_forms.set_email(email)
33+
self.sub_page_forms.set_message(message)
34+
35+
if exercise_result != '0':
36+
exercise_result = self.sub_page_forms.extract_and_calculate_result()
37+
38+
self.sub_page_forms.set_result(result=str(exercise_result))
39+
self.sub_page_forms.click_submit()
40+
41+
if exercise_result != '0':
42+
response_message = self.sub_page_forms.get_submit_response()
43+
else:
44+
response_message = self.sub_page_forms.get_submit_fail_response()
45+
46+
assert response_message == message_after_submit

api_tests/Integration_tests/test_user_alboms.py

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
1+
2+
from fields.extras import ValidationError
3+
from schema import Schema
4+
from marshmallow import Schema, fields, ValidationError
5+
16
from api_tests.globals import ApiHttpConstants
27

38

@@ -32,3 +37,24 @@ def test_user_albums_schema(api):
3237
assert isinstance(album["id"], int), f"id is not int: {album['id']}"
3338
assert isinstance(album["title"], str), f"title is not str: {album['title']}"
3439
assert isinstance(album["userId"], int), f"userId is not int: {album['userId']}"
40+
41+
class AlbumSchema(Schema):
42+
id = fields.Int(required=True)
43+
title = fields.Str(required=True)
44+
userId = fields.Int(required=True)
45+
46+
def test_user_albums_schema_with_marshmellow(api):
47+
"""
48+
Test that the user albums API returns data matching the expected schema.
49+
"""
50+
response = api.get("/albums")
51+
assert response.status_code == 200
52+
data = response.json()
53+
assert isinstance(data, list), "Response should be a list of albums"
54+
55+
schema = AlbumSchema()
56+
for album in data:
57+
try:
58+
schema.load(album)
59+
except ValidationError as err:
60+
assert False, f"Schema validation failed for album: {album}\nErrors: {err.messages}"

conftest.py

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,4 +16,11 @@ def base_url_fe():
1616
@pytest.fixture(scope='session',autouse=True)
1717
def base_url_api():
1818
load_dotenv()
19-
APIClient.BASE_URL_API = os.getenv('BASE_URL_API')
19+
APIClient.BASE_URL_API = os.getenv('BASE_URL_API')
20+
21+
@pytest.fixture(scope="session")
22+
def browser_type_launch_args():
23+
return {
24+
"headless": False,
25+
"slow_mo": 250 # מאט את הפעולות כדי שתוכל לראות מה קורה
26+
}

requirements.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,3 +18,6 @@ python-dotenv
1818
pytest-xdist
1919
requests
2020
zipp>=3.19.1 # not directly required, pinned by Snyk to avoid a vulnerability
21+
fields
22+
schema
23+
marshmallow

0 commit comments

Comments
 (0)