Skip to content

Commit ac83917

Browse files
committed
add running the api testson github actions
add api framework code add fixture for reading env api url update fe url call add requests
1 parent 1bf4113 commit ac83917

File tree

8 files changed

+86
-28
lines changed

8 files changed

+86
-28
lines changed

.github/workflows/github_actions_tests.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ jobs:
4040
run: |
4141
echo "BASE_URL=${BASE_URL}" > .env
4242
43-
- name: Run tests with Pytest
43+
- name: Run tests(UI & API) with Pytest
4444
run: |
4545
source .venv/bin/activate
46-
pytest Ultimateqa/tests -n auto
46+
pytest Ultimateqa/tests -n auto api_tests/test_users.py -n auto

Ultimateqa/tests/conftest.py

Lines changed: 0 additions & 18 deletions
This file was deleted.

Ultimateqa/tests/test_fe.py

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -16,21 +16,19 @@
1616
import Ultimateqa.tests.consts as consts
1717

1818

19-
def test_count_buttons(page: Page,base_url):
20-
#load_dotenv()
21-
#base_url = os.getenv("BASE_URL")
22-
page.goto(base_url)
19+
def test_count_buttons(page: Page, base_url_fe):
20+
page.goto(base_url_fe)
2321
sub_page_buttons = SubPageButtons(page)
2422

2523
count_of_buttons = sub_page_buttons.count_buttons()
2624
assert count_of_buttons == consts.NUN_OF_BUTTONS_TO_FIND, \
2725
f"Found: {count_of_buttons} buttons, expected: {consts.NUN_OF_BUTTONS_TO_FIND}"
2826

2927

30-
def test_verify_href_link(page: Page, base_url):
28+
def test_verify_href_link(page: Page, base_url_fe):
3129
# load_dotenv()
3230
# base_url = os.getenv("BASE_URL")
33-
page.goto(base_url)
31+
page.goto(base_url_fe)
3432
sub_page_social_media = SubPageSocialMedia(page)
3533

3634
facebook_buttons = sub_page_social_media.section.query_selector_all('a[title="Follow on Facebook"]')
@@ -44,10 +42,10 @@ def test_verify_href_link(page: Page, base_url):
4442
('Haim Cohen', 'adam@gmail.com', 'I am Adam', None, 'Thanks for contacting us'),
4543
('Asaf Levi', 'asaf@gmail.com', 'I am Asaf', '0', 'You entered the wrong number in captcha.'),
4644
])
47-
def test_forms(page: Page, name, email, message, exercise_result, message_after_submit,base_url):
45+
def test_forms(page: Page, name, email, message, exercise_result, message_after_submit, base_url_fe):
4846
#load_dotenv()
4947
#base_url = os.getenv("BASE_URL")
50-
page.goto(base_url)
48+
page.goto(base_url_fe)
5149
sub_page_social_forms = SubPageForms(page)
5250

5351
sub_page_social_forms.set_name(name)

api_tests/__init__.py

Whitespace-only changes.

api_tests/api_client.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
import requests
2+
3+
class APIClient:
4+
BASE_URL_API = None
5+
6+
def get(self, endpoint, params=None):
7+
return requests.get(f"{self.BASE_URL_API}/{endpoint}", params=params)
8+
9+
def post(self, endpoint, data=None):
10+
return requests.post(f"{self.BASE_URL_API}/{endpoint}", json=data)
11+
12+
def put(self, endpoint, data=None):
13+
return requests.put(f"{self.BASE_URL_API}/{endpoint}", json=data)
14+
15+
def delete(self, endpoint):
16+
return requests.delete(f"{self.BASE_URL_API}/{endpoint}")

api_tests/globals.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
class ApiHttpConstants:
2+
"""Static class for HTTP response codes."""
3+
OK = 200
4+
CREATED = 201
5+
UNAUTHORIZED=401
6+
NOT_FOUND = 404
7+
BAD_REQUEST = 400
8+
CONFLICT = 409

api_tests/test_users.py

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
from api_tests.globals import ApiHttpConstants
2+
3+
4+
def test_get_user_by_id(api):
5+
response = api.get("users/1")
6+
assert response.status_code == ApiHttpConstants.OK
7+
data = response.json()
8+
assert data["id"] == 1
9+
assert "username" in data
10+
11+
def test_create_post(api):
12+
payload = {
13+
"userId": 1,
14+
"title": "New test post",
15+
"body": "This is a body of the test post"
16+
}
17+
response = api.post("posts", data=payload)
18+
assert response.status_code == ApiHttpConstants.CREATED
19+
data = response.json()
20+
assert data["title"] == payload["title"],"data <> payload!"
21+
22+
def test_update_post(api):
23+
payload = {
24+
"id": 1,
25+
"title": "Updated title",
26+
"body": "Updated body",
27+
"userId": 1
28+
}
29+
response = api.put("posts/1", data=payload)
30+
assert response.status_code == ApiHttpConstants.OK
31+
assert response.json()["title"] == "Updated title"
32+
33+
def test_delete_post(api):
34+
response = api.delete("posts/1")
35+
assert response.status_code == ApiHttpConstants.OK

conftest.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
import os
2+
from dotenv import load_dotenv
3+
import pytest
4+
from api_tests.api_client import APIClient
5+
6+
@pytest.fixture(scope="session")
7+
def api():
8+
return APIClient()
9+
10+
11+
@pytest.fixture(scope='session',autouse=True)
12+
def base_url_fe():
13+
load_dotenv()
14+
return os.getenv('BASE_URL_FE')
15+
16+
@pytest.fixture(scope='session',autouse=True)
17+
def base_url_api():
18+
load_dotenv()
19+
APIClient.BASE_URL_API = os.getenv('BASE_URL_API')

0 commit comments

Comments
 (0)