-
-
Notifications
You must be signed in to change notification settings - Fork 152
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[#228] NEW: query.shadow_root + query.shadow_roots
- Loading branch information
Showing
6 changed files
with
209 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
# How to work with Shadow DOM in Selene? | ||
|
||
– By using advanced [query.js.shadow_root][selene.core.query.js.shadow_root] and [query.js.shadow_roots][selene.core.query.js.shadow_roots] queries, as simply as: | ||
|
||
```python | ||
from selene import browser, have, query | ||
|
||
# GIVEN | ||
paragraphs = browser.all('my-paragraph') | ||
|
||
# WHEN it's enough to access specific elements | ||
paragraph_2_shadow = paragraphs.second.get(query.js.shadow_root) # 💡 | ||
my_shadowed_text_2 = paragraph_2_shadow.element('[name=my-text]') | ||
# OR when you need all shadow roots | ||
my_shadowed_texts = paragraphs.get(query.js.shadow_roots) # 💡 | ||
|
||
# As you can see these queries are lazy, | ||
# so you were able to store them in vars ↖️ | ||
# even before open ↙️ | ||
browser.open('https://the-internet.herokuapp.com/shadowdom') | ||
|
||
# THEN | ||
my_shadowed_text_2.should(have.exact_text("My default text")) # ⬅️ | ||
my_shadowed_texts.should(have.exact_texts("My default text", "My default text")) # ⬅️ | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
65 changes: 65 additions & 0 deletions
65
tests/integration/element__get__query__js__shadow_root__all_elements_test.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
# MIT License | ||
# | ||
# Copyright (c) 2024 Iakiv Kramarenko | ||
# | ||
# Permission is hereby granted, free of charge, to any person obtaining a copy | ||
# of this software and associated documentation files (the "Software"), to deal | ||
# in the Software without restriction, including without limitation the rights | ||
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
# copies of the Software, and to permit persons to whom the Software is | ||
# furnished to do so, subject to the following conditions: | ||
# | ||
# The above copyright notice and this permission notice shall be included in all | ||
# copies or substantial portions of the Software. | ||
# | ||
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | ||
# SOFTWARE. | ||
import logging | ||
|
||
import pytest | ||
|
||
from selene import command, have, query, support | ||
|
||
|
||
def test_actions_on_shadow_roots_of_all_elements(session_browser): | ||
# GIVEN | ||
browser = session_browser.with_(timeout=0.5) | ||
paragraphs = browser.all('my-paragraph') | ||
|
||
# WHEN even before opened browser | ||
paragraph_shadow_roots = paragraphs.get(query.js.shadow_roots) | ||
my_shadowed_texts = paragraph_shadow_roots.all('[name=my-text]') | ||
# AND | ||
browser.open('https://the-internet.herokuapp.com/shadowdom') | ||
|
||
# THEN | ||
my_shadowed_texts.should(have.exact_texts('My default text', 'My default text')) | ||
paragraphs.should( | ||
have.exact_texts( | ||
"Let's have some different text!", | ||
"Let's have some different text!\nIn a list!", | ||
) | ||
) | ||
|
||
# WHEN failed | ||
try: | ||
my_shadowed_texts.should(have.exact_texts('My WRONG text', 'My WRONG text')) | ||
pytest.fail('should have failed on size mismatch') | ||
except AssertionError as error: | ||
# THEN | ||
assert ( | ||
'Message: \n' | ||
'\n' | ||
'Timed out after 0.5s, while waiting for:\n' | ||
"browser.all(('css selector', 'my-paragraph')): shadow roots.all(('css " | ||
"selector', '[name=my-text]')).has exact texts ('My WRONG text', 'My WRONG " | ||
"text')\n" | ||
'\n' | ||
"Reason: AssertionError: actual visible_texts: ['My default text', 'My " | ||
"default text']\n" | ||
) in str(error) |
64 changes: 64 additions & 0 deletions
64
tests/integration/element__get__query__js__shadow_root__element_test.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
# MIT License | ||
# | ||
# Copyright (c) 2024 Iakiv Kramarenko | ||
# | ||
# Permission is hereby granted, free of charge, to any person obtaining a copy | ||
# of this software and associated documentation files (the "Software"), to deal | ||
# in the Software without restriction, including without limitation the rights | ||
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
# copies of the Software, and to permit persons to whom the Software is | ||
# furnished to do so, subject to the following conditions: | ||
# | ||
# The above copyright notice and this permission notice shall be included in all | ||
# copies or substantial portions of the Software. | ||
# | ||
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | ||
# SOFTWARE. | ||
import logging | ||
|
||
import pytest | ||
|
||
from selene import command, have, query, support | ||
|
||
|
||
def test_actions_on_shadow_root_element(session_browser): | ||
# GIVEN | ||
browser = session_browser.with_(timeout=0.5) | ||
paragraphs = browser.all('my-paragraph') | ||
|
||
# WHEN even before opened browser | ||
paragraph_1_shadow = paragraphs.first.get(query.js.shadow_root) | ||
paragraph_2_shadow = paragraphs.second.get(query.js.shadow_root) | ||
my_shadowed_text_1 = paragraph_1_shadow.element('[name=my-text]') | ||
my_shadowed_text_2 = paragraph_2_shadow.element('[name=my-text]') | ||
# AND | ||
browser.open('https://the-internet.herokuapp.com/shadowdom') | ||
|
||
# THEN | ||
paragraphs.first.should(have.exact_text("Let's have some different text!")) | ||
my_shadowed_text_1.should(have.exact_text("My default text")) | ||
paragraphs.second.should( | ||
have.exact_text("Let's have some different text!\nIn a list!") | ||
) | ||
my_shadowed_text_2.should(have.exact_text("My default text")) | ||
|
||
# WHEN failed | ||
try: | ||
my_shadowed_text_1.should(have.exact_text("My WRONG text")) | ||
pytest.fail('should have failed on size mismatch') | ||
except AssertionError as error: | ||
# THEN | ||
assert ( | ||
'Message: \n' | ||
'\n' | ||
'Timed out after 0.5s, while waiting for:\n' | ||
"browser.all(('css selector', 'my-paragraph'))[0]: shadow root.element(('css " | ||
"selector', '[name=my-text]')).has exact text My WRONG text\n" | ||
'\n' | ||
'Reason: AssertionError: actual text: My default text\n' | ||
) in str(error) |