Skip to content

Commit cef84bc

Browse files
committed
document components
1 parent b6fd897 commit cef84bc

File tree

1 file changed

+114
-0
lines changed

1 file changed

+114
-0
lines changed

dusk.md

Lines changed: 114 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,9 @@
2525
- [Navigating To Pages](#navigating-to-pages)
2626
- [Shorthand Selectors](#shorthand-selectors)
2727
- [Page Methods](#page-methods)
28+
- [Components](#components)
29+
- [Generating Components](#generating-components)
30+
- [Using Components](#using-components)
2831
- [Continuous Integration](#continuous-integration)
2932
- [Travis CI](#running-tests-on-travis-ci)
3033
- [CircleCI](#running-tests-on-circle-ci)
@@ -629,6 +632,117 @@ Once the method has been defined, you may use it within any test that utilizes t
629632
->createPlaylist('My Playlist')
630633
->assertSee('My Playlist');
631634

635+
<a name="components"></a>
636+
## Components
637+
638+
Components are similar to Dusk’s “page objects”, but are intended for pieces of UI and functionality that are re-used throughout your application, such as a navigation bar or notification window. As such, components are not bound to specific URLs.
639+
640+
<a name="generating-components"></a>
641+
### Generating Components
642+
643+
To generate a component, use the `dusk:component` Artisan command. New components are placed in the `test/Browser/Components` directory:
644+
645+
php artisan dusk:component DatePicker
646+
647+
As shown above, a "date picker" is an example of a component that might exist throughout your application on a variety of pages. It can become cumbersome to manually write the browser automation logic to select a date in dozens of tests throughout your test suite. Instead, we can define a Dusk component to represent the date picker, allowing us to encapsulate that logic within the component:
648+
649+
<?php
650+
651+
namespace Tests\Browser\Components;
652+
653+
use Laravel\Dusk\Browser;
654+
use Laravel\Dusk\Component as BaseComponent;
655+
656+
class DatePicker extends BaseComponent
657+
{
658+
/**
659+
* Get the root selector for the component.
660+
*
661+
* @return string
662+
*/
663+
public function selector()
664+
{
665+
return '.date-picker';
666+
}
667+
668+
/**
669+
* Assert that the browser page contains the component.
670+
*
671+
* @param Browser $browser
672+
* @return void
673+
*/
674+
public function assert(Browser $browser)
675+
{
676+
$browser->assertVisible($this->selector());
677+
}
678+
679+
/**
680+
* Get the element shortcuts for the component.
681+
*
682+
* @return array
683+
*/
684+
public function elements()
685+
{
686+
return [
687+
'@date-field' => 'input.datepicker-input',
688+
'@month-list' => 'div > div.datepicker-months',
689+
'@day-list' => 'div > div.datepicker-days',
690+
];
691+
}
692+
693+
/**
694+
* Select the given date.
695+
*
696+
* @param \Laravel\Dusk\Browser $browser
697+
* @param int $month
698+
* @param int $year
699+
* @return void
700+
*/
701+
public function selectDate($browser, $month, $year)
702+
{
703+
$browser->click('@date-field')
704+
->within('@month-list', function ($browser) use ($month) {
705+
$browser->click($month);
706+
})
707+
->within('@day-list', function ($browser) use ($day) {
708+
$browser->click($day);
709+
});
710+
}
711+
}
712+
713+
<a name="using-components"></a>
714+
### Using Components
715+
716+
Once the component has been defined, we can easily select a date within the date picker from any test. And, if the logic necessary to select a date changes, we only need to update the component:
717+
718+
<?php
719+
720+
namespace Tests\Browser;
721+
722+
use Tests\DuskTestCase;
723+
use Laravel\Dusk\Browser;
724+
use Tests\Browser\Components\DatePicker;
725+
use Illuminate\Foundation\Testing\DatabaseMigrations;
726+
727+
class ExampleTest extends DuskTestCase
728+
{
729+
/**
730+
* A basic component test example.
731+
*
732+
* @return void
733+
*/
734+
public function testBasicExample()
735+
{
736+
$this->browse(function (Browser $browser) {
737+
$browser->visit('/')
738+
->within(new DatePicker, function ($browser) {
739+
$browser->selectDate(1, 2018);
740+
})
741+
->assertSee('January');
742+
});
743+
}
744+
}
745+
632746
<a name="continuous-integration"></a>
633747
## Continuous Integration
634748

0 commit comments

Comments
 (0)