简体   繁体   中英

How to work with Page Objects in laravel-dusk

I'm a new in a php and dusk, but I try to work with page object in dusk, and I'm stuck because when I try to add page object to test, phpstorm said me that "Method logInUserName not found in $this". Can someone explain to me where i'm wrong?

I have page class:

<?php

namespace Tests\Browser\Pages;

use Laravel\Dusk\Browser;

class LogInPage extends Page
{
/**
 * Get the URL for the page.
 *
 * @return string
 */
public function url()
{
    return '/login';
}

/**
 *
 * @return void
 */
public function logInUserName(Browser $browser)
{
   $browser->type("#username", "lol");
}

}

I have test class

use Tests\Browser\Pages\LogInPage;
use Facebook\WebDriver\Remote\RemoteWebDriver;
use Tests\DuskTestCase;
use Laravel\Dusk\Browser;
use PHPUnit\Framework\Assert;

class ExampleTest extends DuskTestCase
{
/**
 * A basic browser test example.
 *
 * @return void
 */
public function testLogInFail()
{
    $this->browse(function (Browser $browser) {
        $browser
            ->visit(new LogInPage)
            ->logInUserName()
            ->keys("#password","lol")
            ->click("button.btn-primary"));}

Agree this is annoying, there are 2 ways you could get around this

  1. Restart the chaining on the browser object, you may still get a warning about logInUserName but you get your code assist back, which I agree can be useful when still learning.
$browser
    ->visit(new LogInPage)
    ->logInUserName();
$browser
    ->keys("#password","lol")
    ->click("button.btn-primary"));
  1. Create a helper file defining your custom functions

Or use this gist and create a file in the root of your project that your IDE will read - https://gist.github.com/slava-vishnyakov/5eb90352fc97702f53a41888e5bae27a

Only issue is you may get a PHPSTORM warning about multiple definitions exist for class Browser...not sure how to get around that

Results in something like this

<?php

namespace Laravel\Dusk {
    class Browser
    {
        /**
         * @return Browser
         */
        public function logInUserName()
        {
        }
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM