|
| 1 | +# Core Concepts |
| 2 | + |
| 3 | +Playwright's power and reliability come from its well-defined architecture. Understanding these core concepts is key to |
| 4 | +using the library effectively and writing robust browser automation scripts. The API is structured around four main |
| 5 | +objects: `Browser`, `BrowserContext`, `Page`, and `Locator`. |
| 6 | + |
| 7 | +## Browser |
| 8 | + |
| 9 | +A `Browser` instance represents a single browser process (e.g., Chromium, Firefox, or WebKit). You typically launch a |
| 10 | +browser once at the beginning of your script or test suite and close it at the end. |
| 11 | + |
| 12 | +While you can interact with the `Browser` object directly, most of your work will be done within a `BrowserContext`. |
| 13 | + |
| 14 | +```php |
| 15 | +<?php |
| 16 | +use PlaywrightPHP\Playwright; |
| 17 | + |
| 18 | +// The Playwright static class is the easiest way to get started. |
| 19 | +// This launches a Chromium browser and returns a default BrowserContext. |
| 20 | +$context = Playwright::chromium(['headless' => false]); |
| 21 | + |
| 22 | +// ... do stuff ... |
| 23 | + |
| 24 | +// Closing the context will also close the associated browser. |
| 25 | +$context->close(); |
| 26 | +``` |
| 27 | + |
| 28 | +## BrowserContext |
| 29 | + |
| 30 | +A `BrowserContext` is an isolated, "incognito-like" session within a browser instance. Each context has its own cookies, |
| 31 | +local storage, and cache, and they do not share these with other contexts. This makes them perfect for running |
| 32 | +independent tests in parallel. |
| 33 | + |
| 34 | +You can create multiple contexts from a single `Browser` instance. |
| 35 | + |
| 36 | +```php |
| 37 | +// The Browser object is available on the context. |
| 38 | +$browser = $context->browser(); |
| 39 | + |
| 40 | +// Create a second, isolated context. |
| 41 | +$adminContext = $browser->newContext(); |
| 42 | + |
| 43 | +// Create a new page in each context. |
| 44 | +$userPage = $context->newPage(); |
| 45 | +$adminPage = $adminContext->newPage(); |
| 46 | +``` |
| 47 | + |
| 48 | +Contexts are also where you configure session-specific behavior, such as: |
| 49 | + |
| 50 | +* Setting a custom viewport size. |
| 51 | +* Emulating mobile devices. |
| 52 | +* Granting permissions. |
| 53 | +* Loading a saved authentication state. |
| 54 | + |
| 55 | +## Page |
| 56 | + |
| 57 | +A `Page` represents a single tab within a `BrowserContext`. It is the primary object you will use to interact with a web |
| 58 | +page's content. Most of the essential actions, like navigating, clicking, and typing, are methods on the `Page` object. |
| 59 | + |
| 60 | +```php |
| 61 | +// Navigate the page to a URL. |
| 62 | +$page->goto('https://github.com'); |
| 63 | + |
| 64 | +// Interact with elements on the page. |
| 65 | +$page->click('a.HeaderMenu-link--sign-in'); |
| 66 | +$page->type('#login_field', 'my-username'); |
| 67 | +``` |
| 68 | + |
| 69 | +A `BrowserContext` can have multiple `Page` objects (tabs). |
| 70 | + |
| 71 | +## Locator |
| 72 | + |
| 73 | +The `Locator` is the heart of Playwright's modern approach to browser automation. It is the recommended way to find and |
| 74 | +interact with elements on a `Page`. |
| 75 | + |
| 76 | +A `Locator` is a "recipe" for finding an element on the page. Unlike traditional methods that find an element |
| 77 | +immediately, a `Locator` has **auto-waiting** built-in. When you perform an action on a locator, Playwright |
| 78 | +automatically waits for the element to exist and be in an "actionable" state (e.g., visible, enabled, not obscured) |
| 79 | +before performing the action. This eliminates a major source of flakiness in browser tests. |
| 80 | + |
| 81 | +```php |
| 82 | +// Create a locator for the search input field. |
| 83 | +$searchInput = $page->locator('#search-input'); |
| 84 | + |
| 85 | +// Playwright will automatically wait for the element to be ready |
| 86 | +// before filling it with text. |
| 87 | +$searchInput->fill('Playwright for PHP'); |
| 88 | + |
| 89 | +// You can chain locators to find elements within other elements. |
| 90 | +$header = $page->locator('header'); |
| 91 | +$signInButton = $header->locator('a:has-text("Sign In")'); |
| 92 | +$signInButton->click(); |
| 93 | +``` |
| 94 | + |
| 95 | +By understanding and using these four core concepts, you can build powerful, reliable, and maintainable browser |
| 96 | +automation scripts and tests. |
0 commit comments