spyoungtech / behave-webdriver

Selenium webdriver step library for use with the behave BDD testing framework
MIT License
62 stars 21 forks source link

Shadow-dom #50

Open jrd opened 6 years ago

jrd commented 6 years ago

Shadow-dom is very hard to test and to find elements.

find_element_XXX methods cannot pass through shadow-root and therefore great deal of effort should be done to reach some element if nested shadow elements are in the path.

It is also difficult to wait for some element to appear or desappear for the same reason.

driver.execute_script('return arguments[0].shadowRoot', element)

is enough to reach the shadow-dom of an element and from there, any other find_element_XXX method will work (until reaching another shadow dom of course)

Proposition to ease usage:

that splits the css selector or xpath expression to get the shadow-dom of a element and continue descending in the selection.

Rough example:

def shadow(driver, element):
    driver.execute_script('return arguments[0].shadowRoot', element)

def find_element_by_css_selector(driver, selector):
    shadow_dom_selector = '> ##shadow-dom'
    selectors = selector.split(shadow_dom_selector)
    element = None
    for s in selectors:
        if element:
            element = shadow(element)
        else:
            element = driver
        element = element.find_element_by_css_selector(s)
    return element

tata = find_element_by_css_selector(driver, 'div.foo bar > ##shadow-dom baz.toto > #titi  > ##shadow-dom tata')

What do you think?

jrd commented 6 years ago

Interesting information: https://github.com/SeleniumHQ/selenium/issues/4971#issuecomment-340230404

spyoungtech commented 6 years ago

This would definitely be an interesting feature to explore. What I'll do in the coming week is investigate and understand interacting with the Shadow Dom via Selenium as best as I can.

As far as a possible interface for this in behave-webdriver, my main concern would be not affecting the behavior of finding elements not in a shadow dom.

As of right now, there's no provided step for locating/working with elements relative to other elements. Perhaps that may be a good angle to get at the shadow dom.

spyoungtech commented 6 years ago

Just chiming back in. Haven't forgot about this, things have just been a bit busy.

As a next step, I'm planning to put together a scenario or two for shadow dom into the demo app. If we implement something to make interacting with the Shadow Dom easier, I'm still contemplating what an elegant interface for this might look like.

sunpala commented 3 years ago

Hi @spyoungtech, I happened to find your comments while trying to figure out automating a Salesforce app that uses shadow dom, using selenium and Java. I realize now(from your comments), it's not going to be as easy I thought it would be. I was wondering if you have any updates following your investigation. Sorry, I am not trying to be cheeky, but it will be really useful to testers like me. Thanks

spyoungtech commented 3 years ago

Hi @sunpala unfortunately I've not yet found a great way to deal with this use case. Part of the difficulty is that support for shadow dom varies widely by browser.

Another unanswered question for me is how should a user specify this?