Buy

Forget about Behat for a few minutes - I want to talk about a totally different library. Imagine if there were a simple PHP library that let you take command of a browser: surf to pages, find links with CSS, click those links, fill out forms and anything else you can dream up. It exists! And it's called Mink.

This is such a cool library that it deserves some direct attention. Start by creating a mink.php file right at the root of your project. And require composer's autoload file:

5 lines mink.php
... lines 1 - 2
require __DIR__.'/vendor/autoload.php';
... lines 4 - 5

We'll use Mink all by itself, outside of Symfony, Behat and everything else so we can focus on just how it works.

Important Object 1: The Driver

As awesome as Mink is, it only has four important objects. The first is the driver. Create a new $driver variable and set it to new GoutteDriver():

9 lines mink.php
... lines 1 - 4
use Behat\Mink\Driver\GoutteDriver;
... line 7
$driver = new GoutteDriver();

Now, ignore it: I'll come back and explain important object number 1 in a little while.

Important Object 2: The Session

Let's move on to important object #2, and the first that we really care about: the session. Add $session = new Session() and pass it the $driver as an argument:

19 lines mink.php
... lines 1 - 5
use Behat\Mink\Session;
... lines 8 - 11
$session = new Session($driver);
... lines 13 - 19

Think of the session like a browser tab: anything you can do in a tab, you can do in a session. And actually, that isn't very much. You can visit URLs, refresh, go backwards, go forwards and that's about it. Let's use it to visit a very awesome and absurdly-designed site "jurassicpark.wikia.com". After that we'll just print out a few things about the page like the status code, and the current URL:

19 lines mink.php
... lines 1 - 14
$session->visit('http://jurassicpark.wikia.com');
echo "Status code: ". $session->getStatusCode() . "\n";
echo "Current URL: ". $session->getCurrentUrl() . "\n";

To execute this, head over to the terminal and run:

php mink.php

Look at that: it's printing out 200 and a slightly different URL than we put in our code. That makes sense: when you go to the site in a browser, it redirects to the URL we see in our terminal. Mink is emulating a real browser by following redirects. But in reality, so far, it's making invisible cURL requests: it's not using a real browser.

Important Object 3: The Page (DocumentElement)

The third important object is called the page. Grab it by saying $page = $session->getPage():

24 lines mink.php
... lines 1 - 20
$page = $session->getPage();
... lines 22 - 24

I want you to think of this as the JQuery object or the DOM. Anything you can do with JQuery - like select elements, click links and fill out fields - you can do with the page. Less impressively, it also knows the HTML of whatever page we're currently on.

Tip

If you like to dig into the source code, the page is an instance of DocumentElement.

Let's use it to print out this first bit of text on the page with var_dump(substr($page->getText()), 0, 75);:

24 lines mink.php
... lines 1 - 22
echo "First 75 chars: ".substr($page->getText() , 0, 75) . "\n";

Run that again in the terminal.

php mink.php

Now we see the thrilling text of: "Park Pedia - Jurassic Park, Dinosaurs, Stephen Spielberg...". There's some weird a:lang code stuff on the end. Open up the source on the page.

The getText() method returns anything other than the HTML tags themselves. The first part comes from the title tag and then it grabs some other stuff from the style tag, which is technically text.

But what we really want to do is find individual elements so we can click links and fill out fields. Let's talk about that next.

Leave a comment!

  • 2016-10-28 Victor Bocharsky

    Ah, sorry, my fault. This looks work for Behat autocompletion in Sublime, but I have not used it at all yet.: https://packagecontrol.io/pack...

    What about other IDEs - difficult to say, I don't use other IDEs besides PhpStorm a long time. But you can check out PhpStorm early access program - https://confluence.jetbrains.c... , it's a beta version, but free, just don't forget to upgrade it.

    Cheers!

  • 2016-10-28 Sumeet

    Thanks for replying Victor.

    Syntax color highlighting looks good. But i meant autocompleting class methods and being able to use external classes like we could do in eclipse for Java with cmd shift O shortcut. Can it be done with sublime 3? Else, could you recommend an IDE not too costly. Is eclipse / Netbeans a better choice than sublime for PHP ?

    Regards

  • 2016-10-28 Victor Bocharsky

    Hey Sumeet,

    We don't use Sublime - it's just an advanced text editor but IDE. In our screencasts we use PhpStorm IDE. I found a Sublime text 2 plugin for Behat syntax highlighting on GitHub - looks like something you interested in: https://github.com/omissis/sub...

    Cheers!

  • 2016-10-28 Sumeet

    I don't see Autocomplete in sublime. Don't get correct suggestions while typing How can i enable that?