4 * This file is part of the Behat MinkExtension.
5 * (c) Konstantin Kudryashov <ever.zet@gmail.com>
7 * For the full copyright and license information, please view the LICENSE
8 * file that was distributed with this source code.
11 namespace Behat\MinkExtension\Context;
14 use Behat\Mink\WebAssert;
15 use Behat\Mink\Session;
18 * Raw Mink context for Behat BDD tool.
19 * Provides raw Mink integration (without step definitions) and web assertions.
21 * @author Konstantin Kudryashov <ever.zet@gmail.com>
23 class RawMinkContext implements MinkAwareContext
26 private $minkParameters;
31 * @param Mink $mink Mink session manager
33 public function setMink(Mink $mink)
39 * Returns Mink instance.
43 public function getMink()
45 if (null === $this->mink) {
46 throw new \RuntimeException(
47 'Mink instance has not been set on Mink context class. ' .
48 'Have you enabled the Mink Extension?'
56 * Returns the parameters provided for Mink.
60 public function getMinkParameters()
62 return $this->minkParameters;
66 * Sets parameters provided for Mink.
68 * @param array $parameters
70 public function setMinkParameters(array $parameters)
72 $this->minkParameters = $parameters;
76 * Returns specific mink parameter.
82 public function getMinkParameter($name)
84 return isset($this->minkParameters[$name]) ? $this->minkParameters[$name] : null;
88 * Applies the given parameter to the Mink configuration. Consider that all parameters get reset for each
91 * @param string $name The key of the parameter
92 * @param string $value The value of the parameter
94 public function setMinkParameter($name, $value)
96 $this->minkParameters[$name] = $value;
100 * Returns Mink session.
102 * @param string|null $name name of the session OR active session will be used
106 public function getSession($name = null)
108 return $this->getMink()->getSession($name);
112 * Returns Mink session assertion tool.
114 * @param string|null $name name of the session OR active session will be used
118 public function assertSession($name = null)
120 return $this->getMink()->assertSession($name);
124 * Visits provided relative path using provided or default session.
126 * @param string $path
127 * @param string|null $sessionName
129 public function visitPath($path, $sessionName = null)
131 $this->getSession($sessionName)->visit($this->locatePath($path));
135 * Locates url, based on provided path.
136 * Override to provide custom routing mechanism.
138 * @param string $path
142 public function locatePath($path)
144 $startUrl = rtrim($this->getMinkParameter('base_url'), '/') . '/';
146 return 0 !== strpos($path, 'http') ? $startUrl . ltrim($path, '/') : $path;
150 * Save a screenshot of the current window to the file system.
152 * @param string $filename Desired filename, defaults to
153 * <browser_name>_<ISO 8601 date>_<randomId>.png
154 * @param string $filepath Desired filepath, defaults to
155 * upload_tmp_dir, falls back to sys_get_temp_dir()
157 public function saveScreenshot($filename = null, $filepath = null)
159 // Under Cygwin, uniqid with more_entropy must be set to true.
160 // No effect in other environments.
161 $filename = $filename ?: sprintf('%s_%s_%s.%s', $this->getMinkParameter('browser_name'), date('c'), uniqid('', true), 'png');
162 $filepath = $filepath ? $filepath : (ini_get('upload_tmp_dir') ? ini_get('upload_tmp_dir') : sys_get_temp_dir());
163 file_put_contents($filepath . '/' . $filename, $this->getSession()->getScreenshot());