Skip to content

Commit 4ab6eb4

Browse files
authored
Merge pull request #64 from douglasdcm/exception-if-element-not-found
Raise exceptions when element not found
2 parents 1275296 + e6c9c34 commit 4ab6eb4

File tree

4 files changed

+36
-9
lines changed

4 files changed

+36
-9
lines changed

caqui/asynchronous.py

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,10 @@ async def __post(url, payload):
2121
async with session.post(
2222
url, data=json.dumps(payload), headers=HEADERS
2323
) as resp:
24-
response = await resp.json()
25-
return response
24+
status = resp.status
25+
if status in range(200, 399):
26+
return await resp.json()
27+
raise WebDriverError(f"Status code: {resp.status}, {resp.text}")
2628
except Exception as error:
2729
raise WebDriverError("'POST' request failed.") from error
2830

@@ -31,8 +33,10 @@ async def __get(url):
3133
try:
3234
async with aiohttp.ClientSession() as session:
3335
async with session.get(url, headers=HEADERS) as resp:
34-
response = await resp.json()
35-
return response
36+
status = resp.status
37+
if status in range(200, 399):
38+
return await resp.json()
39+
raise WebDriverError(f"Status code: {resp.status}, {resp.text}")
3640
except Exception as error:
3741
raise WebDriverError("'GET' request failed.") from error
3842

@@ -370,6 +374,10 @@ async def find_element(driver_url, session, locator_type, locator_value):
370374
payload = {"using": locator_type, "value": locator_value}
371375
url = f"{driver_url}/session/{session}/element"
372376
response = await __post(url, payload)
377+
if response.get("value").get("error"):
378+
raise WebDriverError(f"Failed to find element. {response}")
379+
if response.get("status") > 0:
380+
raise WebDriverError(f"Failed to find element. {response}")
373381
return helper.get_element(response)
374382
except Exception as error:
375383
raise WebDriverError(

caqui/synchronous.py

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,11 @@ def __get(url):
2222

2323

2424
def __post(url, payload):
25+
response = requests.request("POST", url, headers=HEADERS, data=json.dumps(payload))
2526
try:
26-
return requests.request(
27-
"POST", url, headers=HEADERS, data=json.dumps(payload)
28-
).json()
27+
if response.status_code in range(200, 399):
28+
return response.json()
29+
raise WebDriverError(f"Status code: {response.status_code}, {response.text}")
2930
except Exception as error:
3031
raise WebDriverError("'POST' request failed.") from error
3132

@@ -385,6 +386,8 @@ def find_element(driver_url, session, locator_type, locator_value):
385386
# It helps on debug
386387
if response.get("value").get("error"):
387388
raise WebDriverError(f"Failed to find element. {response}")
389+
if response.get("status") > 0:
390+
raise WebDriverError(f"Failed to find element. {response}")
388391
return helper.get_element(response)
389392
except Exception as error:
390393
raise WebDriverError(

pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ exclude = ["tests*", "utils", ".vscode", ".git*", "dist"]
88

99
[project]
1010
name = "caqui"
11-
version = "1.12.0"
11+
version = "1.13.0"
1212
authors = [
1313
{ name="Douglas Cardoso", email="noemail@noemail.com" },
1414
]

tests/feature/test_sync_and_async.py

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
1-
from pytest import fixture, mark
1+
from pytest import fixture, mark, raises
22
from caqui import asynchronous, synchronous
33
from tests.constants import PAGE_URL
4+
from caqui.exceptions import WebDriverError
45

56

67
@fixture
@@ -25,6 +26,21 @@ def __setup():
2526
synchronous.close_session(driver_url, session)
2627

2728

29+
@mark.asyncio
30+
async def test_raise_exception_when_element_not_found(__setup):
31+
driver_url, session = __setup
32+
locator_type = "xpath"
33+
locator_value = "//invalid-tag"
34+
35+
with raises(WebDriverError):
36+
synchronous.find_element(driver_url, session, locator_type, locator_value)
37+
38+
with raises(WebDriverError):
39+
await asynchronous.find_element(
40+
driver_url, session, locator_type, locator_value
41+
)
42+
43+
2844
@mark.asyncio
2945
async def test_set_timeouts(__setup):
3046
driver_url, session = __setup

0 commit comments

Comments
 (0)