Dusk: Laravel Dusk on Travis CI - Unable to locate element

Created on 28 Feb 2018  路  18Comments  路  Source: laravel/dusk

Hello together. I'm currently facing a problem with Laravel Dusk on Travis CI.

Error

2) Tests\Browser\Tests\Auth\SignInTest::a_user_can_sign_in
Facebook\WebDriver\Exception\NoSuchElementException: no such element: Unable to locate element: {"method":"css selector","selector":"body textarea[name='#email']"}
  (Session info: headless chrome=64.0.3282.186)
  (Driver info: chromedriver=2.35.528139 (47ead77cb35ad2a9a83248b292151462a66cd881),platform=Linux 4.4.0-101-generic x86_64)

Local Environment
I've to mention that all tests (phpunit & dusk) are working on my local machine.

Travis Environment
Except of the tests where an element is needed als tests are working as well.

Set Travis ENV as well.
Link to Travis-CI Test-Message
Full Error Message Log from Travis-CI

I also tried different ways of selecting the element like:

  • -> type('email', $user->email)
  • ->type('@email', $user->email and then define it in the elements method.

454 Checked already this out. Not working as well for me. Unfortunately am at the end with my knowledge.. any suggestions?

Cheers, Stan

Sign-In-Page

public function assert(Browser $browser)
    {
        $browser->assertPathIs($this->url());
    }

    public function signIn(Browser $browser, $email = null, $password = null)
    {
        $browser
            ->resize(1920, 1080)
            ->type('@login-email', $email)
            ->type('@login-password', $password)
            ->click('@login-button');
    }


    /**
     * Get the element shortcuts for the page.
     *
     * @return array
     */
    public function elements()
    {
        return [
        ];
    }

Sign-In-Test

public function a_user_can_sign_in()
        {
            $path = route('login');

            $user = factory(User::class)->create([

                'name' => 'Max Mustermann',
                'email' => '[email protected]',
                'password' => bcrypt('password')

            ]);

            $this->browse(function ($browser) use ($path, $user) {
                $browser
                    ->visit(new SignInPage)
                    ->signIn($user->email, 'password')
                    ->assertPathIs('/backend/users/dashboard')
                    ->assertSeeIn('.navbar', $user->name);
            });
        }

Login Template Blade

@if ($errors->has('email'))
{{ $errors->first('email') }}
                            @endif
                        </div>
                    </div>

.env.dusk.testing

    APP_NAME=Testing
    APP_ENV=testing
    APP_KEY=
    APP_DEBUG=true
    APP_LOG_LEVEL=debug
    APP_URL=http://do.testing.test

    DB_CONNECTION=pgsql
    DB_DATABASE=testing
    DB_USERNAME=postgres
    DB_PASSWORD=

    BROADCAST_DRIVER=log
    CACHE_DRIVER=array
    SESSION_DRIVER=database
    QUEUE_DRIVER=sync

    MAIL_FROM_NAME= Testing
    [email protected]

    MAIL_DRIVER=log

travis.yml

sudo: true

dist: trusty

language: php

env:
  global:
    - CC_TEST_REPORTER_ID=

addons:
  chrome: stable

  code_climate:
    repo_token:
      secure:

php:
  - 7.2

services:
  - redis-server
  - postgres

before_script:
   - psql -c 'create database testing;' -U postgres
   - cp .env.travis .env
   - cp phpunit.travis.xml phpunit.xml

    - google-chrome-stable --headless --disable-gpu --remote-debugging-port=9222 http://localhost &
    - php artisan serve &

install:
   - travis_retry composer install --no-interaction --prefer-dist --no-suggest

   - cp .env.travis .env
   - cp phpunit.travis.xml phpunit.xml

   - php artisan key:generate

script:
  - phpunit
  - php artisan dusk

after_script:
  - vendor/bin/test-reporter

after_success:
  - chmod +x ./tests.sh; ./tests.sh

Most helpful comment

This problem is solved easily, specifying the url in the .env file, in my case, as I am using a virtual host, change APP_URL = http: //app.local instead of APP_URL = http: // localhost (by default in laravel). This way you can access the view and laravel dusk can find the input and what you define to complete your test.

All 18 comments

If you dump the page source before ->type(), do you get the expected HTML?

hey @staudenmeir thanks for your message, the dump seems to be correct. I can see all expected elements. Dump Travis CI

Can you find other elements? Something like this: $browser->element('body');

Is the HTML valid?

Hi @staudenmeir @StanBarrows, im facing a similar issue. How do you actually dump the page content from a dusk test?

found it: ->dump()

found it: ->dump()

This problem is solved easily, specifying the url in the .env file, in my case, as I am using a virtual host, change APP_URL = http: //app.local instead of APP_URL = http: // localhost (by default in laravel). This way you can access the view and laravel dusk can find the input and what you define to complete your test.

@StanBarrows Are you still having this issue?

Closing this issue because it's already solved, old or not relevant anymore. Feel free to reply if you're still experiencing this issue.

Do you see the email field when you append ->dump(); after ->assertSee('Login')?

Do you see the email field when you append ->dump(); after ->assertSee('Login')?

No cannot see that, sorry for late reply

Do you see the rest of the page? Or is the dump basically empty?

Are you using JavaScript to insert the email field?

Do you see the rest of the page? Or is the dump basically empty?

Are you using JavaScript to insert the email field?
ooh sorry, i can see the email field actually
i didnt notice it at first time

Did you delete your original post?

Did you delete your original post?
yes, this is the test function and the error :

public function testExample()
{
$this->browse(function (Browser $browser) {
$browser->visit('/#!/auth')
->assertSee('Email');
->type('email', '[email protected]')
->type('password', 'abc123')
->press('Log in')
->assertPathIs('/#!');
});

}

this is the error i got:
TestsLoginTest::testExample
FacebookWebDriverExceptionNoSuchElementException: no such element: Unable to locate element: {"method":"css selector
","selector":"body textarea[name='email']"}

Please create a minimal application to reproduce the issue and upload it to GitHub.

Please create a minimal application to reproduce the issue and upload it to GitHub.

I found it!
silly mistake, i have to select the fields based on their Ids not their names. that is why it could not find the email field.
basically, it should be something like this:

$browser->visit('/#!/auth')
->assertSee('Login');
->type('#email', '[email protected]')
->type('#password', 'abc123')
->press('Log in')
->assertPathIs('/#!');
});

Btw, thank you very much for your effort and time.

Another possible solution:

Between load pages or at the star of your Dusk test add a big pause, Travis VM runs considerably slower than your local pc.

// Wait for Travis CI to load the page
if (App::environment('testing')) {
    $browser->pause(10000);
}                              

Was this page helpful?
0 / 5 - 0 ratings

Related issues

fwang-laralabs picture fwang-laralabs  路  6Comments

marcusmoore picture marcusmoore  路  3Comments

dmitryuk picture dmitryuk  路  4Comments

bruno-barros picture bruno-barros  路  6Comments

ahtinurme picture ahtinurme  路  6Comments