{"id":13545652,"url":"https://github.com/laravel/browser-kit-testing","last_synced_at":"2025-05-14T01:07:49.394Z","repository":{"id":37677712,"uuid":"75771493","full_name":"laravel/browser-kit-testing","owner":"laravel","description":"Provides backwards compatibility for BrowserKit testing in the latest Laravel release.","archived":false,"fork":false,"pushed_at":"2025-02-12T01:54:57.000Z","size":427,"stargazers_count":509,"open_issues_count":0,"forks_count":73,"subscribers_count":23,"default_branch":"7.x","last_synced_at":"2025-04-03T06:52:13.809Z","etag":null,"topics":["laravel","testing"],"latest_commit_sha":null,"homepage":"","language":"PHP","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/laravel.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":".github/CONTRIBUTING.md","funding":null,"license":"LICENSE.md","code_of_conduct":".github/CODE_OF_CONDUCT.md","threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":".github/SECURITY.md","support":".github/SUPPORT.md","governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2016-12-06T21:06:26.000Z","updated_at":"2025-02-18T20:24:57.000Z","dependencies_parsed_at":"2024-01-08T03:22:38.613Z","dependency_job_id":"44a4e275-32e4-49fb-9ce3-ffbdd4168d53","html_url":"https://github.com/laravel/browser-kit-testing","commit_stats":{"total_commits":261,"total_committers":40,"mean_commits":6.525,"dds":0.3716475095785441,"last_synced_commit":"0f1252716260085c7e5a9409f38117b7c3130add"},"previous_names":[],"tags_count":37,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/laravel%2Fbrowser-kit-testing","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/laravel%2Fbrowser-kit-testing/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/laravel%2Fbrowser-kit-testing/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/laravel%2Fbrowser-kit-testing/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/laravel","download_url":"https://codeload.github.com/laravel/browser-kit-testing/tar.gz/refs/heads/7.x","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248208689,"owners_count":21065205,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["laravel","testing"],"created_at":"2024-08-01T11:01:08.272Z","updated_at":"2025-04-10T11:37:16.926Z","avatar_url":"https://github.com/laravel.png","language":"PHP","funding_links":[],"categories":["PHP"],"sub_categories":[],"readme":"# Laravel BrowserKit Testing\n\n\u003ca href=\"https://github.com/laravel/browser-kit-testing/actions\"\u003e\u003cimg src=\"https://github.com/laravel/browser-kit-testing/workflows/tests/badge.svg\" alt=\"Build Status\"\u003e\u003c/a\u003e\n\u003ca href=\"https://packagist.org/packages/laravel/browser-kit-testing\"\u003e\u003cimg src=\"https://img.shields.io/packagist/dt/laravel/browser-kit-testing\" alt=\"Total Downloads\"\u003e\u003c/a\u003e\n\u003ca href=\"https://packagist.org/packages/laravel/browser-kit-testing\"\u003e\u003cimg src=\"https://img.shields.io/packagist/v/laravel/browser-kit-testing\" alt=\"Latest Stable Version\"\u003e\u003c/a\u003e\n\u003ca href=\"https://packagist.org/packages/laravel/browser-kit-testing\"\u003e\u003cimg src=\"https://img.shields.io/packagist/l/laravel/browser-kit-testing\" alt=\"License\"\u003e\u003c/a\u003e\n\n## Introduction\n\nLaravel BrowserKit Testing provides a very fluent API for making HTTP requests to your application, examining the output, and even filling out forms.\n\n## Official Documentation\n\n### Installation\n\nFirst, install this package:\n\n    composer require laravel/browser-kit-testing --dev\n\nNext, modify your application's base `TestCase` class to extend `Laravel\\BrowserKitTesting\\TestCase` instead of `Illuminate\\Foundation\\Testing\\TestCase`:\n\n```php\n\u003c?php\n\nnamespace Tests;\n\nuse Laravel\\BrowserKitTesting\\TestCase as BaseTestCase;\n\nabstract class TestCase extends BaseTestCase\n{\n    use CreatesApplication;\n\n    public $baseUrl = 'http://localhost';\n\n    // ...\n}\n```\n\nNo other modifications to your tests should be necessary.\n\n### Usage\n\nTo get started with a simple example, take a look at the test defined below:\n\n```php\n\u003c?php\n\nuse Illuminate\\Foundation\\Testing\\WithoutMiddleware;\nuse Illuminate\\Foundation\\Testing\\DatabaseTransactions;\n\nclass ExampleTest extends TestCase\n{\n    /**\n     * A basic functional test example.\n     *\n     * @return void\n     */\n    public function testBasicExample()\n    {\n        $this-\u003evisit('/')\n             -\u003esee('Laravel')\n             -\u003edontSee('Rails');\n    }\n}\n```\n\nThe `visit` method makes a `GET` request into the application. The `see` method asserts that we should see the given text in the response returned by the application. The `dontSee` method asserts that the given text is not returned in the application response. This is the most basic application test available in Laravel.\n\nYou may also use the 'visitRoute' method to make a 'GET' request via a named route:\n\n```php\n$this-\u003evisitRoute('profile');\n\n$this-\u003evisitRoute('profile', ['user' =\u003e 1]);\n```\n\n### Interacting With Your Application\n\nOf course, you can do much more than simply assert that text appears in a given response. Let's take a look at some examples of clicking links and filling out forms:\n\n### Interacting With Links\n\nIn this test, we will make a request to the application, \"click\" a link in the returned response, and then assert that we landed on a given URI. For example, let's assume there is a link in our response that has a text value of \"About Us\":\n\n```html\n\u003ca href=\"/about-us\"\u003eAbout Us\u003c/a\u003e\n```\n\nNow, let's write a test that clicks the link and asserts the user lands on the correct page:\n\n```php\npublic function testBasicExample()\n{\n    $this-\u003evisit('/')\n         -\u003eclick('About Us')\n         -\u003eseePageIs('/about-us');\n}\n```\n\nYou may also check that the user has arrived at the correct named route using the `seeRouteIs` method:\n\n```php\n-\u003eseeRouteIs('profile', ['user' =\u003e 1]);\n```\n\n#### Interacting With Forms\n\nLaravel also provides several methods for testing forms. The `type`, `select`, `check`, `attach`, and `press` methods allow you to interact with all of your form's inputs. For example, let's imagine this form exists on the application's registration page:\n\n```html\n\u003cform action=\"/register\" method=\"POST\"\u003e\n    {{ csrf_field() }}\n\n    \u003cdiv\u003e\n        Name: \u003cinput type=\"text\" name=\"name\"\u003e\n    \u003c/div\u003e\n\n    \u003cdiv\u003e\n        \u003cinput type=\"checkbox\" value=\"yes\" name=\"terms\"\u003e Accept Terms\n    \u003c/div\u003e\n\n    \u003cdiv\u003e\n        \u003cinput type=\"submit\" value=\"Register\"\u003e\n    \u003c/div\u003e\n\u003c/form\u003e\n```\n\nWe can write a test to complete this form and inspect the result:\n\n```php\npublic function testNewUserRegistration()\n{\n    $this-\u003evisit('/register')\n         -\u003etype('Taylor', 'name')\n         -\u003echeck('terms')\n         -\u003epress('Register')\n         -\u003eseePageIs('/dashboard');\n}\n```\n\nOf course, if your form contains other inputs such as radio buttons or drop-down boxes, you may easily fill out those types of fields as well. Here is a list of each form manipulation method:\n\nMethod  | Description\n------------- | -------------\n`$this-\u003etype($text, $elementName)`  |  \"Type\" text into a given field.\n`$this-\u003eselect($value, $elementName)`  |  \"Select\" a radio button or drop-down field.\n`$this-\u003echeck($elementName)`  |  \"Check\" a checkbox field.\n`$this-\u003euncheck($elementName)`  |  \"Uncheck\" a checkbox field.\n`$this-\u003eattach($pathToFile, $elementName)`  |  \"Attach\" a file to the form.\n`$this-\u003epress($buttonTextOrElementName)`  |  \"Press\" a button with the given text or name.\n\n##### File Inputs\n\nIf your form contains `file` inputs, you may attach files to the form using the `attach` method:\n\n```php\npublic function testPhotoCanBeUploaded()\n{\n    $this-\u003evisit('/upload')\n         -\u003eattach($pathToFile, 'photo')\n         -\u003epress('Upload')\n         -\u003esee('Upload Successful!');\n}\n```\n\n### Testing JSON APIs\n\nLaravel also provides several helpers for testing JSON APIs and their responses. For example, the `json`, `get`, `post`, `put`, `patch`, and `delete` methods may be used to issue requests with various HTTP verbs. You may also easily pass data and headers to these methods. To get started, let's write a test to make a `POST` request to `/user` and assert that the expected data was returned:\n\n```php\n\u003c?php\n\nclass ExampleTest extends TestCase\n{\n    /**\n     * A basic functional test example.\n     *\n     * @return void\n     */\n    public function testBasicExample()\n    {\n        $this-\u003ejson('POST', '/user', ['name' =\u003e 'Sally'])\n             -\u003eseeJson([\n                 'created' =\u003e true,\n             ]);\n    }\n}\n```\n\n\u003e {tip} The `seeJson` method converts the given array into JSON, and then verifies that the JSON fragment occurs **anywhere** within the entire JSON response returned by the application. So, if there are other properties in the JSON response, this test will still pass as long as the given fragment is present.\n\n#### Verifying Exact Match\n\nIf you would like to verify that the given array is an **exact** match for the JSON returned by the application, you should use the `seeJsonEquals` method:\n\n```php\n\u003c?php\n\nclass ExampleTest extends TestCase\n{\n    /**\n     * A basic functional test example.\n     *\n     * @return void\n     */\n    public function testBasicExample()\n    {\n        $this-\u003ejson('POST', '/user', ['name' =\u003e 'Sally'])\n             -\u003eseeJsonEquals([\n                 'created' =\u003e true,\n             ]);\n    }\n}\n```\n\n#### Verifying Structural Match\n\nIt is also possible to verify that a JSON response adheres to a specific structure. In this scenario, you should use the `seeJsonStructure` method and pass it your expected JSON structure:\n\n```php\n\u003c?php\n\nclass ExampleTest extends TestCase\n{\n    /**\n     * A basic functional test example.\n     *\n     * @return void\n     */\n    public function testBasicExample()\n    {\n        $this-\u003eget('/user/1')\n             -\u003eseeJsonStructure([\n                 'name',\n                 'pet' =\u003e [\n                     'name',\n                     'age',\n                 ],\n             ]);\n    }\n}\n```\n\nThe above example illustrates an expectation of receiving a `name` attribute and a nested `pet` object with its own `name` and `age` attributes. `seeJsonStructure` will not fail if additional keys are present in the response. For example, the test would still pass if the `pet` had a `weight` attribute.\n\nYou may use the `*` to assert that the returned JSON structure has a list where each list item contains at least the attributes found in the set of values:\n\n```php\n\u003c?php\n\nclass ExampleTest extends TestCase\n{\n    /**\n     * A basic functional test example.\n     *\n     * @return void\n     */\n    public function testBasicExample()\n    {\n        // Assert that each user in the list has at least an id, name and email attribute.\n        $this-\u003eget('/users')\n             -\u003eseeJsonStructure([\n                 '*' =\u003e [\n                     'id',\n                     'name',\n                     'email',\n                 ],\n             ]);\n    }\n}\n```\n\nYou may also nest the `*` notation. In this case, we will assert that each user in the JSON response contains a given set of attributes and that each pet on each user also contains a given set of attributes:\n\n```php\n$this-\u003eget('/users')\n     -\u003eseeJsonStructure([\n         '*' =\u003e [\n             'id', 'name', 'email', 'pets' =\u003e [\n                 '*' =\u003e [\n                     'name',\n                     'age',\n                 ],\n             ],\n         ],\n     ]);\n```\n\n### Sessions / Authentication\n\nLaravel provides several helpers for working with the session during testing. First, you may set the session data to a given array using the `withSession` method. This is useful for loading the session with data before issuing a request to your application:\n\n```php\n\u003c?php\n\nclass ExampleTest extends TestCase\n{\n    public function testApplication()\n    {\n        $this-\u003ewithSession(['foo' =\u003e 'bar'])\n             -\u003evisit('/');\n    }\n}\n```\n\nOf course, one common use of the session is for maintaining state for the authenticated user. The `actingAs` helper method provides a simple way to authenticate a given user as the current user. For example, we may use a [model factory](/docs/{{version}}/database-testing#writing-factories) to generate and authenticate a user:\n\n```php\n\u003c?php\n\nclass ExampleTest extends TestCase\n{\n    public function testApplication()\n    {\n        $user = factory(App\\User::class)-\u003ecreate();\n\n        $this-\u003eactingAs($user)\n             -\u003ewithSession(['foo' =\u003e 'bar'])\n             -\u003evisit('/')\n             -\u003esee('Hello, '.$user-\u003ename);\n    }\n}\n```\n\nYou may also specify which guard should be used to authenticate the given user by passing the guard name as the second argument to the `actingAs` method:\n\n```php\n$this-\u003eactingAs($user, 'api')\n```\n\n## Disabling Middleware\n\nWhen testing your application, you may find it convenient to disable [middleware](/docs/{{version}}/middleware) for some of your tests. This will allow you to test your routes and controller in isolation from any middleware concerns. Laravel includes a simple `WithoutMiddleware` trait that you can use to automatically disable all middleware for the test class:\n\n```php\n\u003c?php\n\nuse Illuminate\\Foundation\\Testing\\WithoutMiddleware;\nuse Illuminate\\Foundation\\Testing\\DatabaseMigrations;\nuse Illuminate\\Foundation\\Testing\\DatabaseTransactions;\n\nclass ExampleTest extends TestCase\n{\n    use WithoutMiddleware;\n\n    //\n}\n```\n\nIf you would like to only disable middleware for a few test methods, you may call the `withoutMiddleware` method from within the test methods:\n\n```php\n\u003c?php\n\nclass ExampleTest extends TestCase\n{\n    /**\n     * A basic functional test example.\n     *\n     * @return void\n     */\n    public function testBasicExample()\n    {\n        $this-\u003ewithoutMiddleware();\n\n        $this-\u003evisit('/')\n             -\u003esee('Laravel');\n    }\n}\n```\n\n### Custom HTTP Requests\n\nIf you would like to make a custom HTTP request into your application and get the full `Illuminate\\Http\\Response` object, you may use the `call` method:\n\n```php\npublic function testApplication()\n{\n    $response = $this-\u003ecall('GET', '/');\n\n    $this-\u003eassertEquals(200, $response-\u003estatus());\n}\n```\n\nIf you are making `POST`, `PUT`, or `PATCH` requests you may pass an array of input data with the request. Of course, this data will be available in your routes and controller via the [Request instance](/docs/{{version}}/requests):\n\n```php\n$response = $this-\u003ecall('POST', '/user', ['name' =\u003e 'Taylor']);\n```\n\n### PHPUnit Assertions\n\nLaravel provides a variety of custom assertion methods for [PHPUnit](https://phpunit.de/) tests:\n\nMethod  | Description\n------------- | -------------\n`-\u003eassertResponseOk();`  |  Assert that the client response has an OK status code.\n`-\u003eassertResponseStatus($code);`  |  Assert that the client response has a given code.\n`-\u003eassertViewHas($key, $value = null);`  |  Assert that the response view has a given piece of bound data.\n`-\u003eassertViewHasAll(array $bindings);`  |  Assert that the view has a given list of bound data.\n`-\u003eassertViewMissing($key);`  |  Assert that the response view is missing a piece of bound data.\n`-\u003eassertRedirectedTo($uri, $with = []);`  |  Assert whether the client was redirected to a given URI.\n`-\u003eassertRedirectedToRoute($name, $parameters = [], $with = []);`  |  Assert whether the client was redirected to a given route.\n`-\u003eassertRedirectedToAction($name, $parameters = [], $with = []);`  |  Assert whether the client was redirected to a given action.\n`-\u003eassertSessionHas($key, $value = null);`  |  Assert that the session has a given value.\n`-\u003eassertSessionHasAll(array $bindings);`  |  Assert that the session has a given list of values.\n`-\u003eassertSessionHasErrors($bindings = [], $format = null);`  |  Assert that the session has errors bound.\n`-\u003eassertHasOldInput();`  |  Assert that the session has old input.\n`-\u003eassertSessionMissing($key);`  |  Assert that the session is missing a given key.\n\n## Contributing\n\nThank you for considering contributing to BrowserKit Testing! The contribution guide can be found in the [Laravel documentation](https://laravel.com/docs/contributions).\n\n## Code of Conduct\n\nIn order to ensure that the Laravel community is welcoming to all, please review and abide by the [Code of Conduct](https://laravel.com/docs/contributions#code-of-conduct).\n\n## Security Vulnerabilities\n\nPlease review [our security policy](https://github.com/laravel/browser-kit-testing/security/policy) on how to report security vulnerabilities.\n\n## License\n\nLaravel BrowserKit Testing is open-sourced software licensed under the [MIT license](LICENSE.md).\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Flaravel%2Fbrowser-kit-testing","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Flaravel%2Fbrowser-kit-testing","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Flaravel%2Fbrowser-kit-testing/lists"}