Laravel dusk testing cheat sheet

 //test a URI 
 $browser->visit('/login'); 

  //test a route
 $browser->visitRoute('login'); 

 //browser back

 $browser->back(); 

 $browser->forward(); //browser forward

 $browser->refresh(); //refresh page

 $browser->resize(1920, 1080); //resize browser window

 $browser->maximize(); //maximise browser window

 $browser->fitContent(); //maximise to fit the content of the browser

 $browser->disableFitOnFailure(); // disable above operation on failure

 $browser->move($x = 100, $y = 100); // move browser window to different positions

 //test page which require authentication, after loginAs method user session will be maintained

 use App\Models\User;
 $this->browse(function ($browser)) {
     $browser->loginAs(User::find(1)) 
         ->visit('/home');
 });

 $browser->cookie('name');
 $browser->cookie('name','test');

 $browser->screenshot('filename'); //this will store the screenshot test/browser/console directory

 $browser->storeSource('filename');
 //prefix @ to use dusk selector
 Login
 $browser->click('@login-button');

 //set value for a input field using value method
 $browser->value('selector', 'value');

 //set value for a input field using inputValue method
 $browser->inputValue('field');

 //display text for an element for the give selector
 $browser->text('selector');

 //get attributes for a matching the field
 $browser->attribute('selector', 'value');

 //type text into input field
 $browser->type('email','[email protected]');

 //use append method to add more content without clearing previous data
 $browser->type('tags','hello')
         ->append('tags' 'world');

 //clear value of an input field
 $browser->clear('email');

// Dusk will type input field slowly (100ms default)
 $browser->typeslowly('phone', '123 456 789');

 $browser->type('tags', 'foo')
         ->appendSlowly('tags', ', bar, baz');

 // select a value for a dropdown field
 $browser->select('size','large');

 //select a random option for dropdown with one argument
 $browser->select('size');

 //tick a checkbox, terms is the name of checkbox field
 $browser->check('terms');

 //untick a checkbox
 $browser->uncheck('terms');

 //select a radio button
 $browser->radio('size', 'large');

 //attach a file
 $browser->attach('photo', DIR.'/test.png');

 //press a button
 $browser->press('Login');


/wait for 5sec and then press the button
 $browser->pressAndWaitFor('Save');

 //wait for 1sec and then press the button

 $browser->pressAndWaitFor('Save', 1);
 //click a link which displays text "click here"
 $browser->clickLink("click here");

 // check if you can see the link text
 $browser->seeLink("click here");

Leave a Comment

Your email address will not be published. Required fields are marked *