2

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"));}
  • How should this work after all? You are calling `visit` on a `Browser` instance in your test. How does this relate to the `LogInPage` class? Is there any method called `logInUserName` in the object returned by `visit`? – Nico Haase Feb 21 '18 at 21:44
  • On the `LogInPage` class described `logInUserName` function, where I've type text to the input field `$browser->type("#username", "lol");`. `visit` default method in the dusk [https://laravel.com/docs/5.5/dusk] – Qwersss qqqwqw Mar 09 '18 at 13:13
  • @nico-haase Actually, this code is working, but I can't use auto-complete after `logInUserName`, php storm shows 'no suggestions'. Very hard to write code without auto-complete – Qwersss qqqwqw Mar 09 '18 at 13:20

1 Answers1

1

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()
        {
        }
    }
}
Carlton
  • 5,533
  • 4
  • 54
  • 73