Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Shadow dom #101

Open
tejas-elsner opened this issue Jul 14, 2022 · 3 comments
Open

Shadow dom #101

tejas-elsner opened this issue Jul 14, 2022 · 3 comments

Comments

@tejas-elsner
Copy link

What are you trying to achieve?

Get the Shadow Dom

What do you get instead?

Not able to get the result from Shadow root

Provide console output if related. Use -vvv mode for more details.

# paste output here

Provide test source code if related

// paste test

Details

  • Codeception version:
  • PHP Version:
  • Operating System:
  • Installation type: Phar || Composer
  • List of installed packages (composer show)
  • Suite configuration:
# paste suite config here
@Naktibalda Naktibalda transferred this issue from Codeception/Codeception Jul 14, 2022
@Naktibalda
Copy link
Member

I guess that you want it in WebDriver module.

You are welcome to implement this feature and raise pull request.

@DanielSiepmann
Copy link

DanielSiepmann commented Feb 20, 2024

The underlying library added support: php-webdriver/php-webdriver#1010
So someone with the necessary knowledge should be able to add this to the module. I need this right now and will play around with this. I'll share my insights if they are helpful.

It looks like Firefox added ShadowDom support as well, but still lacks support for some locators, see: w3c/webdriver#1610 so adding support within the module might end in requests why things aren't working which is an underlying issue.

@DanielSiepmann
Copy link

I came up with the following:

diff --git a/src/Codeception/Module/WebDriver.php b/src/Codeception/Module/WebDriver.php
index 2c5b578..51492f1 100644
--- a/src/Codeception/Module/WebDriver.php
+++ b/src/Codeception/Module/WebDriver.php
@@ -1176,9 +1176,9 @@ class WebDriver extends CodeceptionModule implements
 
     public function click($link, $context = null): void
     {
-        $page = $this->webDriver;
+        $page = $this->getBaseElement();
         if ($context) {
-            $page = $this->matchFirstOrFail($this->webDriver, $context);
+            $page = $this->matchFirstOrFail($page, $context);
         }
 
         $el = $this->_findClickable($page, $link);
@@ -3661,6 +3661,32 @@ class WebDriver extends CodeceptionModule implements
         $this->setBaseElement();
     }
 
+    public function performOnShadowRoot($element, $actions, int $timeout = 10): void
+    {
+        $this->waitForElement($element, $timeout);
+        $this->setBaseElement($element);
+        $this->debugSection('InnerText', $this->getBaseElement()->getText());
+
+        $this->baseElement = $this->getBaseElement()->getShadowRoot();
+
+        if (is_callable($actions)) {
+            $actions($this);
+            $this->setBaseElement();
+            return;
+        }
+
+        if (is_array($actions)) {
+            $actions = ActionSequence::build()->fromArray($actions);
+        }
+
+        if (!$actions instanceof ActionSequence) {
+            throw new InvalidArgumentException("2nd parameter, actions should be callback, ActionSequence or array");
+        }
+
+        $actions->run($this);
+        $this->setBaseElement();
+    }
+
     /**
      * @param string|array|WebDriverBy $element
      */

That way it could be used like this:

        $this->tester->performOnShadowRoot(
            'typo3-backend-new-content-element-wizard',
            ActionSequence::build()
                ->click("//span[normalize-space(.)='$tabName']")
                ->click("//span[normalize-space(.)='$contentElementLabel']")
        );

Following the existing performOn() approach.

I had to fix the click() method that does not seem to respect the base Element in current version.
I also noticed that the performOn won't work with additions like step decorators, e.g. Codeception\Step\Retry.
Also ShadowDom via WebDriver in Browsers currently doesn't seem to support various locators, e..g XPath. So our above example won't work.

Due to all those issues I didn't open a PR. I don't have the feeling general shadow dom support is there yet … But feel free to use the patch and provide a PR or patch your own code.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants