Skip to content

Commit

Permalink
Add ShadowRoot field (#336)
Browse files Browse the repository at this point in the history
+ Run tests on chrome and firefox, skip shadow root test on firefox
  • Loading branch information
jsfehler authored Feb 11, 2021
1 parent b9a33c7 commit b60a976
Show file tree
Hide file tree
Showing 6 changed files with 73 additions and 2 deletions.
2 changes: 2 additions & 0 deletions stere/fields/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
from .splinter.input import Input
from .splinter.link import Link
from .splinter.money import Money
from .splinter.shadow_root import ShadowRoot

__all__ += [
'Button',
Expand All @@ -34,4 +35,5 @@
'Input',
'Link',
'Money',
'ShadowRoot',
]
41 changes: 41 additions & 0 deletions stere/fields/splinter/shadow_root.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
from typing import Optional

from splinter.driver.webdriver import WebDriverElement
from splinter.element_list import ElementList

from ..decorators import stere_performer
from ..field import Field


@stere_performer('null_action', consumes_arg=False)
class ShadowRoot(Field):
"""Field that finds the shadow-root of an element.
Only useful as the root of an Area.
Example:
>>> address_form = Area(
>>> root=ShadowRoot('css', '#addressFormBlock'),
>>> address=Input('css', '#userAddress'),
>>> )
"""

def find_all(self, wait_time: Optional[int] = None) -> ElementList:
"""Get the shadowRoot element for any found elements.
Returns:
ElementList
"""
found_elements = self._element.find(wait_time)

shadow_roots = []
for elem in found_elements:
shadow_root = self.browser.execute_script(
'return arguments[0].shadowRoot', elem._element,
)
shadow_roots.append(WebDriverElement(shadow_root, elem))

return ElementList(
shadow_roots, find_by=self.strategy, query=self.locator,
)
7 changes: 5 additions & 2 deletions tests/splinter/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,11 @@ def after(request, browser):

def fin():
# Send result to Sauce labs
res = str(not request.node.rep_call.failed).lower()
browser.execute_script("sauce:job-result={}".format(res))
try:
res = str(not request.node.rep_call.failed).lower()
browser.execute_script("sauce:job-result={}".format(res))
except AttributeError:
pass

if os.getenv('REMOTE_RUN') == "True":
request.addfinalizer(fin)
Expand Down
6 changes: 6 additions & 0 deletions tests/splinter/pages/dummy.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
Link,
Money,
Root,
ShadowRoot,
Text,
)

Expand Down Expand Up @@ -207,3 +208,8 @@ def __init__(self):
)

self.money_field = Money('id', 'moneyMoney')

self.shadow_root_area = Area(
root=ShadowRoot('css', '#has_shadow_root'),
data=Text('css', '#text_in_shadow_root'),
)
17 changes: 17 additions & 0 deletions tests/splinter/splinter_fields/test_shadow_root.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import pytest


@pytest.fixture(autouse=True)
def skip_by_browser(request):
marker = request.node.get_closest_marker('skip_if_browser')
if marker.args[0] == request.config.option.browser_name:
pytest.skip(marker.args[1])


@pytest.mark.skip_if_browser('firefox', "Can't get shadowRoot in firefox")
def test_shadow_root_find_all(test_page):
"""When I find the shadow root of an element
Then the elements in the shadow root can be found.
"""
test_page.navigate()
assert test_page.shadow_root_area.data.value == 'Inside a shadow root'
2 changes: 2 additions & 0 deletions tox.ini
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ commands =
py.test -s -vv {posargs} --cov=stere --cov-append tests/browserenabled
py.test -s -vv {posargs} --cov=stere --cov-append tests/config
py.test -s -vv -n 4 {posargs} --browser-name=firefox --splinter-webdriver=firefox --cov=stere --cov-append tests/splinter
py.test -s -vv -n 4 {posargs} --browser-name=chrome --splinter-webdriver=chrome --cov=stere --cov-append tests/splinter

[remote]
setenv =
Expand All @@ -30,6 +31,7 @@ commands =
py.test -s -vv {posargs} --cov=stere --cov-append tests/browserenabled
py.test -s -vv {posargs} --cov=stere --cov-append tests/config
py.test -s -vv -n 4 {posargs} --browser-name=firefox --splinter-webdriver=remote --cov=stere --cov-append tests/splinter
py.test -s -vv -n 4 {posargs} --browser-name=chrome --splinter-webdriver=remote --cov=stere --cov-append tests/splinter
py.test -s -vv {posargs} --browser-name=ios --cov=stere --cov-append tests/appium

[testenv:py36-local]
Expand Down

0 comments on commit b60a976

Please sign in to comment.