11Controlling 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 let you switch tabs!).
6+
7+ First, start your session (it's like opening your browser tab). Nothing can
8+ be done with it before starting it.
79
810.. code-block :: php
911
10- // init session:
12+ // Choose a Mink driver. More about it in later chapters.
13+ $driver = new \Behat\Mink\Driver\GoutteDriver();
14+
1115 $session = new \Behat\Mink\Session($driver);
1216
13- // start session:
17+ // start the session
1418 $session->start();
1519
1620 .. note ::
1721
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.
22+ The first argument to the session constructor is a driver object. Drivers
23+ are the way the Mink abstraction layer works. You will discover more
24+ about the available drivers in a :doc: `later chapter </guides/drivers >`.
2025
21- ``start() `` call is required in order to configure the browser emulator or
22- controller to be fully functional.
26+ .. caution ::
27+
28+ Although Mink does its best to remove differences between the different
29+ drivers, each driver has unique features and shortcomings. See the :ref: `driver-feature-support `
30+ to see which features are supported by each driver.
2331
2432Basic Browser Interaction
25- ~~~~~~~~~~~~~~~~~~~~~~~~~
33+ -------------------------
2634
27- After you've instantiated the ``$session `` object, you can control the actual
28- browser emulator with it:
35+ Now that your session is started, you'll want to open a page with it. Just
36+ after starting, the session is not on any page (in a real browser, you would
37+ on the ``about:blank `` page), and calling any other action is likely to fail.
2938
3039.. code-block :: php
3140
32- // open some page in browser:
3341 $session->visit('http://my_project.dev/some_page.php');
3442
35- // get the current page URL:
36- echo $session->getCurrentUrl();
43+ .. note ::
3744
38- // get the response status code:
39- echo $session->getStatusCode();
45+ Mink is primarily designed to be used for testing websites. To allow
46+ you to browse and test error pages, the ``Session::visit `` method does
47+ not consider error status codes as invalid. It will *not * throw an exception
48+ in this case. You will need to check the status code (or certain text
49+ on the page) to know if the response was successful or not.
50+
51+ Interacting with the Page
52+ -------------------------
53+
54+ The session gives you access to the page through the ``Session::getPage ``
55+ method. This allows you to :doc: `traverse </guides/traversing-pages >` and
56+ :doc: `manipulate </guides/manipulating-pages >` it. The next chapters cover
57+ the page API in depth. Most of what you'll do with Mink will use this object,
58+ but you can continue reading to learn more about the Session.
4059
41- // get page content:
42- echo $session->getPage()->getContent();
60+ Using the Browser History
61+ -------------------------
4362
44- // open another page:
45- $session->visit('http://my_project.dev/second_page.php');
63+ The session gives you access to the browser history:
64+
65+ .. code-block :: php
66+
67+ // get the current page URL:
68+ echo $session->getCurrentUrl();
4669
4770 // use history controls:
4871 $session->reload();
4972 $session->back();
5073 $session->forward();
5174
52- // evaluate JS expression:
53- echo $session->evaluateScript(
54- "return 'something from browser';"
55- );
75+ Cookie Management
76+ -----------------
5677
57- // wait for n milliseconds or
58- // till JS expression becomes true:
59- $session->wait(
60- 5000,
61- "$('.suggestions-results').children().length > 0"
62- );
78+ The session can manipulate cookies available in the browser.
79+
80+ .. code-block :: php
81+
82+ // set cookie:
83+ $session->setCookie('cookie name', 'value');
84+
85+ // get cookie:
86+ echo $session->getCookie('cookie name');
87+
88+ // delete cookie:
89+ $session->setCookie('cookie name', null);
6390
6491 .. note ::
6592
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.
93+ With drivers that use JavaScript to control the browser - like Sahi -
94+ you may be restricted to accessing/setting all, but `HttpOnly cookies `_ .
6995
70- Cookies and Headers management
71- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
96+ Status Code Retrieval
97+ ---------------------
7298
73- With ``Behat\Mink\Session `` you can control your browsers cookies and headers:
99+ The session lets you retrieve the HTTP status code of the response:
100+
101+ .. code-block :: php
102+
103+ echo $session->getStatusCode();
104+
105+ Headers Management
106+ ------------------
107+
108+ The session lets you manipulate request headers and access response headers:
74109
75110.. code-block :: php
76111
@@ -80,65 +115,101 @@ With ``Behat\Mink\Session`` you can control your browsers cookies and headers:
80115 // retrieving response headers:
81116 print_r($session->getResponseHeaders());
82117
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-
92118 .. note ::
93119
94- Headers handling is only supported in headless drivers, because there
95- is no way browser controllers can get such information out of the browser .
120+ Headers handling is only supported in headless drivers (e.g. Goutte).
121+ Browser controllers (e.g. Selenium2) cannot access that information .
96122
97123HTTP Authentication
98- ~~~~~~~~~~~~~~~~~~~
124+ -------------------
99125
100- Also, Mink session has a special method to perform HTTP Basic authentication:
126+ The session has a special method to perform HTTP Basic authentication:
101127
102128.. code-block :: php
103129
104130 $session->setBasicAuth($user, $password);
105131
132+ The method can also be used to reset a previous authentication:
133+
134+ .. code-block :: php
135+
136+ $session->setBasicAuth(false);
137+
106138 .. note ::
107139
108140 Automatic HTTP authentication is only supported in headless drivers.
109141 Because HTTP authentication in browser requires manual user action, that
110142 can't be done remotely for browser controllers.
111143
144+ Javascript Evaluation
145+ ---------------------
146+
147+ The session allows you to execute or evaluate Javascript.
148+
149+ .. code-block :: php
150+
151+ // Execute JS
152+ $session->executeScript('document.body.firstChild.innerHTML = "";');
153+
154+ // evaluate JS expression:
155+ echo $session->evaluateScript(
156+ "return 'something from browser';"
157+ );
158+
159+ .. note ::
160+
161+ The difference between these methods is that ``Session::evaluateScript ``
162+ returns the result of the expression. When you don't need to get a return
163+ value, using ``Session::executeScript `` is better.
164+
165+ You can also wait until a given JS expression returns a truthy value or the
166+ timeout is reached:
167+
168+ .. code-block :: php
169+
170+ // wait for n milliseconds or
171+ // till JS expression becomes truthy:
172+ $session->wait(
173+ 5000,
174+ "$('.suggestions-results').children().length"
175+ );
176+
177+ .. note ::
178+
179+ The ``Session::wait `` method returns ``true `` when the evaluation becomes
180+ truthy. It will return ``false `` when the timeout is reached.
181+
112182Resetting the Session
113- ~~~~~~~~~~~~~~~~~~~~~
183+ ---------------------
114184
115185The 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:
186+ for acceptance tests. But a very important part in testing is isolation.
187+
188+ Mink provides two very useful methods to isolate tests, which can be used
189+ in your test's ``teardown `` methods:
119190
120191.. code-block :: php
121192
122193 // soft-reset:
123194 $session->reset();
124195
125196 // hard-reset:
197+ $session->stop();
198+ // or if you want to start again at the same time
126199 $session->restart();
127200
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:
201+ Stopping the session is the best way to reset the session to its initial
202+ state. It will close the browser entirely. To use the session again, you
203+ need to start the session before any other action. The ``Session::restart ``
204+ shortcut allows you to do these 2 steps in a single call.
130205
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.
206+ The drawback of closing the browser and starting it again is that it takes
207+ time. In many cases, a lower level of isolation is enough in favor of a faster
208+ resetting. The ``Session::reset `` method covers this use case. It will try
209+ to clear the cookies and reset the request headers and the browser history
210+ to the limit of the driver possibilities.
138211
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.
212+ Taking all this into account, it is recommended to use ``Session::reset() ``
213+ by default and to call ``Session::stop() `` when you need really full isolation.
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+ .. _HttpOnly cookies : http://en.wikipedia.org/wiki/HTTP_cookie#HttpOnly_cookie
0 commit comments