### Install Playwright Laravel Package Source: https://github.com/hyvor/laravel-playwright/blob/main/README.md Installs the Playwright Laravel package for use in Playwright end-to-end tests via npm. ```bash npm install @hyvor/laravel-playwright ``` -------------------------------- ### Install Laravel Playwright Package Source: https://github.com/hyvor/laravel-playwright/blob/main/README.md Installs the Laravel Playwright package as a development dependency in a Laravel application using Composer. ```bash composer require --dev hyvor/laravel-playwright ``` -------------------------------- ### Fetch Playwright Endpoint Example (JavaScript) Source: https://github.com/hyvor/laravel-playwright/blob/main/README.OLD.md Demonstrates how to make a POST request to a Playwright endpoint in a JavaScript environment. ```javascript fetch('/playwright/artisan', { method: 'POST', headers: { 'Content-Type': 'application/json', }, body: JSON.stringify(INPUT_DATA), }) ``` -------------------------------- ### Execute Select Query and Get Results (JSON Input) Source: https://github.com/hyvor/laravel-playwright/blob/main/README.OLD.md Shows the JSON format for running SELECT queries and receiving the results as an array of objects. ```json { "query": "SELECT * FROM users WHERE id = 1", "connection": "mysql" } ``` -------------------------------- ### Configure Laravel Playwright Source: https://github.com/hyvor/laravel-playwright/blob/main/README.OLD.md Configures the Laravel Playwright package by adding an 'e2e' key to the application's configuration file. ```php return [ // ... 'e2e' => [ /** * The prefix for the testing endpoints that are used to interact with Playwright * Make sure to change `use.laravelBaseUrl` in playwright.config.ts if you change this */ 'prefix' => 'playwright', /** * The environments in which the testing endpoints are enabled */ 'environments' => ['local', 'testing'], ], ]; ``` -------------------------------- ### Create Model with Factory (JSON Input) Source: https://github.com/hyvor/laravel-playwright/blob/main/README.OLD.md Illustrates the JSON payload for creating model instances using Laravel factories, including options for count and attributes. ```json { "model": "User", "attributes": { "name": "John Doe" } } ``` ```json { "model": "App\\Database\\Models\\User", "count": 5 } ``` -------------------------------- ### POST /playwright/query Source: https://github.com/hyvor/laravel-playwright/blob/main/README.OLD.md Executes raw SQL queries against a specified database connection. ```APIDOC ## POST /playwright/query ### Description Executes a raw SQL query against the database. Suitable for data manipulation. ### Method POST ### Endpoint /playwright/query ### Parameters #### Request Body - **query** (string) - Required - The SQL query to execute. - **connection** (string) - Optional - The database connection to use (e.g., `mysql`). Defaults to the primary connection. ### Request Example ```json { "query": "UPDATE users SET name = 'John Doe' WHERE id = 1", "connection": "mysql" } ``` ### Response #### Success Response (200) Indicates successful execution of the query. ``` -------------------------------- ### Run Artisan Commands (JSON Input/Output) Source: https://github.com/hyvor/laravel-playwright/blob/main/README.OLD.md Shows the JSON payload for running Artisan commands via the Playwright endpoint and the expected JSON response. ```json { "command": "migrate:fresh", "parameters": ["--seed"] } ``` ```json { "code": 0, "output": "" } ``` -------------------------------- ### POST /playwright/select Source: https://github.com/hyvor/laravel-playwright/blob/main/README.OLD.md Executes raw SQL SELECT queries and returns the results. ```APIDOC ## POST /playwright/select ### Description Executes a raw SQL SELECT query and returns the results as an array of objects. ### Method POST ### Endpoint /playwright/select ### Parameters #### Request Body - **query** (string) - Required - The SQL SELECT query to execute. - **connection** (string) - Optional - The database connection to use (e.g., `mysql`). Defaults to the primary connection. ### Request Example ```json { "query": "SELECT * FROM users WHERE id = 1", "connection": "mysql" } ``` ### Response #### Success Response (200) - Returns an array of objects, where each object represents a row from the query result. ``` -------------------------------- ### Configure Laravel E2E Endpoints Source: https://github.com/hyvor/laravel-playwright/blob/main/README.md Configures the testing endpoints for Playwright interaction within a Laravel application by defining an 'e2e' key in the `config/app.php` file. Allows customization of the endpoint prefix and the environments in which these endpoints are enabled. ```php return [ // ... 'e2e' => [ /** * The prefix for the testing endpoints that are used to interact with Playwright * Make sure to change `use.laravelBaseUrl` in playwright.config.ts if you change this */ 'prefix' => 'playwright', /** * The environments in which the testing endpoints are enabled * CAUTION: Enabling the testing endpoints in production can be a critical security issue */ 'environments' => ['local', 'testing'], ], ]; ``` -------------------------------- ### Configure Laravel Dynamically with Playwright Source: https://github.com/hyvor/laravel-playwright/blob/main/README.md Modify Laravel's configuration dynamically during test execution using `laravel.config()`. This allows you to change settings like timezone for all subsequent requests within a test. Additionally, use `laravel.travel()` to simulate specific times and `laravel.registerBootFunction()` to hook into the Laravel boot process for tasks like service mocking. ```typescript import { test } from '@hyvor/laravel-playwright'; test('example', async ({ laravel }) => { // SET DYNAMIC CONFIG // ================== // Update a config value // This value will be used in all subsequent requests sent to Laravel // until the test ends and calls `tearDown` (which is done automatically) await laravel.config('app.timezone', 'Europe/Paris'); // TRAVEL TO A TIME // ================= // Travel to a specific time in the application // This is similar to Laravel's `travelTo` method await laravel.travel('2022-01-01 12:00:00'); // REGISTER A BOOT FUNCTION // ======================== // Register a function to run while Laravel is booting // This is useful to mock a service dependency, for example await laravel.registerBootFunction('App\E2EHelper::swapPaymentService'); }); ``` -------------------------------- ### POST /playwright/artisan Source: https://github.com/hyvor/laravel-playwright/blob/main/README.OLD.md Executes Artisan commands within the Laravel application. Useful for tasks like running migrations or seeding the database. ```APIDOC ## POST /playwright/artisan ### Description Allows running Artisan commands from Playwright tests. Supports passing command parameters. ### Method POST ### Endpoint /playwright/artisan ### Parameters #### Request Body - **command** (string) - Required - The Artisan command to execute. - **parameters** (array | object) - Optional - Parameters to pass to the command. ### Request Example ```json { "command": "migrate:fresh", "parameters": ["--seed"] } ``` ### Response #### Success Response (200) - **code** (integer) - The exit code of the command. - **output** (string) - The standard output of the command. #### Response Example ```json { "code": 0, "output": "" } ``` ``` -------------------------------- ### Execute Database Query (JSON Input) Source: https://github.com/hyvor/laravel-playwright/blob/main/README.OLD.md Defines the JSON structure for executing raw SQL queries against a specified database connection. ```json { "query": "UPDATE users SET name = 'John Doe' WHERE id = 1", "connection": "mysql" } ``` -------------------------------- ### POST /playwright/factory Source: https://github.com/hyvor/laravel-playwright/blob/main/README.OLD.md Creates Eloquent models using Laravel factories, optionally specifying counts and attributes. ```APIDOC ## POST /playwright/factory ### Description Creates Eloquent models using factories. Can create single or multiple models with specified attributes. ### Method POST ### Endpoint /playwright/factory ### Parameters #### Request Body - **model** (string) - Required - The model class name (e.g., `User` or `App\Models\User`). - **count** (integer) - Optional - The number of models to create. If set, returns an array of models. Defaults to 1. - **attributes** (object) - Optional - Attributes to set for the created model(s). ### Request Example #### Single Model ```json { "model": "User", "attributes": { "name": "John Doe" } } ``` #### Multiple Models ```json { "model": "App\\Database\\Models\\User", "count": 5 } ``` ### Response #### Success Response (200) - Returns a single model object if `count` is not provided or is 1. - Returns an array of model objects if `count` is greater than 1. ``` -------------------------------- ### Execute Database Queries with Laravel Playwright Source: https://github.com/hyvor/laravel-playwright/blob/main/README.md Run raw SQL queries directly against your database using `laravel.query()`. This method supports queries with bindings and allows you to specify different database connections. It also handles unprepared statements for more complex operations. ```typescript import { test } from '@hyvor/laravel-playwright'; test('example', async ({ laravel }) => { // RUN A DATABASE QUERY // ==================== // Run a query await laravel.query('DELETE FROM users'); // Run a query with bindings await laravel.query('DELETE FROM users WHERE id = ?', [1]); // Run a query on a specific connection await laravel.query('DELETE FROM users', [], { connection: 'connection1' }); // Run a unprepared statement await laravel.query( """ DROP SCHEMA public CASCADE; CREATE SCHEMA public; GRANT ALL ON SCHEMA public TO public; """, [], { unprepared: true }); }); ``` -------------------------------- ### Swap Playwright Test Import Source: https://github.com/hyvor/laravel-playwright/blob/main/README.md Demonstrates how to modify the import statement in Playwright test files to use the custom `test` object provided by `@hyvor/laravel-playwright` instead of the default from `@playwright/test`. This enables access to Playwright's Laravel-specific functionalities. ```diff - import { test } from '@playwright/test'; + import { test } from '@hyvor/laravel-playwright'; ``` -------------------------------- ### Run Artisan Command in Playwright Test Source: https://github.com/hyvor/laravel-playwright/blob/main/README.md Executes a Laravel Artisan command, such as 'migrate:fresh', from within a Playwright end-to-end test using the `laravel.artisan()` method provided by the `@hyvor/laravel-playwright` package. ```typescript test('example', async ({ laravel }) => { laravel.artisan('migrate:fresh'); }); ``` -------------------------------- ### Run Artisan Commands with Laravel Playwright Source: https://github.com/hyvor/laravel-playwright/blob/main/README.md Execute Artisan commands within your Laravel application using the `laravel.artisan()` method. This function allows you to run commands with or without parameters, capturing their output and exit codes. It's useful for tasks like database migrations and seeding. ```typescript import { test } from '@hyvor/laravel-playwright'; test('example', async ({ laravel }) => { // RUN ARTISAN COMMANDS // ==================== const output = await laravel.artisan('migrate:fresh'); // output.code: number - The exit code of the command // output.output: string - The output of the command // with parameters await laravel.artisan('db:seed', ['--class', 'DatabaseSeeder']); }); ``` -------------------------------- ### Truncate Database Tables (JSON Input) Source: https://github.com/hyvor/laravel-playwright/blob/main/README.OLD.md Provides the JSON structure for truncating database tables, optionally specifying connections. ```json { "connections": [] // optional } ``` -------------------------------- ### POST /playwright/truncate Source: https://github.com/hyvor/laravel-playwright/blob/main/README.OLD.md Truncates all tables in the specified database connections. A faster alternative to migrating for clearing data. ```APIDOC ## POST /playwright/truncate ### Description Truncates all tables in the database. Can specify multiple database connections. ### Method POST ### Endpoint /playwright/truncate ### Parameters #### Request Body - **connections** (array) - Optional - An array of database connection names to truncate. If omitted, the default connection is used. ### Request Example ```jsonc { "connections": [] // optional } ``` ### Response #### Success Response (200) Indicates successful truncation of tables. ``` -------------------------------- ### Configure Playwright with Laravel Options Type Source: https://github.com/hyvor/laravel-playwright/blob/main/README.md Configures Playwright, specifically for TypeScript projects, by including the `LaravelOptions` type within the `defineConfig` function to ensure proper type checking for Laravel-specific configurations. ```typescript import type { LaravelOptions } from '@hyvor/laravel-playwright'; export default defineConfig({ use: { laravelBaseUrl: 'http://localhost/playwright', }, }); ``` -------------------------------- ### Create Models from Factories with Laravel Playwright Source: https://github.com/hyvor/laravel-playwright/blob/main/README.md Generate Eloquent models from your factories using `laravel.factory()`. This method allows you to create single or multiple models, optionally with specific attributes, providing a convenient way to populate your database with test data. ```typescript import { test } from '@hyvor/laravel-playwright'; test('example', async ({ laravel }) => { // CREATE MODELS FROM FACTORIES // ============================ // Create a App\Models\User model // user will be an object of the model const user = await laravel.factory('User'); // Create a App\Models\User model with attributes await laravel.factory('User', { name: 'John Doe' }); // Create 5 App\Models\User models // users will be an array of the models const users = await laravel.factory('User', {}, 5); // Create a CustomModel model await laravel.factory('CustomModel'); }); ``` -------------------------------- ### Execute Select Queries with Laravel Playwright Source: https://github.com/hyvor/laravel-playwright/blob/main/README.md Retrieve data from your database using `laravel.select()`. This method executes SELECT statements, returning an array of objects. It supports parameter binding and targeting specific database connections, enabling flexible data retrieval for testing. ```typescript import { test } from '@hyvor/laravel-playwright'; test('example', async ({ laravel }) => { // RUN A SELECT QUERY // ================== // Run a select query // Returns an array of objects const blogs = await laravel.select('SELECT * FROM blogs'); // Run a select query with bindings await laravel.select('SELECT * FROM blogs WHERE id = ?', [1]); // Run a select query on a specific connection await laravel.select('SELECT * FROM blogs', [], { connection: 'connection1' }); }); ``` -------------------------------- ### POST /playwright/function Source: https://github.com/hyvor/laravel-playwright/blob/main/README.OLD.md Calls arbitrary PHP functions or static class methods. ```APIDOC ## POST /playwright/function ### Description Calls a specified PHP function or static class method with provided arguments. Returns the function's output. ### Method POST ### Endpoint /playwright/function ### Parameters #### Request Body - **function** (string) - Required - The name of the function or static method (e.g., `my_function` or `MyClass::myStaticMethod`). - **args** (array | object) - Required - Arguments to pass to the function. Can be an array for positional arguments or an object for named arguments. ### Request Example #### Positional Arguments ```json { "function": "fullName", "args": ["Supun", "Wimalasena"] } ``` #### Named Arguments ```json { "function": "fullName", "args": { "first": "Supun", "last": "Wimalasena" } } ``` #### Static Method Call ```json { "function": "namespace\\class::method", "args": [] } ``` ### Response #### Success Response (200) - Returns the JSON-encoded return value of the called PHP function or method. ``` -------------------------------- ### Call PHP Function or Static Method (JSON Input) Source: https://github.com/hyvor/laravel-playwright/blob/main/README.OLD.md Demonstrates how to invoke PHP functions or static class methods, including passing arguments as arrays or named parameters. ```json { "function": "fullName", "args": ["Supun", "Wimalasena"] } ``` ```json { "function": "fullName", "args": { "first": "Supun", "last": "Wimalasena" } } ``` ```json { "function": "namespace\\class::method", "args": [] } ``` -------------------------------- ### Configure Playwright Base URL Source: https://github.com/hyvor/laravel-playwright/blob/main/README.md Sets the `laravelBaseUrl` in the Playwright configuration file (`playwright.config.ts`) to the base URL of the testing endpoints. This URL combines the application's base URL with the prefix defined in the Laravel configuration. ```typescript export default defineConfig({ // ...other use: { laravelBaseUrl: 'http://localhost/playwright', }, }); ``` -------------------------------- ### Truncate Tables with Laravel Playwright Source: https://github.com/hyvor/laravel-playwright/blob/main/README.md Efficiently truncate database tables using the `laravel.truncate()` method. You can truncate all tables or specify individual connections to clear data selectively. This is crucial for resetting the database state between tests. ```typescript import { test } from '@hyvor/laravel-playwright'; test('example', async ({ laravel }) => { // TRUNCATE TABLES // =============== await laravel.truncate(); // in specific DB connections await laravel.truncate(['connection1', 'connection2']); }); ``` -------------------------------- ### Call PHP Functions with Laravel Playwright Source: https://github.com/hyvor/laravel-playwright/blob/main/README.md Invoke PHP functions and static class methods within your Laravel application using `laravel.callFunction()`. This method facilitates direct interaction with your backend logic, returning JSON-encoded results that are automatically decoded in Playwright. It supports passing arguments as arrays or objects for flexible function calls. ```typescript import { test } from '@hyvor/laravel-playwright'; test('example', async ({ laravel }) => { // RUN A PHP FUNCTION // ================== // Run a PHP function // Returns the output of the function // Output is JSON encoded in Laravel and decoded in Playwright // The following examples call this function: // function sayHello($name) { return "Hello, $name!"; } const funcOutput = await laravel.callFunction('sayHello'); // Run a PHP function with parameters await laravel.callFunction('sayHello', ['John']); // Run a PHP function with named parameters await laravel.callFunction('sayHello', { name: 'John' }); // Run a static class method await laravel.callFunction("App\\MyAwesomeClass::method"); }); ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.