### Start a Mink session with a driver Source: https://mink.behat.org/en/latest/guides/session.html Creates a GoutteDriver and a Session, then starts the session. The driver is the first argument to the session constructor. Starting the session is required before any other action. ```php // Choose a Mink driver. More about it in later chapters. $driver = new \Behat\Mink\Driver\GoutteDriver(); $session = new \Behat\Mink\Session($driver); // start the session $session->start(); ``` -------------------------------- ### Install Mink with Composer Source: https://mink.behat.org/en/latest/index.html Install Mink and its dependencies via Composer. Use `php composer.phar require --dev behat/mink` for local Composer installations. ```bash $ composer require --dev behat/mink ``` -------------------------------- ### Initialize and start multiple sessions Source: https://mink.behat.org/en/latest/guides/managing-sessions.html Creates two Mink sessions with different drivers and starts them, then visits the same URL in both sessions. Each session must use a different driver instance because the driver manages the session state. ```php // init sessions $session1 = new \Behat\Mink\Session($driver1); $session2 = new \Behat\Mink\Session($driver2); // start sessions $session1->start(); $session2->start(); $session1->visit('http://my_project.dev/chat.php'); $session2->visit('http://my_project.dev/chat.php'); ``` -------------------------------- ### Install BrowserKitDriver with Composer Source: https://mink.behat.org/en/latest/drivers/browserkit.html Install the BrowserKitDriver package via Composer. This is a pure PHP library. ```bash $ composer require behat/mink-browserkit-driver ``` -------------------------------- ### Install SeleniumDriver via Composer Source: https://mink.behat.org/en/latest/drivers/selenium.html Install the SeleniumDriver package via Composer. ```bash $ composer require behat/mink-selenium-driver ``` -------------------------------- ### Start Sahi Proxy Source: https://mink.behat.org/en/latest/drivers/sahi.html Navigate to the Sahi bin directory and execute the startup script to run the proxy server. ```bash cd $YOUR_PATH_TO_SAHI/bin ./sahi.sh ``` -------------------------------- ### Install ZombieDriver via Composer Source: https://mink.behat.org/en/latest/drivers/zombie.html Use this command to install the behat/mink-zombie-driver package via Composer. ```bash $ composer require behat/mink-zombie-driver ``` -------------------------------- ### Instantiate ZombieDriver with automatic server setup Source: https://mink.behat.org/en/latest/drivers/zombie.html Use this PHP code to create a ZombieDriver instance with automatic server management. The driver will handle the server setup for you. ```php $driver = new \Behat\Mink\Driver\ZombieDriver( new \Behat\Mink\Driver\NodeJS\Server\ZombieServer() ); ``` -------------------------------- ### Install zombie.js globally with npm Source: https://mink.behat.org/en/latest/drivers/zombie.html Install zombie.js globally using npm, as required for the ZombieDriver to function. ```bash $ npm install -g zombie ``` -------------------------------- ### Access sessions by name Source: https://mink.behat.org/en/latest/guides/managing-sessions.html Uses the Mink container to get sessions by their registered names and visit a URL. Sessions are lazy-started on the first getSession() call, so the browser is not started until needed. ```php $mink->getSession('goutte')->visit('http://my_project.dev/chat.php'); $mink->getSession('sahi')->visit('http://my_project.dev/chat.php'); ``` -------------------------------- ### Reset and restart sessions Source: https://mink.behat.org/en/latest/guides/managing-sessions.html Shows how to reset or restart all started sessions using resetSessions() and restartSessions(). These methods only affect sessions that have been started. ```php // reset started sessions $mink->resetSessions(); // restart started sessions $mink->restartSessions(); ``` -------------------------------- ### Get the page element with Session::getPage Source: https://mink.behat.org/en/latest/guides/traversing-pages.html Retrieves the DocumentElement instance representing the current page. This is the starting point for page manipulation. ```php $page = $session->getPage(); // You can now manipulate the page. ``` -------------------------------- ### Run Chrome with remote debugging Source: https://mink.behat.org/en/latest/drivers/chrome.html Start Chrome with the necessary flags to enable remote debugging for the driver. ```bash $ google-chrome-stable --remote-debugging-address=0.0.0.0 --remote-debugging-port=9222 ``` -------------------------------- ### Install SahiDriver via Composer Source: https://mink.behat.org/en/latest/drivers/sahi.html Use Composer to add the Sahi driver to your project dependencies. ```bash $ composer require behat/mink-sahi-driver ``` -------------------------------- ### Install Selenium2Driver via Composer Source: https://mink.behat.org/en/latest/drivers/selenium2.html Use Composer to add the Selenium2Driver package to your project. ```bash $ composer require behat/mink-selenium2-driver ``` -------------------------------- ### Install ChromeDriver via Composer Source: https://mink.behat.org/en/latest/drivers/chrome.html Use Composer to add the dmore/chrome-mink-driver package to your project. ```bash $ composer require dmore/chrome-mink-driver ``` -------------------------------- ### Visit a page with Mink session Source: https://mink.behat.org/en/latest/guides/session.html Uses the session's visit method to open a URL. The session must be started first. The visit method does not throw exceptions for error status codes, so check the status code or page content to verify success. ```php $session->visit('http://my_project.dev/some_page.php'); ``` -------------------------------- ### Install GoutteDriver via Composer Source: https://mink.behat.org/en/latest/drivers/goutte.html Install the behat/mink-goutte-driver package using Composer. The driver is compatible with Goutte 1.x (Guzzle 3) and Goutte 2.x (Guzzle 4+), with Composer likely selecting Goutte 2.x by default. ```bash $ composer require behat/mink-goutte-driver ``` -------------------------------- ### Run Chrome in headless mode Source: https://mink.behat.org/en/latest/drivers/chrome.html Start Chrome in headless mode (supported in version 59+) with remote debugging enabled. ```bash $ google-chrome-stable --disable-gpu --headless --remote-debugging-address=0.0.0.0 --remote-debugging-port=9222 ``` -------------------------------- ### Execute and Evaluate JavaScript with executeScript and evaluateScript Source: https://mink.behat.org/en/latest/guides/session.html Shows how to execute JavaScript without a return value using executeScript, and how to evaluate a JavaScript expression and get its result using evaluateScript. The evaluateScript method returns the result of the expression, while executeScript is better when no return value is needed. ```php // Execute JS $session->executeScript('document.body.firstChild.innerHTML = "";'); // evaluate JS expression: echo $session->evaluateScript( "return 'something from browser';" ); ``` -------------------------------- ### Managing Cookies with setCookie and getCookie Source: https://mink.behat.org/en/latest/guides/session.html Set, get, and delete cookies using the session. Note that with JavaScript-driven drivers like Sahi, access to HttpOnly cookies may be restricted. ```php // set cookie: $session->setCookie('cookie name', 'value'); // get cookie: echo $session->getCookie('cookie name'); // delete cookie: $session->setCookie('cookie name', null); ``` -------------------------------- ### Set NODE_PATH environment variable Source: https://mink.behat.org/en/latest/drivers/zombie.html Add this line to your .bashrc to include npm libs in NODE_PATH, which is necessary after installing npm and zombie.js. ```bash export NODE_PATH="/PATH/TO/NPM/node_modules" ``` -------------------------------- ### Reset Session with reset, stop, and restart Source: https://mink.behat.org/en/latest/guides/session.html Shows the three methods for session isolation: reset for a soft reset (clears cookies, request headers, and browser history), stop for a hard reset (closes the browser entirely), and restart as a shortcut to stop and start again. Recommended to use reset by default and stop when full isolation is needed. ```php // soft-reset: $session->reset(); // hard-reset: $session->stop(); // or if you want to start again at the same time $session->restart(); ``` -------------------------------- ### Retrieving HTTP Status Code Source: https://mink.behat.org/en/latest/guides/session.html Get the HTTP status code of the response using getStatusCode. ```php echo $session->getStatusCode(); ``` -------------------------------- ### Check and get HTML attributes with hasAttribute and getAttribute Source: https://mink.behat.org/en/latest/guides/manipulating-pages.html Finds an element and checks if it has an 'href' attribute, then prints its value or a fallback message. ```php $el = $page->find('css', '.something'); if ($el->hasAttribute('href')) { echo $el->getAttribute('href'); } else { echo 'This anchor is not a link. It does not have an href.'; } ``` -------------------------------- ### Get tag name with NodeElement::getTagName Source: https://mink.behat.org/en/latest/guides/manipulating-pages.html Finds an element by CSS selector and prints its tag name, which is always lowercased. ```php $el = $page->find('css', '.something'); // get tag name: echo $el->getTagName(); // displays 'a' ``` -------------------------------- ### Find elements with CSS selector Source: https://mink.behat.org/en/latest/guides/traversing-pages.html Use the 'css' selector type with ElementInterface::find to locate elements by CSS expressions. The first example finds an h1 element, the second finds an icon inside a button. ```php $title = $page->find('css', 'h1'); $buttonIcon = $page->find('css', '.btn > .icon'); ``` -------------------------------- ### Initialize SahiDriver with Custom Client Source: https://mink.behat.org/en/latest/drivers/sahi.html Use this verbose initialization to configure the driver to connect to a specific Sahi proxy host, port, and session ID. ```php $driver = new \Behat\Mink\Driver\SahiDriver( 'firefox', new \Behat\SahiClient\Client( new \Behat\SahiClient\Connection($sid, $host, $port) ) ); ``` -------------------------------- ### Create SeleniumDriver Instance Source: https://mink.behat.org/en/latest/drivers/selenium.html Instantiate the Selenium client and create a SeleniumDriver instance with the browser name, base URL, and client. ```php $client = new \Selenium\Client($host, $port); $driver = new \Behat\Mink\Driver\SeleniumDriver( 'firefox', 'base_url', $client ); ``` -------------------------------- ### Run Selenium Server Source: https://mink.behat.org/en/latest/drivers/selenium.html Run the Selenium Server standalone JAR file. Update the version number to the one you downloaded. ```bash $ java -jar selenium-server-standalone-2.44.0.jar ``` -------------------------------- ### Initialize Selenium2Driver Source: https://mink.behat.org/en/latest/drivers/selenium2.html Instantiate the Selenium2Driver class to begin using it in your tests. ```php $driver = new \Behat\Mink\Driver\Selenium2Driver('firefox'); ``` -------------------------------- ### Initialize SahiDriver Source: https://mink.behat.org/en/latest/drivers/sahi.html Create a new SahiDriver instance by specifying the browser name. ```php $driver = new \Behat\Mink\Driver\SahiDriver('firefox'); ``` -------------------------------- ### Instantiate ZombieDriver with manual server configuration Source: https://mink.behat.org/en/latest/drivers/zombie.html Use this PHP code to create a ZombieDriver with more control over the server initialization, such as specifying host, port, node binary, and script. Defaults: host 127.0.0.1, port 8124, nodeBin 'node', script null (default script). ```php $driver = new \Behat\Mink\Driver\ZombieDriver( new \Behat\Mink\Driver\Zombie\Server($host, $port, $nodeBin, $script) ); ``` -------------------------------- ### Instantiate BrowserKitDriver Source: https://mink.behat.org/en/latest/drivers/browserkit.html Instantiate the BrowserKitDriver with a BrowserKit client. The client is typically provided by Goutte or the Symfony HttpKernel component. ```php $browserkitClient = // ... $driver = new \Behat\Mink\Driver\BrowserKitDriver($browserkitClient); ``` -------------------------------- ### Configure Mink for ChromeDriver Source: https://mink.behat.org/en/latest/drivers/chrome.html Initialize the Mink session using the ChromeDriver instance pointing to the remote debugging port. ```php use Behat\Mink\Mink; use Behat\Mink\Session; use DMore\ChromeDriver\ChromeDriver; $mink = new Mink(array( 'browser' => new Session(new ChromeDriver('http://localhost:9222', null, 'http://www.google.com')) )); ``` -------------------------------- ### Register named sessions in Mink container Source: https://mink.behat.org/en/latest/guides/managing-sessions.html Creates a Mink container, registers two sessions under names 'goutte' and 'sahi', and sets 'goutte' as the default session name. This allows accessing sessions by name through the container. ```php $mink = new \Behat\Mink\Mink(); $mink->registerSession('goutte', $goutteSession); $mink->registerSession('sahi', $sahiSession); $mink->setDefaultSessionName('goutte'); ``` -------------------------------- ### Set HTTP Basic Authentication with $session->setBasicAuth($user, $password) Source: https://mink.behat.org/en/latest/guides/session.html Use this method to perform HTTP Basic authentication in a Mink session. Note that automatic HTTP authentication is only supported in headless drivers; browser controllers require manual user action. ```php $session->setBasicAuth($user, $password); ``` -------------------------------- ### Include Composer Autoloader Source: https://mink.behat.org/en/latest/index.html Include the Composer autoloader to use Mink in your project. ```php require_once 'vendor/autoload.php'; ``` -------------------------------- ### Managing Request and Response Headers Source: https://mink.behat.org/en/latest/guides/session.html Set request headers and retrieve response headers. Headers handling is only supported in headless drivers like Goutte; browser controllers like Selenium2 cannot access this information. ```php // setting browser language: $session->setRequestHeader('Accept-Language', 'fr'); // retrieving response headers: print_r($session->getResponseHeaders()); ``` -------------------------------- ### Using Browser History Controls Source: https://mink.behat.org/en/latest/guides/session.html Retrieve the current page URL and use history controls: reload, back, and forward. ```php // get the current page URL: echo $session->getCurrentUrl(); // use history controls: $session->reload(); $session->back(); $session->forward(); ``` -------------------------------- ### Configure GoutteDriver with custom client Source: https://mink.behat.org/en/latest/drivers/goutte.html Set up a GoutteDriver with a manually configured Goutte client. This allows more precise configuration of the Goutte client before passing it to the driver. ```php $client = new \Goutte\Client(); // Do more configuration for the Goutte client $driver = new \Behat\Mink\Driver\GoutteDriver($client); ``` -------------------------------- ### Instantiate GoutteDriver Source: https://mink.behat.org/en/latest/drivers/goutte.html Create a new GoutteDriver instance. The driver extends BrowserKitDriver and can automatically instantiate the Goutte client. ```php $driver = new \Behat\Mink\Driver\GoutteDriver(); ``` -------------------------------- ### Nested traversing with find and findField Source: https://mink.behat.org/en/latest/guides/traversing-pages.html Shows how to find a form element and then a field inside it. Throws an exception if the form is not found. ```php $registerForm = $page->find('css', 'form.register'); if (null === $registerForm) { throw new \Exception('The element is not found'); } // find some field INSIDE form with class="register" $field = $registerForm->findField('Email'); ``` -------------------------------- ### Drag and drop one element onto another with dragTo Source: https://mink.behat.org/en/latest/guides/interacting-with-pages.html Shows how to drag an element onto a target element using the dragTo method. Both elements are found via the page's find method. ```php $dragged = $page->find(...); $target = $page->find(...); $dragged->dragTo($target); ``` -------------------------------- ### Register custom selector with SelectorsHandler Source: https://mink.behat.org/en/latest/guides/traversing-pages.html Register a custom selector by implementing Behat\Mink\Selector\SelectorInterface and registering it in the SelectorsHandler. The recommended way is to do this when building your Session, as shown. ```php $selector = new \App\MySelector(); $handler = new \Behat\Mink\Selector\SelectorsHandler(); $handler->registerSelector('mine', $selector); $driver = // ... $session = new \Behat\Mink\Session($driver, $handler); ``` -------------------------------- ### Access default session without name Source: https://mink.behat.org/en/latest/guides/managing-sessions.html Calls getSession() without arguments to retrieve the default session, which was set via setDefaultSessionName('goutte'). This works because the default session name is configured. ```php $mink->getSession()->visit('http://my_project.dev/chat.php'); ``` -------------------------------- ### Reset HTTP Basic Authentication with $session->setBasicAuth(false) Source: https://mink.behat.org/en/latest/guides/session.html Call setBasicAuth with false to reset a previous authentication. This is useful for clearing credentials between tests. ```php $session->setBasicAuth(false); ``` -------------------------------- ### Find elements with XPath selector Source: https://mink.behat.org/en/latest/guides/traversing-pages.html Use the 'xpath' selector type with ElementInterface::findAll to run XPath queries. Note that this selector searches inside the current node (the element for the page object), so passing an XPath from ElementInterface::getXpath will not work; use ElementInterface::isValid to check element existence. ```php $anchorsWithoutUrl = $page->findAll('xpath', '//a[not(@href)]'); ``` -------------------------------- ### Find elements with named selector Source: https://mink.behat.org/en/latest/guides/traversing-pages.html Use the 'named' selector type with ElementInterface::find. The second argument is an array with two elements: the query name (e.g., 'link') and the value to search. Named selectors try exact match first, then fallback to partial matching; use 'named_exact' or 'named_partial' to force behavior. ```php $topLink = $page->find('named', array('link', $escapedValue)); ``` -------------------------------- ### Wait for JavaScript Condition with $session->wait($timeout, $condition) Source: https://mink.behat.org/en/latest/guides/session.html Waits until the given JavaScript expression returns a truthy value or the timeout is reached. The method returns true when the evaluation becomes truthy, and false when the timeout is reached. ```php // wait for n milliseconds or // till JS expression becomes truthy: $session->wait( 5000, "$('.suggestions-results').children().length" ); ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.