Skip to content

Commit 3d23771

Browse files
authored
Merge pull request #55 from douglasdcm/add-find-element
Add find element
2 parents 9cd035d + aeb0b0e commit 3d23771

File tree

8 files changed

+223
-1
lines changed

8 files changed

+223
-1
lines changed

caqui/asynchronous.py

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,49 @@ async def __get(url):
3737
raise WebDriverError("'GET' request failed.") from error
3838

3939

40+
async def set_timeouts(driver_url, session, timeouts):
41+
"""Set timeouts"""
42+
try:
43+
url = f"{driver_url}/session/{session}/timeouts"
44+
payload = {
45+
"implicit": timeouts,
46+
}
47+
await __post(url, payload)
48+
return True
49+
except Exception as error:
50+
raise WebDriverError(f"Failed to set timeouts.") from error
51+
52+
53+
async def find_children_elements(
54+
driver_url, session, parent_element, locator_type, locator_value
55+
):
56+
"""Find the children elements by 'locator_type'"""
57+
try:
58+
url = f"{driver_url}/session/{session}/element/{parent_element}/elements"
59+
payload = {"using": locator_type, "value": locator_value, "id": parent_element}
60+
response = await __post(url, payload)
61+
return helper.get_elements(response)
62+
except Exception as error:
63+
raise WebDriverError(
64+
f"Failed to find the children elements from '{parent_element}'."
65+
) from error
66+
67+
68+
async def find_child_element(
69+
driver_url, session, parent_element, locator_type, locator_value
70+
):
71+
"""Find the child element by 'locator_type'"""
72+
try:
73+
url = f"{driver_url}/session/{session}/element/{parent_element}/element"
74+
payload = {"using": locator_type, "value": locator_value, "id": parent_element}
75+
response = await __post(url, payload)
76+
return helper.get_element(response)
77+
except Exception as error:
78+
raise WebDriverError(
79+
f"Failed to find the child element from '{parent_element}'."
80+
) from error
81+
82+
4083
async def get_page_source(driver_url, session):
4184
"""Get the page source (all content)"""
4285
try:

caqui/synchronous.py

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,49 @@ def __delete(url):
3737
raise WebDriverError("'DELETE' request failed.") from error
3838

3939

40+
def set_timeouts(driver_url, session, timeouts):
41+
"""Set timeouts"""
42+
try:
43+
url = f"{driver_url}/session/{session}/timeouts"
44+
payload = {
45+
"implicit": timeouts,
46+
}
47+
__post(url, payload)
48+
return True
49+
except Exception as error:
50+
raise WebDriverError(f"Failed to set timeouts.") from error
51+
52+
53+
def find_children_elements(
54+
driver_url, session, parent_element, locator_type, locator_value
55+
):
56+
"""Find the children elements by 'locator_type'"""
57+
try:
58+
url = f"{driver_url}/session/{session}/element/{parent_element}/elements"
59+
payload = {"using": locator_type, "value": locator_value, "id": parent_element}
60+
response = __post(url, payload)
61+
return helper.get_elements(response)
62+
except Exception as error:
63+
raise WebDriverError(
64+
f"Failed to find the children elements from '{parent_element}'."
65+
) from error
66+
67+
68+
def find_child_element(
69+
driver_url, session, parent_element, locator_type, locator_value
70+
):
71+
"""Find the child element by 'locator_type'"""
72+
try:
73+
url = f"{driver_url}/session/{session}/element/{parent_element}/element"
74+
payload = {"using": locator_type, "value": locator_value, "id": parent_element}
75+
response = __post(url, payload)
76+
return helper.get_element(response)
77+
except Exception as error:
78+
raise WebDriverError(
79+
f"Failed to find the child element from '{parent_element}'."
80+
) from error
81+
82+
4083
def get_page_source(driver_url, session):
4184
"""Get the page source (all content)"""
4285
try:

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.11.0"
11+
version = "1.12.0"
1212
authors = [
1313
{ name="Douglas Cardoso", email="noemail@noemail.com" },
1414
]

tests/feature/test_sync_and_async.py

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,70 @@ def __setup():
2525
synchronous.close_session(driver_url, session)
2626

2727

28+
@mark.asyncio
29+
async def test_set_timeouts(__setup):
30+
driver_url, session = __setup
31+
timeouts_1 = 5000 # milliseconds
32+
timeouts_2 = 3000 # milliseconds
33+
34+
synchronous.set_timeouts(driver_url, session, timeouts_1)
35+
36+
assert synchronous.get_timeouts(driver_url, session).get("implicit") == timeouts_1
37+
38+
await asynchronous.set_timeouts(driver_url, session, timeouts_2)
39+
40+
assert synchronous.get_timeouts(driver_url, session).get("implicit") == timeouts_2
41+
42+
43+
@mark.asyncio
44+
async def test_find_children_elements(__setup):
45+
driver_url, session = __setup
46+
expected = 5 # parent inclusive
47+
locator_type = "xpath"
48+
locator_value = "//div"
49+
50+
parent_element = synchronous.find_element(
51+
driver_url, session, locator_type, '//div[@class="parent"]'
52+
)
53+
54+
children_elements = synchronous.find_children_elements(
55+
driver_url, session, parent_element, locator_type, locator_value
56+
)
57+
58+
assert len(children_elements) == expected
59+
60+
children_elements = await asynchronous.find_children_elements(
61+
driver_url, session, parent_element, locator_type, locator_value
62+
)
63+
64+
assert len(children_elements) == expected
65+
66+
67+
@mark.asyncio
68+
async def test_find_child_element(__setup):
69+
driver_url, session = __setup
70+
expected = "any4"
71+
locator_type = "xpath"
72+
locator_value = '//div[@class="child4"]'
73+
74+
parent_element = synchronous.find_element(
75+
driver_url, session, locator_type, '//div[@class="parent"]'
76+
)
77+
78+
child_element = synchronous.find_child_element(
79+
driver_url, session, parent_element, locator_type, locator_value
80+
)
81+
82+
text = synchronous.get_text(driver_url, session, child_element)
83+
84+
assert text == expected
85+
child_element = await asynchronous.find_child_element(
86+
driver_url, session, parent_element, locator_type, locator_value
87+
)
88+
text = synchronous.get_text(driver_url, session, child_element)
89+
assert text == expected
90+
91+
2892
@mark.asyncio
2993
async def test_get_page_source(__setup):
3094
driver_url, session = __setup

tests/html/playground.html

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,14 @@ <h1>Basic page</h1>
1818
<br>
1919
<p id="end">end</p>
2020

21+
<div class="parent">
22+
any0
23+
<div class="child1">any1</div>
24+
<div class="child2">any2</div>
25+
<div class="child3">any3</div>
26+
<div class="child4">any4</div>
27+
</div>
28+
2129
<script>
2230
function myFunction(element, color) {
2331
element.style.color = color;

tests/test_sniffer.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
from selenium import webdriver
44
from pytest import fixture, mark
55
from tests.constants import PAGE_URL
6+
from selenium.webdriver.common.by import By
67

78

89
@fixture
@@ -26,11 +27,21 @@ def setup():
2627
driver.quit()
2728

2829

30+
@mark.skip("used just to discover request data")
31+
def test_sniffer_find_children_elements(setup):
32+
driver = setup
33+
element = driver.find_element(By.XPATH, '//div[@class="parent"]')
34+
elements = element.find_elements(By.XPATH, "//div")
35+
for e in elements:
36+
print("element_text:", e.text)
37+
38+
2939
@mark.skip("used just to discover request data")
3040
def test_exec_script(setup):
3141
driver = setup
3242
assert driver.execute_script("return document.body.scrollHeight") == "any"
3343

44+
3445
@mark.skip("used just to discover request data")
3546
def test_clear(setup):
3647
driver = setup

tests/unit/test_async_unit.py

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,37 @@ async def mock_request(*args):
1111
pass
1212

1313

14+
@mark.asyncio
15+
async def test_set_timeouts():
16+
async def mock_request(*args):
17+
return fake_responses.GET_TIMEOUTS
18+
19+
with patch("caqui.asynchronous.__post", mock_request):
20+
assert await asynchronous.set_timeouts("", "", "") == True
21+
22+
23+
@mark.asyncio
24+
async def test_find_children_elements():
25+
element = "C230605181E69CB2C4C36B8E83FE1245_element_2"
26+
27+
async def mock_request(*args):
28+
return fake_responses.FIND_ELEMENTS
29+
30+
with patch("caqui.asynchronous.__post", mock_request):
31+
assert element in await asynchronous.find_children_elements("", "", "", "", "")
32+
33+
34+
@mark.asyncio
35+
async def test_find_child_element():
36+
element = "0.8851292311864847-1"
37+
38+
async def mock_request(*args):
39+
return fake_responses.FIND_ELEMENT
40+
41+
with patch("caqui.asynchronous.__post", mock_request):
42+
assert await asynchronous.find_child_element("", "", "", "", "") == element
43+
44+
1445
@mark.asyncio
1546
async def test_execute_script():
1647
expected = "any"

tests/unit/test_sync_unit.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,28 @@
33
from tests import fake_responses
44

55

6+
@patch("requests.request", return_value=fake_responses.GET_TIMEOUTS)
7+
def test_set_timeouts(*args):
8+
assert synchronous.set_timeouts("", "", "") == True
9+
10+
11+
@patch("requests.request", return_value=fake_responses.FIND_ELEMENTS)
12+
def test_find_children_elements(*args):
13+
element = "C230605181E69CB2C4C36B8E83FE1245_element_2"
14+
15+
elements = synchronous.find_children_elements("", "", "", "", "")
16+
17+
assert element in elements
18+
assert len(elements) == 3
19+
20+
21+
@patch("requests.request", return_value=fake_responses.FIND_ELEMENT)
22+
def test_find_child_element(*args):
23+
expected = "0.8851292311864847-1"
24+
25+
assert synchronous.find_child_element("", "", "", "", "") == expected
26+
27+
628
@patch("requests.request", return_value=fake_responses.EXECUTE_SCRIPT)
729
def test_execute_script(*args):
830
expected = "any"

0 commit comments

Comments
 (0)