minkphp / webdriver-classic-driver

Mink driver for the W3C WebDriver Classic protocol
MIT License
3 stars 5 forks source link

Support switching to frame by index #8

Closed aik099 closed 8 months ago

aik099 commented 8 months ago

Please improve the switchToIFrame method with frame/iframe switching support by an index. Currently, in the W3C-compliant mode, it doesn't work.

Please see https://github.com/minkphp/Mink/issues/866 for more info.

I would happily create PR similar to https://github.com/minkphp/MinkSelenium2Driver/pull/378 as well.

uuf6429 commented 8 months ago

Please improve the switchToIFrame method with frame/iframe switching support by an index. Currently, in the W3C-compliant mode, it doesn't work.

It should already work, and actually the PR you linked to looks broken - it only handles index (id), or?

    public function switchToIFrame($name = null)
    {
        $this->getWebDriverSession()->frame(array('id' => $name));
    }

vs

    public function switchToIFrame(?string $name = null): void
    {
        $frameQuery = $name;
        if ($name && $this->getWebDriver()->isW3cCompliant()) {
            try {
                $frameQuery = $this->getWebDriver()->findElement(WebDriverBy::id($name));
            } catch (NoSuchElementException $e) {
                $frameQuery = $this->getWebDriver()->findElement(WebDriverBy::name($name));
            }
        }

        $this->getWebDriver()->switchTo()->frame($frameQuery);
    }

I'd be fine with replacing ?string type-def with null|string|int PHPDoc type-hint though (just removing the type declaration is a step backword IMO).

uuf6429 commented 8 months ago

Looking at the json-wire link you provided it mentions:

JSON Parameters: id - {string|number|null|WebElement JSON Object} Identifier for the frame to change focus to.

It doesn't say anything about an index just "identifier", however it also mentions the number type, so if I understand correctly, this means:

->switchToIFrame('xx');    // non-W3C mode / "old" mink logic, <iframe name=xx>
->switchToIFrame('xx');    // W3C mode, <iframe id=xx>
->switchToIFrame(1);       // W3C mode, first <iframe> element <- this is broken case we're discussing now, right?
aik099 commented 8 months ago

@uuf6429 , I've confirmed (by running a live test), that providing a number does switch to a window.frames[that_number] frame.

I'm only concerned by the frame switching by index, because in Selenium 3 using MinkSelenium2Driver frame switching by name doesn't work at all.

uuf6429 commented 8 months ago

I'm only concerned by the frame switching by index, because in Selenium 3 using MinkSelenium2Driver frame switching by name doesn't work at all.

I guess that's because MinkSelenium2Driver is only using id()? This driver actually does check selenium 3, and it works. For me this looks like a new feature; to support switching by index (which is also ok).

aik099 commented 8 months ago

Long story short: I've managed to get it working by writing a code equivalent to the one used by this driver:

  1. find web element by id/name
  2. try switching a frame to the web element instead of providing id/name directly

Thank you for the idea.

PR: https://github.com/minkphp/MinkSelenium2Driver/pull/382 .