Search code examples
phplaravellaravel-dusklaravel-dusk2

How can I take a screenshot using duncan3dc/dusk library?


I am using the duncan3dc module in laravel, Successfully importing text from the default example But I do not know how to take a screenshot,

I just installed this module using Composer.

$ composer requires duncan3dc/dusk.

Does it mean that everything needed for this module is automatically installed?

Or do I need something else to take a screenshot?

Below is my controller code

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use duncan3dc\Laravel\Dusk;

class TestController extends Controller
{

    public function test()
    {
        $dusk = new Dusk;
        $dusk->visit("http://example.com");
        $dusk->screenshot("ddd");
        echo $dusk->element("h1")->getText() . "\n";
    }
}

When I run the above code, I get this error:

file_put_contents (/tmp/ddd.png): failed to open stream: No such file or directory

I think there is no folder, so I get the error, where should I create the folder in the project folder?


Solution

  • According to the changelog from version 0.8.0 you can pass fully qualified path to screenshot method. So doing this will work (you should see the file ddd.png created near the php file):

    $dusk = new Dusk;
    $dusk->visit("http://example.com");
    $dusk->screenshot("./ddd.png");
    echo $dusk->element("h1")->getText() . "\n";
    

    You can also pass absolute path:

    $dusk->screenshot("C:\windows\temp\ddd.png"); // if you are using windows
    

    or

    $dusk->screenshot("/tmp/ddd.png"); // if you are using macos/linux