-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_case.py
More file actions
172 lines (144 loc) · 6 KB
/
Copy pathtest_case.py
File metadata and controls
172 lines (144 loc) · 6 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
from SeleniumAPI import SeleniumAPI
import unittest
import asyncio
import json
from SeleniumAPI import SeleniumAPI
from unittest.mock import patch, MagicMock
import time
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
def time_side_effect():
# yield the first few specified values
for val in [0, 0.01, 61.01]:
yield val
# after the first few, yield an incrementing value
counter = 62.01
while True:
yield counter
counter += 1
class TestSeleniumAPI(unittest.TestCase):
@classmethod
def setUpClass(cls):
chrome_options = Options()
chrome_options.add_argument("--headless")
cls.driver = webdriver.Chrome(options=chrome_options)
@classmethod
def tearDownClass(cls):
cls.driver.quit()
cls.driver = None
def test_call_get(self):
url = "https://reqres.in/api/users" # Public API url
method = "GET"
headers = {"Content-Type": "application/json"}
self.driver.get(url)
response = SeleniumAPI(self.driver, url, method, headers)
self.assertIsNotNone(response)
self.assertIn("data", response)
def test_call_post(self):
url = "https://reqres.in/api/users" # Public API url
method = "POST"
headers = {"Content-Type": "application/json"}
payload = {"name": "John Doe", "job": "Software Engineer"}
self.driver.get(url)
response = SeleniumAPI(self.driver, url, method, headers, payload)
response = json.dumps(response)
self.assertIsNotNone(response)
self.assertIn("id", response)
def test_call_put(self):
url = "https://jsonplaceholder.typicode.com/posts/1"
method = "PUT"
headers = {"Content-Type": "application/json"}
payload = {"id": 1, "title": "foo", "body": "bar", "userId": 1}
self.driver.get(url)
response = SeleniumAPI(self.driver, url, method, headers, payload)
self.assertIsNotNone(response)
self.assertIn("title", response)
def test_call_delete(self):
url = "https://jsonplaceholder.typicode.com/posts/1"
method = "DELETE"
self.driver.get(url)
response = SeleniumAPI(self.driver, url, method)
# The response from a DELETE request to the JSONPlaceholder API is an empty object, so we'll check for that.
self.assertEqual(response, {})
def test_call_patch(self):
url = "https://jsonplaceholder.typicode.com/posts/1"
method = "PATCH"
headers = {"Content-Type": "application/json"}
payload = {"title": "foo"}
self.driver.get(url)
response = SeleniumAPI(self.driver, url, method, headers, payload)
self.assertIsNotNone(response)
self.assertIn("title", response)
def test_call_with_error(self):
url = "https://reqres.in/api/users" # Public API url
method = "POST"
payload = {"name": "John Doe", "job": "Software Engineer"}
payload = {"title": "foo", "body": "bar", "userId": 1}
with self.assertRaises(Exception):
SeleniumAPI(self.driver, url, method, headers, payload)
def test_non_existent_urls(self):
with self.assertRaises(Exception):
SeleniumAPI(self.driver, "non_existent_url")
def test_call_missing_url(self):
method = "GET"
with self.assertRaises(Exception):
SeleniumAPI(self.driver, None, method)
def test_call_missing_method(self):
url = "https://reqres.in/api/users"
with self.assertRaises(Exception):
SeleniumAPI(self.driver, url, None)
def test_call_missing_headers(self):
url = "https://reqres.in/api/users"
method = "POST"
payload = {"name": "John Doe", "job": "Software Engineer"}
response = SeleniumAPI(self.driver, url, method, None, payload)
self.assertIsNotNone(response)
self.assertIn("id", response)
def test_call_missing_payload(self):
url = "https://reqres.in/api/users"
method = "POST"
headers = {"Content-Type": "application/json"}
with self.assertRaises(Exception):
SeleniumAPI(self.driver, url, method, headers, None)
def test_call_missing_driver(self):
method = "GET"
with self.assertRaises(Exception):
url = "https://reqres.in/api/users"
SeleniumAPI(None, url, method)
def test_call_missing_url_and_method(self):
with self.assertRaises(Exception):
SeleniumAPI(self.driver, None, None)
def test_call_missing_url_and_headers(self):
method = "GET"
with self.assertRaises(Exception):
SeleniumAPI(self.driver, None, method, None)
def test_call_missing_url_and_payload(self):
method = "POST"
headers = {"Content-Type": "application/json"}
with self.assertRaises(Exception):
SeleniumAPI(self.driver, None, method, headers, None)
def test_call_missing_method_and_headers(self):
url = "https://reqres.in/api/users"
with self.assertRaises(Exception):
SeleniumAPI(self.driver, url, None, None)
def test_call_missing_method_and_payload(self):
url = "https://reqres.in/api/users"
headers = {"Content-Type": "application/json"}
with self.assertRaises(Exception):
SeleniumAPI(self.driver, url, None, headers, None)
def test_call_missing_headers_and_payload(self):
url = "https://reqres.in/api/users"
method = "POST"
response = SeleniumAPI(self.driver, url, method, None, None)
self.assertIsNotNone(response)
def test_call_missing_payload(self):
url = "https://reqres.in/api/users"
method = "POST"
headers = {"Content-Type": "application/json"}
with self.assertRaises(Exception):
SeleniumAPI(self.driver, url, method, headers, None)
def test_call_missing_all(self):
with self.assertRaises(Exception):
SeleniumAPI(None, None, None, None, None)
if __name__ == "__main__":
unittest.main()