1

Using Laravel 4, I am trying to run some selenium tests but want to auto login users. I have the following but it seems the session ID in the browser differs to that which I can get through the test.

$this->_user = User::create([
    ...
]);

Auth::login($this->_user);
... or ...
$this->app['auth']->login($this->_user);

Neither work (even with $this->startSession() ).

I have also tried getting the session id from redis and preceeding the two above calls with $this->app['session']->setId($id);

What's the correct way of modifying the session that the browser has?

Edit: I think the problem comes from Auth\Guard::getName generating a unique id

Ashley
  • 5,939
  • 9
  • 39
  • 82

2 Answers2

0

If you want to use Selenium to fill in and submit a login form, you don't need to worry about the unique session ID. Selenium is able to login to the website just like a normal user.

However, if you want to bypass the login page and directly login to the website, you may need to save the login authentication cookies and then re-use it in all your test scripts. You can refer to this example. It is in Python though.

Community
  • 1
  • 1
userpal
  • 1,483
  • 2
  • 22
  • 38
0

Simply use Auth::loginAsId() within a controller.

On Laravel 4.2, I extended Laracasts\Integrated into Selenium2 and then extended my tests from there. tests/Selenium2.php looked something like this:

use Laracasts\Integrated\Extensions\Selenium;
use Laracasts\Integrated\Services\Laravel\Application as Laravel;

class Selenium2 extends Laracasts\Integrated\Extensions\Selenium {

use Laravel;

protected $baseUrl = 'https://www.myapp.loc';

/**
 * Go to an arbitrary url
 *
 * @param $url
 * @return $this
 */
protected function goToUrl($url)
{
    $url = $this->baseUrl() . $url;
    $this->currentPage = $url;
    $this->session->open($url);
    return $this;
}

}

Notice that it uses $this->session, which is an instance of the built-in webdriver, which you'll need to use in order to keep the session.

For Laravel 5.3, Something like this:

Route::get('loginasid/{id}', function($id) {
    if ( App::environment('testing')) {
        Auth::loginUsingId($id);
    }
}

then in your test

$this->visit( 'loginasid/' . $user->id );
$this->visit( 'otherpage' );

This is tested on Laravel 5.3. Your question asks for 4.x so I included answers for both, but you're better off upgrading to 5.3, the first step of which is to write your tests ;)

iateadonut
  • 1,951
  • 21
  • 32