3 namespace Zumba\Mink\Driver;
5 use Behat\Mink\Driver\CoreDriver;
6 use Behat\Mink\Exception\DriverException;
7 use Zumba\GastonJS\Browser\Browser;
10 * Class BasePhantomJSDriver
11 * @package Zumba\Mink\Driver
13 class BasePhantomJSDriver extends CoreDriver {
18 protected $phantomHost;
19 /** @var \Twig_Loader_Filesystem */
20 protected $templateLoader;
21 /** @var \Twig_Environment */
22 protected $templateEnv;
25 * Instantiates the driver
26 * @param string $phantomHost browser "api" oriented host
27 * @param string $templateCache where we are going to store the templates cache
29 public function __construct($phantomHost, $templateCache = null) {
30 $this->phantomHost = $phantomHost;
31 $this->browser = new Browser($phantomHost);
32 $this->templateLoader = new \Twig_Loader_Filesystem(realpath(__DIR__ . '/Resources/Script'));
33 $this->templateEnv = new \Twig_Environment($this->templateLoader, array('cache' => $this->templateCacheSetup($templateCache), 'strict_variables' => true));
37 * Sets up the cache template location for the scripts we are going to create with the driver
38 * @param $templateCache
40 * @throws DriverException
42 protected function templateCacheSetup($templateCache) {
43 $cacheDir = $templateCache;
44 if ($templateCache === null) {
45 $cacheDir = sys_get_temp_dir() . DIRECTORY_SEPARATOR . "jcalderonzumba" . DIRECTORY_SEPARATOR . "phantomjs";
46 if (!file_exists($cacheDir)) {
47 mkdir($cacheDir, 0777, true);
51 if (!file_exists($cacheDir)) {
52 throw new DriverException("Template cache $cacheDir directory does not exist");
58 * Helper to find a node element given an xpath
59 * @param string $xpath
62 * @throws DriverException
64 protected function findElement($xpath, $max = 1) {
65 $elements = $this->browser->find("xpath", $xpath);
66 if (!isset($elements["page_id"]) || !isset($elements["ids"]) || count($elements["ids"]) !== $max) {
67 throw new DriverException("Failed to get elements with given $xpath");
75 public function getBrowser() {
76 return $this->browser;
80 * @return \Twig_Environment
82 public function getTemplateEnv() {
83 return $this->templateEnv;
87 * Returns a javascript script via twig template engine
88 * @param $templateName
92 public function javascriptTemplateRender($templateName, $viewData) {
93 /** @var $templateEngine \Twig_Environment */
94 $templateEngine = $this->getTemplateEnv();
95 return $templateEngine->render($templateName, $viewData);