laravel / dusk

Laravel Dusk provides simple end-to-end testing and browser automation.
https://laravel.com/docs/dusk
MIT License
1.88k stars 322 forks source link

Facebook\WebDriver\Exception\NoSuchDriverException: no such session #105

Closed OwenMelbz closed 5 years ago

OwenMelbz commented 7 years ago

Hi,

I'm starting to scaffold a suite of browser tests for a project, however when I run the full suite via php artisan dusk 90% of the tests fail to due what seems like a race condition.

10) Tests\Browser\RegisterTest::testPublicCanRegister
Facebook\WebDriver\Exception\NoSuchDriverException: no such session
  (Driver info: chromedriver=2.27.440174 (e97a722caafc2d3a8b807ee115bfb307f7d2cfd9),platform=Mac OS X 10.12.3 x86_64)

/Users/owen/Sites/footy-finance/vendor/facebook/webdriver/lib/Exception/WebDriverException.php:100
/Users/owen/Sites/footy-finance/vendor/facebook/webdriver/lib/Remote/HttpCommandExecutor.php:321
/Users/owen/Sites/footy-finance/vendor/facebook/webdriver/lib/Remote/RemoteWebDriver.php:597
/Users/owen/Sites/footy-finance/vendor/facebook/webdriver/lib/Remote/RemoteWebDriver.php:373
/Users/owen/Sites/footy-finance/vendor/laravel/dusk/src/Browser.php:186
/Users/owen/Sites/footy-finance/vendor/laravel/dusk/src/TestCase.php:143
/Users/owen/Sites/footy-finance/vendor/laravel/framework/src/Illuminate/Support/Collection.php:244
/Users/owen/Sites/footy-finance/vendor/laravel/dusk/src/TestCase.php:144
/Users/owen/Sites/footy-finance/vendor/laravel/dusk/src/TestCase.php:90
/Users/owen/Sites/footy-finance/tests/Browser/RegisterTest.php:65

If I run the tests class by class like php artisan dusk tests/Browser/RegisterTest.php they always work.

It just seems when they're all together they expect your computer to be consistently fast and be able to spin up a chrome instance and load the page before the timeout.

Is there anyway we can make it so it slows down/waits a little longer before trying to run the tests?

OwenMelbz commented 7 years ago

An update for anybody with similar issues, the way I've currently tried to solve it is using a nasty sleep :(

E.g. my tests look something like

public function testSchoolsCanRegister()
    {
        $testEmail = 'teststudent';
        User::where(['email' => $testEmail])->forceDelete();
        $school = factory(School::class)->create();

        sleep(1);

        $this->browse(function ($browser) use ($school, $testEmail) {
            $browser->visit('/register/student')
                ->type('#school_code', $school->auth_code)
                ->type('#email', $testEmail)
                ->type('#password', 'secret')
                ->check('#tos')
                ->press('#SignUp')
                ->assertPathIs('/play');

            $school->forceDelete();

            $newUser = User::where(['email' => $testEmail])->first();
            $this->assertNotNull($newUser);
            $this->assertSame($newUser->email, $testEmail);
            $newUser->forceDelete();
        });
    }

    public function testPublicCanRegister()
    {
        $testEmail = 'test@selesti.com';
        User::where(['email' => $testEmail])->forceDelete();

        sleep(1);

        $this->browse(function ($browser) use ($testEmail) {
            $browser->visit('/register/public')
                ->type('#name', 'testUser')
                ->type('#email', $testEmail)
                ->type('#password', 'secret');

            $browser->driver->executeScript('jQuery("#dob").val("28/03/1991")');

            $browser->select('#region', '1')
                ->check('#tos')
                ->press('#SignUp')
                ->assertPathIs('/play');

            $newUser = User::where(['email' => $testEmail])->first();
            $this->assertNotNull($newUser);
            $this->assertSame($newUser->email, $testEmail);
            $newUser->forceDelete();
        });
    }
jminkler commented 7 years ago

Getting the same, and adding the sleep, does help.

browner12 commented 7 years ago

do you still have phpspec installed? a couple versions back, a new Laravel install had phpspec installed by default. It was causing some strange issues for me, and after removing it things worked much better.

deleugpn commented 7 years ago

@OwenMelbz and @jminkler do you still need help with this?

OwenMelbz commented 7 years ago

I guess lol - but issue stands that it tries to execute the commands before the driver is fully booted, so not sure what can be done unless the method is overwritten to wait longer

deleugpn commented 7 years ago

Is the same problem happening on the up to date version? Meaning you're able to run a single test file, but not the whole suite?

Doomeyes commented 7 years ago

I had the exact same issue. I found this excerpt on; http://php.net/manual/en/features.connection-handling.php

Excerpt: I finally found out what was wrong: The session only gets closed by PHP at the very end of the script, and since access to the session data is locked to prevent more than one page writing to it simultaneously, the new page cannot load until the original processing has finished.

It got me thinking, so I simply did : session()->flush() at the very end of each of my individual test scripts and they all started working together, each one running right after the other, like you would expect. No need for sleep().

Here is what the short version looks like:

class Registration1Test extends DuskTestCase {

public function testRegistrationTest1()
{

    $this->browse(function ($browser) {
         // run the tests
    });

   session()->flush();

}

}

I hope this helps!

i-radwan commented 7 years ago

Unfortunately, this didn't work either 😞

OwenMelbz commented 7 years ago

@hemoali if your problem isn't resolved by adding a sleep() then your issue is probably something different, so probably worth posting your full issue

Doomeyes commented 7 years ago

@hemoali - just a note, my solution didn't work if all my tests were in the same file ( as the script would not have ended as noted in the excerpt above). When I put the tests in their own individual files (allowing for each individual script to end), then it worked like a charm. Not sure if that applies to your situation, but something I noted when testing. Prior to flushing the session info, neither one worked.

cerw commented 7 years ago

getting same, very radnomly.

dmitryuk commented 7 years ago

same bug ( After restart node-chrome in docker, tests passed successfull

cerw commented 7 years ago

This issue is very random even in docker it just happens ever now and then, nothing seem to fix, it.

carlosadames commented 7 years ago

Any solution yet? Same issue.

cerw commented 7 years ago

I found a solution for this: in my gitlab-runner i have put

shm_size = 536870912

that fixed all "no sessions" errors.

lsfiege commented 7 years ago

the solution of @cerw worked for me, docs for this config here

cerw commented 7 years ago

This is my gitlab-runner for docker. @lsfiege

config.toml

concurrent = 1
check_interval = 0

[[runners]]
  name = "google-gitlab-runner"
  url = "https://xxxxxx/"
  token = "xxxxxxx"
  executor = "docker"
  environment = ["COMPOSER_CACHE_DIR=/cache"]
  [runners.docker]
    tls_verify = false
    image = "php:7.0"
    privileged = true
    security_opt = ["seccomp:unconfined"]
    disable_cache = false
    cache_dir = "/cache"
    volumes = ["/var/cache:/cache:rw"]
    shm_size = 536870912
[runners.cache]

I run this docker image

essivision commented 6 years ago

Where is docker config

lsfiege commented 6 years ago

Is in /etc/gitlab-runner, here is my config:

concurrent = 1
check_interval = 0

[[runners]]
  name = ******************************
  url = "https://gitlab.com/"
  token = "*************************************"
  executor = "docker"
  [runners.docker]
    tls_verify = false
    image = "lsfiege/laravel-dusk-ci"
    privileged = true
    disable_cache = false
    volumes = ["/cache"]
    shm_size = 536870912
  [runners.cache]
essivision commented 6 years ago

I'm work on Windows 10

msonowal commented 6 years ago

I found a solution for this: in my gitlab-runner i have put

shm_size = 536870912

that fixed all "no sessions" errors.

Thanks