Giter Club home page Giter Club logo

laravel-visual-testing's Introduction

Visual UI screenshot testing for Laravel Dusk

Latest Version on Packagist Software License Build Status

This package extends Dusk with the ability to do visual diffs with the Percy visual testing platform.

Why write visual tests?

If you are new to the idea of visual testing we recommend reading through Visual testing and visual diffs on the Percy blog.

Sometimes called visual regression testing or UI testing, visual testing is the process of automatically discovering and reviewing software for perceptual changes.

Visual testing is all about what your users actually see and interact with.

This form of testing is very useful in cases where you want to guard against unexpected changed to your UI. Visual testing is not meant to replace your Laravel unit/feature/browser tests, but rather provide another tool in your testing toolbox.

Getting started

This package integrates with Laravel Dusk. If you haven't already, first go through the Dusk installation steps and make sure you can run the example test with php artisan dusk.

Next:

  1. Sign up for a free account at percy.io and create your first project. Put your PERCY_TOKEN in your Laravel .env file (or specific dusk environment files if you are using those).

    PERCY_TOKEN=aaabbbcccdddeeefff
    
  2. Install the @percy/agent NPM package.

    npm install --save-dev @percy/agent
    
  3. Install this composer package.

    composer require stechstudio/laravel-visual-testing --dev
    

How to use

To take a snapshot call snapshot() on the browser instance in any of your Dusk tests.

$browser->visit('/auth/login')
        ->snapshot();

Then run your test suite like your normally would.

php artisan dusk

Naming your snapshots

By default the name of your snapshot will be the relative URL of the page (e.g. /auth/login). You can also pass in your own name when taking the snapshot.

$browser->visit('/auth/login')
        ->snapshot('Login page');

Snapshot options

You can pass in an array of options when taking a snapshot:

  • widths: An array of integers representing the browser widths at which you want to take snapshots.
  • minHeight: An integer specifying the minimum height of the resulting snapshot, in pixels. Defaults to 1024px.
$browser->visit('/auth/login')
        ->snapshot('Login page', [ 'widths' => [768, 992, 1200] ]);

Disabling snapshots

If you want to run your tests without snapshots, use the --without-percy command line option.

Selecting base build branch

Percy uses a variety of strategies to determine the optimal base build for comparison. For details see Base build selection.

If you want to override and specify your own base you have two options:

  • --percy-target-branch : Specify base by branch name
  • --percy-target-commit : Specify by target commit SHA (only works if there is a finished Percy build for that commit)

Basic example

Open the example test at tests/Browser/ExampleTest.php. Add a call to snapshot() right after the visit, and pass in a name for your snapshot.

public function testBasicExample()
{
    $this->browse(function (Browser $browser) {
        $browser->visit('/')
            ->snapshot('basic-example') // <-- add this
            ->assertSee('Laravel');
                
    });
}

Now go run your test:

php artisan dusk

If all goes well, you should see output similar to this:

$ php artisan dusk
[percy] created build #1
[percy] percy has started.

[percy] snapshot taken: 'basic-example'
.                                                                   1 / 1 (100%)

Time: 2.37 seconds, Memory: 22.00MB

OK (1 test, 1 assertion)
[percy] stopping percy...
[percy] waiting for 1 snapshots to complete...
[percy] done.
[percy] finalized build #1

Now go check out your Percy dashboard, and you should see the new build.

At this point it won't have anything to compare the snapshot to. But if you go modify the welcome.blade.php file and run it again, you'll get a nice visual diff of your change.

laravel-visual-testing's People

Contributors

chrysanthos avatar inventionate avatar jszobody avatar mattias-persson avatar mattrabe avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar

laravel-visual-testing's Issues

Pest + Dusk + Sail

I am trying to get this setup with a new Laravel App.

I am using Laravel sail to develop in docker and I have Pest and Laravel Dusk setup and working. (php artisan dusk --pest --without-percy works perfectly)

Now I wanted to add this but when I run php artisan dusk --pest I receive this error message:

[percy] Config file not found
[percy] created build #3: https://percy.io/1e6dcf4c/esg-radar/builds/12960345
    Error: Failed to launch the browser process!
    /var/www/html/node_modules/puppeteer/.local-chromium/linux-818858/chrome-linux/chrome: error while loading shared libraries: libnss3.so: cannot open shared object file: No such file or directory


    TROUBLESHOOTING: https://github.com/puppeteer/puppeteer/blob/main/docs/troubleshooting.md

It seems that percy does not know anything about the remote selenium instance nor how to use it.
Do you see a way we could get this working? Or is this a lost cause?

Support Laravel 6.x

This package currently fails with Call to undefined function STS\VisualTesting\Console\starts_with() when running on Laravel 6+.

Many array and string helpers were removed in Laravel 6, and now have to be called as static class functions. For example: starts_with() is now only Str::startsWith() and array_get() is now only Arr:get().

I found a fork by @alvaxet which addresses at least some of this: https://github.com/alvaxet/laravel-visual-testing/commit/676d3f3e00455d9defa5b8407aa2c489c9fadebe and will be digging a bit deeper starting now...

UPDATE: alvaxet's PR only addresses one of the two helpers in the codebase, so I have created my own fork, fixed both, and opened a PR: #3

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.