mardi 31 octobre 2017

Laravel Dusk LoginAs doesnt work

I have such a Dusk test:

namespace Tests\Browser\Features\ContentAdministratorSubsystem;

use App;
use App\Model\Data\Models\Account;
use Illuminate\Foundation\Testing\DatabaseMigrations;
use Illuminate\Support\Facades\Session;
use Laravel\Dusk\Browser;
use Tests\DuskTestCase;

class ApproveCommentTest extends DuskTestCase
{

protected $user;

public function setUp() 
{

    parent::setUp();    

}

public function tearDown()
{

    parent::tearDown();

}

/** @test */
public function when_user_confirms_approve_comment_action_its_box_dissappears()
{

    $this->user = factory(Account::class)->create(['status' => 'confirmed', 'type' => 'Content Administrator']);

    $this->browse(function ($first, $second) {
        $first->loginAs(Account::find($this->user->id))->visit(env('APP_URL').'/content-administrator')->assertSee('Sorry, the page you are looking for could not be found.');
    });

}

}

Its my first time I'm testing with Dusk, so all I want to do is to login and visit Content Administrator Page.

So that means that this test should fail. Cause that message can only be seen if something went wrong. I'm 100% sure that env('APP_URL').'/content-administrator' is a correct url, I used dd() to see what it looks like and it is OK. I'm also sure that $this->user->id returns a real ID.

Route::prefix('content-administrator')->middleware('contentAdministrator')->namespace('ContentAdministratorSubsystem')->group(function () {
    Route::get('/', 'MainPageController@main');

    Route::get('/comments/delete/{id}', 'CommentController@delete');
    Route::get('/comments/approve/{id}', 'CommentController@approve');
});

This is my routes file. I have a middleware, but now it does nothing. Just passes request to $next.

When I tried to dd($first->loginAs($this->user->id)) I got this result:

Laravel\Dusk\Browser {#90
  +driver: Facebook\WebDriver\Remote\RemoteWebDriver {#89
    #executor: Facebook\WebDriver\Remote\HttpCommandExecutor {#259
      #url: "http://localhost:9515"
      #curl: curl resource {@397
        url: "http://localhost:9515/session/007a6ed618f11c93335f5710c834b8a1/url"
        content_type: "application/json; charset=utf-8"
        http_code: 200
        header_size: 102
        request_size: 230
        filetime: -1
        ssl_verify_result: 0
        redirect_count: 0
        total_time: 0.469234
        namelookup_time: 0.000219
        connect_time: 0.000403
        pretransfer_time: 0.000611
        size_upload: 53.0
        size_download: 72.0
        speed_download: 72.0
        speed_upload: 53.0
        download_content_length: 72.0
        upload_content_length: 53.0
        starttransfer_time: 0.469201
        redirect_time: 0.0
        redirect_url: ""
        primary_ip: "127.0.0.1"
        certinfo: []
        primary_port: 9515
        local_ip: "127.0.0.1"
        local_port: 50024
      }
    }
    #capabilities: Facebook\WebDriver\Remote\DesiredCapabilities {#91
      -capabilities: array:23 [
        "acceptSslCerts" => true
        "applicationCacheEnabled" => false
        "browserConnectionEnabled" => false
        "browserName" => "chrome"
        "chrome" => array:2 [
          "chromedriverVersion" => "2.32.498513 (2c63aa53b2c658de596ed550eb5267ec5967b351)"
          "userDataDir" => "/tmp/.org.chromium.Chromium.JFlr8n"
        ]
        "cssSelectorsEnabled" => true
        "databaseEnabled" => false
        "handlesAlerts" => true
        "hasTouchScreen" => false
        "javascriptEnabled" => true
        "locationContextEnabled" => true
        "mobileEmulationEnabled" => false
        "nativeEvents" => true
        "networkConnectionEnabled" => false
        "pageLoadStrategy" => "normal"
        "platform" => "Linux"
        "rotatable" => false
        "setWindowRect" => true
        "takesHeapSnapshot" => true
        "takesScreenshot" => true
        "unexpectedAlertBehaviour" => ""
        "version" => "62.0.3202.75"
        "webStorageEnabled" => true
      ]
    }
    #sessionID: "007a6ed618f11c93335f5710c834b8a1"
    #mouse: null
    #keyboard: null
    #touch: null
    #executeMethod: Facebook\WebDriver\Remote\RemoteExecuteMethod {#77
      -driver: Facebook\WebDriver\Remote\RemoteWebDriver {#89}
    }
  }
  +resolver: Laravel\Dusk\ElementResolver {#87
    +driver: Facebook\WebDriver\Remote\RemoteWebDriver {#89}
    +prefix: "body"
    +elements: []
    #buttonFinders: array:5 [
      0 => "findById"
      1 => "findButtonBySelector"
      2 => "findButtonByName"
      3 => "findButtonByValue"
      4 => "findButtonByText"
    ]
  }
  +page: null
  +component: null
}

Im not sure whether it means that it has logged in successfully.

I also get this error.log file:

[
    {
        "level": "SEVERE",
        "message": "http:\/\/mydomain\/ - Failed to load resource: the server responded with a status of 404 (Not Found)",
        "source": "network",
        "timestamp": 1509463678547
    }
]

That "mydomain" in real life is a correct value, I just replaced original one with this in this example to not show the domain.

So why this test succeeds when it shouldn't?

Aucun commentaire:

Enregistrer un commentaire