Skip to content

Commit 296d697

Browse files
committed
Rewrite the session chapter
1 parent afed839 commit 296d697

1 file changed

Lines changed: 140 additions & 67 deletions

File tree

guides/session.rst

Lines changed: 140 additions & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -1,76 +1,114 @@
11
Controlling the Browser
22
=======================
33

4-
Ok. Now we know how to create the browser driver to talk with a specific
5-
browser emulator. Although we can use drivers directly to call some actions
6-
on the emulator, Mink provides a better way - ``Session``:
4+
In Mink, the entry point to the browser is called the session. Think about
5+
it as being your browser window (some drivers even allow to deal with switching
6+
tabs).
7+
8+
The first thing to do with your session is to start it. Nothing can be done
9+
with it before starting it.
710

811
.. code-block:: php
912
10-
// init session:
13+
// choose a Mink driver. More about it in later chapters
14+
$driver = new \Behat\Mink\Driver\GoutteDriver();
15+
1116
$session = new \Behat\Mink\Session($driver);
1217
13-
// start session:
18+
// start the session
1419
$session->start();
1520
1621
.. note::
1722

18-
As you can see, the first argument to the session (``$driver``) is just
19-
a simple driver instance, which we created in the previous chapter.
23+
The first argument to the session constructor is a driver object, which
24+
is how the Mink abstraction layer works. You will discover more about
25+
the available drivers in a :doc:`later chapter </guides/drivers>`.
26+
2027

21-
``start()`` call is required in order to configure the browser emulator or
22-
controller to be fully functional.
28+
.. note::
29+
30+
Although Mink does its best on removing browser differences between different
31+
browser emulators, it can't do much in some cases. See the :ref:`driver-feature-support`
32+
to see which features are supported by each driver.
2333

2434
Basic Browser Interaction
25-
~~~~~~~~~~~~~~~~~~~~~~~~~
35+
-------------------------
2636

27-
After you've instantiated the ``$session`` object, you can control the actual
28-
browser emulator with it:
37+
The first thing to do to use your session is to open a page with it. Just
38+
after starting, the session is not on any page (in a real browser, you would
39+
on ``about:blank``), and calling any other action is likely to fail.
2940

3041
.. code-block:: php
3142
32-
// open some page in browser:
3343
$session->visit('http://my_project.dev/some_page.php');
3444
35-
// get the current page URL:
36-
echo $session->getCurrentUrl();
45+
.. note::
3746

38-
// get the response status code:
39-
echo $session->getStatusCode();
47+
Mink is primarily designed to be used for testing websites. To allow
48+
covering error pages as well, ``Session::visit`` does not consider that
49+
error status codes are invalid. It will not throw an exception in this
50+
case. You will need to check whether the response was a success or an
51+
error. It will only throw an exception when Mink cannot load the page
52+
(network error, ...).
53+
54+
Interacting with the Page
55+
-------------------------
56+
57+
The session gives you access to the page through the ``Session::getPage``
58+
method. This allows you to :doc:`traverse it </guides/traversing-pages>` and
59+
:doc:`manipulate it </guides/manipulating-pages>`. The next chapters are
60+
covering the page API in depth.
4061

41-
// get page content:
42-
echo $session->getPage()->getContent();
62+
Using the Browser History
63+
-------------------------
4364

44-
// open another page:
45-
$session->visit('http://my_project.dev/second_page.php');
65+
The session gives you access to the browser history:
66+
67+
.. code-block:: php
68+
69+
// get the current page URL:
70+
echo $session->getCurrentUrl();
4671
4772
// use history controls:
4873
$session->reload();
4974
$session->back();
5075
$session->forward();
5176
52-
// evaluate JS expression:
53-
echo $session->evaluateScript(
54-
"return 'something from browser';"
55-
);
77+
Cookies Management
78+
------------------
5679

57-
// wait for n milliseconds or
58-
// till JS expression becomes true:
59-
$session->wait(
60-
5000,
61-
"$('.suggestions-results').children().length > 0"
62-
);
80+
The session can manipulate cookies available in the browser.
81+
82+
.. code-block:: php
83+
84+
// set cookie:
85+
$session->setCookie('cookie name', 'value');
86+
87+
// get cookie:
88+
echo $session->getCookie('cookie name');
89+
90+
// delete cookie:
91+
$session->setCookie('cookie name', null);
6392
6493
.. note::
6594

66-
Although Mink does its best on removing browser differences between different
67-
browser emulators, it can't do much in some cases. See the :ref:`driver-feature-support`
68-
to see which features are supported by each driver.
95+
In browser controllers, the access to http-only cookies may be restricted
96+
as they cannot be accessed in Javascript.
97+
98+
Status Code Retrieval
99+
---------------------
100+
101+
The session lets you retrieve the status code of the response:
69102

70-
Cookies and Headers management
71-
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
103+
.. code-block:: php
104+
105+
// get the response status code:
106+
echo $session->getStatusCode();
107+
108+
Headers Management
109+
------------------
72110

73-
With ``Behat\Mink\Session`` you can control your browsers cookies and headers:
111+
The session lets you manipulate request headers and access response headers:
74112

75113
.. code-block:: php
76114
@@ -80,65 +118,100 @@ With ``Behat\Mink\Session`` you can control your browsers cookies and headers:
80118
// retrieving response headers:
81119
print_r($session->getResponseHeaders());
82120
83-
// set cookie:
84-
$session->setCookie('cookie name', 'value');
85-
86-
// get cookie:
87-
echo $session->getCookie('cookie name');
88-
89-
// delete cookie:
90-
$session->setCookie('cookie name', null);
91-
92121
.. note::
93122

94123
Headers handling is only supported in headless drivers, because there
95124
is no way browser controllers can get such information out of the browser.
96125

97126
HTTP Authentication
98-
~~~~~~~~~~~~~~~~~~~
127+
-------------------
99128

100-
Also, Mink session has a special method to perform HTTP Basic authentication:
129+
The Mink session has a special method to perform HTTP Basic authentication:
101130

102131
.. code-block:: php
103132
104133
$session->setBasicAuth($user, $password);
105134
135+
The method can also be used to reset a previous authentication:
136+
137+
.. code-block:: php
138+
139+
$session->setBasicAuth(false);
140+
106141
.. note::
107142

108143
Automatic HTTP authentication is only supported in headless drivers.
109144
Because HTTP authentication in browser requires manual user action, that
110145
can't be done remotely for browser controllers.
111146

147+
Javascript Evaluation
148+
---------------------
149+
150+
The session allows you to execute or evaluate Javascript.
151+
152+
.. code-block:: php
153+
154+
// Execute JS
155+
$session->evaluateScript('document.body.firstChild.innerHtml = "";');
156+
157+
// evaluate JS expression:
158+
echo $session->evaluateScript(
159+
"return 'something from browser';"
160+
);
161+
162+
.. note::
163+
164+
The difference between these methods is that ``Session::evaluateScript``
165+
returns the result of the expression. When you don't need to get a return
166+
value, using ``Session::executeScript`` is better.
167+
168+
You can also wait until a give JS expression returns a truthy value or the
169+
timeout is reached:
170+
171+
.. code-block:: php
172+
173+
// wait for n milliseconds or
174+
// till JS expression becomes true:
175+
$session->wait(
176+
5000,
177+
"$('.suggestions-results').children().length"
178+
);
179+
180+
.. note::
181+
182+
The ``Session::wait`` method returns the result of the evaluation. It
183+
will return ``null`` when the timeout is reached.
184+
112185
Resetting the Session
113-
~~~~~~~~~~~~~~~~~~~~~
186+
---------------------
114187

115188
The primary aim for Mink is to provide a single consistent web browsing API
116-
for acceptance tests. But most important part in testing is isolation. We
117-
need a way to isolate our tests from each other. And Mink provides two very
118-
useful methods for you to use in your ``teardown()`` methods:
189+
for acceptance tests. But a very important part in testing is isolation.
190+
191+
Mink provides two very useful methods to isolate tests, to be used in your
192+
``teardown`` methods:
119193

120194
.. code-block:: php
121195
122196
// soft-reset:
123197
$session->reset();
124198
125199
// hard-reset:
200+
$session->stop();
201+
// or if you want to start again at the same time
126202
$session->restart();
127203
128-
Both methods do exactly the same job for headless browsers, they clear browser's
129-
cookies and history. The difference appears with browser controllers:
130-
131-
* ``$session->reset()`` will try to clean all available from browser side
132-
cookies. It's very fast and doesn't require the physical reload of the
133-
browser between tests, making them much faster. But it has a disadvantage:
134-
it clears only the cookies available browser-side. And we also have ``http-only``
135-
cookies. In such case, resetting simply won't work. Also, browsing history
136-
will state the same after this call. So, it's very fast, but limited in
137-
complex cases.
204+
Stopping the session is the best way to reset the session to its initial
205+
state. It will close the browser entirely. Using the session again requires
206+
starting the session before any other action. The ``Session::restart`` shortcut
207+
allows to do these 2 steps in a single actions.
138208

139-
* ``$session->restart()`` will physically restart the browser. This action
140-
will physically clean **all** your cookies and browsing history by cost
141-
of browser reloading.
209+
The drawback of closing the browser and starting it again is that it takes
210+
time. In many cases, a lower level of isolation is enough in favor of a faster
211+
resetting. The ``Session::reset`` method covers this use case. It will try
212+
to clear the cookies and reset the request headers and the browser history
213+
in the limit of the driver possibilities.
142214

143-
Taking all this into account, it would be the best way to use ``reset()``
144-
by default and to call ``restart()`` in cases when we need really full isolation.
215+
Taking all this into account, it is recommended to use ``Session::reset()``
216+
by default and to call ``Session::stop()`` in cases when we need really full
217+
isolation.

0 commit comments

Comments
 (0)