phpunit-selenium icon indicating copy to clipboard operation
phpunit-selenium copied to clipboard

Browser resurrection in shared mode

Open zerkms opened this issue 12 years ago • 7 comments

Now if you closeWindow() in the end of the test method (sometimes it's a test scenario, like in my case) then you'll see the next test method is failed with the exception

Error communicating with the remote browser. It may have died.

or

Session not found ....

(it's driver dependent)

How about resurrecting the browser in PHPUnit_Extensions_Selenium2TestCase_SessionStrategy_Shared::session() in case if it was closed?

I see the implementation to look like

public function session(array $parameters, $internal = false)
{
    if ($this->lastTestWasNotSuccessful) {
        if ($this->session !== NULL) {
            $this->session->stop();
            $this->session = NULL;
        }
        $this->lastTestWasNotSuccessful = FALSE;
    }
    if ($this->session === NULL) {
        $this->session = $this->original->session($parameters);
        $this->mainWindow = $this->session->windowHandle();
    } else {
        try {
            $this->session->window($this->mainWindow);
        } catch (Exception $e) {
            if ($internal) {
                throw $e;
            }

            if (strpos($e->getMessage(), 'Session not found') === false && strpos($e->getMessage(), 'It may have died') === false) {
                throw $e;
            }

            $this->session->stop();
            $this->session = NULL;
            return $this->session($parameters, true);
        }
    }
    return $this->session;
}

What do you think?

If you think it makes sense - I'll cover this solution with tests and send PR.

zerkms avatar Jan 30 '13 23:01 zerkms

Should be nice for performance, but have you located the reason why the current policy (opening a new session) fails?

giorgiosironi avatar Feb 02 '13 15:02 giorgiosironi

@giorgiosironi,

yep - the current implementation invokes

$this->session->window($this->mainWindow);

for every test run (except of first), assuming there is a mainWindow. But as long as we closed it - there is no one, thus we get an exception.

zerkms avatar Feb 02 '13 21:02 zerkms

I will merge a smarter solution than the current one. Take a loot at the existing tests too (if there are any at the unit or end-to-end level) too to see if they need to be updated.

giorgiosironi avatar Feb 03 '13 10:02 giorgiosironi

@giorgiosironi,

"Take a loot" --- do like!

zerkms avatar Feb 03 '13 10:02 zerkms

Oops :) "look"

giorgiosironi avatar Feb 03 '13 10:02 giorgiosironi

@zerkms I have added new changes in #195 , is this variant ok?

emaks avatar Feb 10 '13 12:02 emaks

@emaks, I will try to look later, don't have much time now, sorry

zerkms avatar Feb 10 '13 19:02 zerkms