Created
March 15, 2019 22:06
-
-
Save martinbean/7ac08e77cc13f295a1bdae77cad1ce78 to your computer and use it in GitHub Desktop.
Laravel Dusk example test
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace Tests\Browser\Pages; | |
use Laravel\Dusk\Browser; | |
class CartPage extends Page | |
{ | |
/** | |
* {@inheritDoc} | |
*/ | |
public function url() | |
{ | |
return '/cart'; | |
} | |
/** | |
* {@inheritDoc} | |
*/ | |
public function assert(Browser $browser) | |
{ | |
$browser->assertPathIs($this->url()); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace Tests\Browser\Pages; | |
use Laravel\Dusk\Browser; | |
class CheckoutPage extends Page | |
{ | |
/** | |
* {@inheritDoc} | |
*/ | |
public function url() | |
{ | |
return '/checkout'; | |
} | |
/** | |
* {@inheritDoc} | |
*/ | |
public function assert(Browser $browser) | |
{ | |
$browser->assertPathIs($this->url()); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace Tests\Browser; | |
use App\Order; | |
use App\Product; | |
use App\User; | |
use Illuminate\Foundation\Testing\DatabaseMigrations; | |
use Illuminate\Support\Facades\Hash; | |
use Laravel\Dusk\Browser; | |
use Tests\Browser\Pages\CartPage; | |
use Tests\Browser\Pages\CheckoutPage; | |
use Tests\Browser\Pages\OrderConfirmationPage; | |
use Tests\Browser\Pages\ProductPage; | |
use Tests\DuskTestCase; | |
class CheckoutTest extends DuskTestCase | |
{ | |
use DatabaseMigrations; | |
private $product; | |
private $user; | |
protected function setUp(): void | |
{ | |
parent::setUp(); | |
$this->product = factory(Product::class)->state('active')->create(); | |
$this->user = factory(User::class)->create([ | |
'email' => 'john.doe@example.com', | |
'password' => Hash::make('secret'), | |
]); | |
} | |
public function testUserCanCompletePurchase(): void | |
{ | |
$this->browse(function (Browser $browser) { | |
$browser | |
->visit(new ProductPage($this->product)) | |
->click('@sku') | |
->click('@add-to-cart'); | |
$browser | |
->on(new CartPage) | |
->click('@checkout') | |
->type('email', 'john.doe@example.com') | |
->type('password', 'secret') | |
->click('@login'); | |
$browser | |
->on(new CheckoutPage) | |
->type('first_name', 'John') | |
->type('last_name', 'Doe') | |
->type('email', 'john.doe@example.com') | |
->type('street_address', '1241 Main Street') | |
->type('locality', 'Stamford') | |
->type('region', 'CT') | |
->type('postal_code', '06902') | |
->select('country', 'US') | |
->click('@proceed-to-payment'); | |
$order = Order::latest()->firstOr(function () { | |
$this->fail('Could not find any orders.'); | |
}); | |
$browser | |
->on(new OrderConfirmationPage($order)) | |
->assertSee($this->product->name) | |
->assertSee('John Doe') | |
->assertSee('1241 Main Street') | |
->assertSee('Stamford') | |
->assertSee('CT') | |
->assertSee('06902') | |
->assertSee('US'); | |
}); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace Tests\Browser\Pages; | |
use Laravel\Dusk\Browser; | |
class LoginPage extends Page | |
{ | |
/** | |
* {@inheritDoc} | |
*/ | |
public function url() | |
{ | |
return '/login'; | |
} | |
/** | |
* {@inheritDoc} | |
*/ | |
public function assert(Browser $browser) | |
{ | |
$browser->assertPathIs($this->url()); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace Tests\Browser\Pages; | |
use App\Order; | |
use Laravel\Dusk\Browser; | |
class OrderConfirmationPage extends Page | |
{ | |
/** | |
* The order instance. | |
* | |
* @var \App\Order | |
*/ | |
private $order; | |
/** | |
* Create a new page instance. | |
* | |
* @param \App\Order $order | |
* @return void | |
*/ | |
public function __construct(Order $order) | |
{ | |
$this->order = $order; | |
} | |
/** | |
* Get the URL for the page. | |
* | |
* @return string | |
*/ | |
public function url() | |
{ | |
return route('order.show', $this->order); | |
} | |
/** | |
* Assert that the browser is on the page. | |
* | |
* @param Browser $browser | |
* @return void | |
*/ | |
public function assert(Browser $browser) | |
{ | |
$browser->assertUrlIs($this->url()); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace Tests\Browser\Pages; | |
use App\Product; | |
use Laravel\Dusk\Browser; | |
class ProductPage extends Page | |
{ | |
/** | |
* The product instance. | |
* | |
* @var \App\Product | |
*/ | |
private $product; | |
/** | |
* Create a new page instance. | |
* | |
* @param \App\Product $product | |
* @return void | |
*/ | |
public function __construct(Product $product) | |
{ | |
$this->product = $product; | |
} | |
/** | |
* {@inheritDoc} | |
*/ | |
public function url() | |
{ | |
return $this->product->url(); | |
} | |
/** | |
* {@inheritDoc} | |
*/ | |
public function assert(Browser $browser) | |
{ | |
$browser->assertUrlIs($this->url()); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment