laravel / dusk

Laravel Dusk provides simple end-to-end testing and browser automation.
https://laravel.com/docs/dusk
MIT License
1.88k stars 323 forks source link

Error on changing the tab #336

Closed guirociozanini closed 7 years ago

guirociozanini commented 7 years ago

I'am getting this error, i'am trying to change the tab of the page.

1) Tests\Browser\RegisterTest::testExample
ErrorException: Only variables should be passed by reference

/var/www/html/dusk/tests/Browser/RegisterTest.php:59
/var/www/html/dusk/vendor/laravel/dusk/src/TestCase.php:91
/var/www/html/dusk/tests/Browser/RegisterTest.php:66

<?php

namespace Tests\Browser;

use Tests\DuskTestCase;
use Laravel\Dusk\Browser;
use Carbon\Carbon;
use Illuminate\Foundation\Testing\DatabaseMigrations;

class RegisterTest extends DuskTestCase
{
    /**
     * A Dusk test example.
     *
     * @return void
     */

    public function testExample()
    {

        $this->browse(function (Browser $browser) {
            $browser->visit('/')

                //Espera pelo icone de acesso de prestador e clica
                ->waitFor('img[src="imgs/001.gif"]')
                ->click('img[src="imgs/001.gif"]')

                //Verifica se o form de login está na tela
                ->assertSee('Forma Acesso')

                //Verifica se o campo CNPJ está selecionado
                ->radio('cpf_cnpj', 'cnpj')
                ->assertRadioSelected('cpf_cnpj', 'cnpj')

                //Preenche os campos de acesso e realiza o login
                ->value('#ext-gen29', 'user')
                ->value('#ext-gen33', 'pass')
                ->click('#ext-gen42')

                //Espera pelo icone de consulta da nota fiscal e clica
                ->waitFor('img[src="imgs/icon_nfse5.gif"]')
                ->click("img[src='imgs/icon_nfse5.gif']")

                //Verifica se o radio Periodo está selecionado
                ->waitFor('input[name="opcConsulta"]')
                ->radio('opcConsulta', 'periodo')
                ->assertRadioSelected('opcConsulta', 'periodo')

                //Preenche os campos de data De: / Até:
                ->valueByPosition('div > input', 1, Carbon::now()->subMonth()->startOfMonth()->format('d/m/Y'))
                ->valueByPosition('div > input', 2, Carbon::now()->subMonth()->endOfMonth()->format('d/m/Y'))
                ->click('.x-btn-text')

                //Clicar e baixar a nota
                ->waitFor('.viewNota', 30)
                ->click('.viewNota')

                ->driver->switchTo()->window(end($browser->driver->getWindowHandles()))

                ->waitFor('.titulo', 10)
                ->click('.titulo');

        });
    }
}
deleugpn commented 7 years ago

Try something like this instead:

// Collect all tabs and grab the last one (recently opened).
$window = collect($browser->driver->getWindowHandles())->last();

// Switch to the new tab that contains the screenshot
$browser->driver->switchTo()->window($window);
guirociozanini commented 7 years ago

I'am getting another error now:

1) Tests\Browser\RegisterTest::testExample
Error: Call to undefined method Facebook\WebDriver\Remote\RemoteWebDriver::assertSee()
                // Switch to the new tab that contains the screenshot
                ->driver->switchTo()->window(collect($browser->driver->getWindowHandles())->last())                 

                ->assertSee('Exportar PDF')
                ->clickLink('Exportar PDF');
deleugpn commented 7 years ago

You cannot chain that call. You need to store a reference to the browser.

guirociozanini commented 7 years ago

How do I store the reference to the browser?

deleugpn commented 7 years ago
$response = $browser->visit('/')

                //Espera pelo icone de acesso de prestador e clica
                ->waitFor('img[src="imgs/001.gif"]')
                ->click('img[src="imgs/001.gif"]')

                //Verifica se o form de login está na tela
                ->assertSee('Forma Acesso')

                //Verifica se o campo CNPJ está selecionado
                ->radio('cpf_cnpj', 'cnpj')
                ->assertRadioSelected('cpf_cnpj', 'cnpj')

                //Preenche os campos de acesso e realiza o login
                ->value('#ext-gen29', 'user')
                ->value('#ext-gen33', 'pass')
                ->click('#ext-gen42')

                //Espera pelo icone de consulta da nota fiscal e clica
                ->waitFor('img[src="imgs/icon_nfse5.gif"]')
                ->click("img[src='imgs/icon_nfse5.gif']")

                //Verifica se o radio Periodo está selecionado
                ->waitFor('input[name="opcConsulta"]')
                ->radio('opcConsulta', 'periodo')
                ->assertRadioSelected('opcConsulta', 'periodo')

                //Preenche os campos de data De: / Até:
                ->valueByPosition('div > input', 1, Carbon::now()->subMonth()->startOfMonth()->format('d/m/Y'))
                ->valueByPosition('div > input', 2, Carbon::now()->subMonth()->endOfMonth()->format('d/m/Y'))
                ->click('.x-btn-text')

                //Clicar e baixar a nota
                ->waitFor('.viewNota', 30)
                ->click('.viewNota');

// Collect all tabs and grab the last one (recently opened).
$window = collect($response->driver->getWindowHandles())->last();

// Switch to the new tab that contains the screenshot
$response->driver->switchTo()->window($window);

                $response->waitFor('.titulo', 10)
                ->click('.titulo');
guirociozanini commented 7 years ago

I did this and worked:

            ->driver->switchTo()->window(collect($browser->driver->getWindowHandles())->last()); 

             $browser

             ->waitFor('.titulo')
             ->click('.titulo')
             ->pause(500)

I have another doubt, is it possible to download a static page pdf? Example:

www.mywebsite.com/19208312093821.pdf

Cause when dusk presses the download button, a popup opens to click Save (ubuntu window)